內存警告
		ios下每個app可用的內存是被限制的,如果一個app使用的內存超過了這個閥值,則系統會向該app發送Memory Warning消息。收到消息后,app必須盡可能多的釋放一些不必要的內存,否則OS會關閉app。
		幾種內存警告級別(便于理解內存警告之后的行為)
		 Memory warning level:
typedef enum {
OSMemoryNotificationLevelAny = -1,
OSMemoryNotificationLevelNormal = 0,
OSMemoryNotificationLevelWarning = 1,
OSMemoryNotificationLevelUrgent = 2,
OSMemoryNotificationLevelCritical = 3
}OSMemoryNotificationLevel;(5.0以后廢棄了)
		            1、Warning (not-normal) — 退出或者關閉一些不必要的后臺程序 e.g. Mail
		            2、Urgent — 退出所有的后臺程序 e.g. Safari and iPod.
		            3、Critical and beyond — 重啟
		響應內存警告:
		在應用程序委托中實現applicationDidReceiveMemoryWarning:方法:
		            應用程序委托對象中接收內存警告消息
		在您的UIViewController子類中實現didReceiveMemoryWarning方法:
		            視圖控制器中接收內存警告消息
		注冊UIApplicationDidReceiveMemoryWarningNotification通知:
		            其它類中使用通知接收內存警告消息(例如:清理緩存數據)
View Controller
		生成view:
		loadView
		1、loadView在每一次使用self.view這個property,并且self.view為nil的時候被調用,用以產生一個有效的self.view(手工維護views,必須重寫該方法)
		2、view 控制器收到didReceiveMemoryWarning的消息時, 默認的實現是檢查當前控制器的view是否在使用。 如果它的view不在當前正在使用的view hierarchy里面,且你的控制器實現了loadView方法,那么這個view將被release, loadView方法將被再次調用來創建一個新的view。(注:ios6.0以下 如果沒有實現loadview,內存警告時不會調用viewDidUnload)
		viewDidLoad
		一般我們會在這里做界面上的初始化操作,比如往view中添加一些子視圖、從數據庫或者網絡加載模型數據到子視圖中
		官網提供的生成view的流程圖:
		
		官網提供的卸載view的流程圖:
		
		On iOS 5 and Earlier
		1 系統發出警告或者ViewController本身調用導致didReceiveMemoryWarning被調用
		2 調用viewWillUnload之后釋放View
		3 調用viewDidUnload
		ios5.0 LeaksDemo 
-(void)didReceiveMemoryWarning
{
//In earlier versions of iOS, the system automatically attempts to unload a view controller's views when memory is low
[super didReceiveMemoryWarning];
//didReceiveMemoryWarining 會判斷當前ViewController的view是否顯示在window上,如果沒有顯示在window上,則didReceiveMemoryWarining 會自動將viewcontroller 的view以及其所有子view全部銷毀,然后調用viewcontroller的viewdidunload方法。
			
			}
			- (void)viewDidUnload
			{
			    // 被release的對象必須是在 viewDidLoad中能重新創建的對象
			    // For example: 
			       self.myOutlet = nil;
			    self.tableView = nil;
			    dataArray = nil;
			   
			    [super viewDidUnload];
			}
On iOS 6 and Later
1 系統發出警告或者ViewController本身調用導致didReceiveMemoryWarning被調用
2 - (void)didReceiveMemoryWarning;中釋放當前不在使用的資源
ios6.0 LeaksDemo
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];//即使沒有顯示在window上,也不會自動的將self.view釋放。注意跟ios6.0之前的區分
// Add code to clean up any of your own resources that are no longer necessary.
// 此處做兼容處理需要加上ios6.0的宏開關,保證是在6.0下使用的,6.0以前屏蔽以下代碼,否則會在下面使用self.view時自動加載viewDidUnLoad
if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
//需要注意的是self.isViewLoaded是必不可少的,其他方式訪問視圖會導致它加載,在WWDC視頻也忽視這一點。
if (self.isViewLoaded && !self.view.window)// 是否是正在使用的視圖
{
// Add code to preserve data stored in the views that might be
// needed later.
// Add code to clean up other strong references to the view in
// the view hierarchy.
self.view = nil;// 目的是再次進入時能夠重新加載調用viewDidLoad函數。
}
}
}
		內存不足時的處理
		當我們打開很多應用程序,以及3d游戲時,程序內存不足,會發生內存警告,那內存接下來會左些什么呢????? 
		內存警告關乎到一個進程問題!每一個程序都是一個進程,有的進程在程序進入后臺之后就開始了休眠,而有些程序進入后臺卻還一直在運行程序,比如QQ!當控制器接受到內存警告之后,會做如下方法:
		1.當控制器接收到內存警告時,會調用 didReceiveMemoryWarning 方法 
		2.didReceiveMemoryWarning方法內部的默認實現以下步驟: 首先會檢測控制器的view在不在屏幕上
if (self.view.superview == nil) { // 檢測控制器的view在不在屏幕上
// 就會嘗試銷毀控制器的view
// 即將銷毀的時候,就會調用控制器的 viewWillUnload
// 銷毀完畢的時候,就會調用控制器的 viewDidUnload方法
} else {
			// 不銷毀控制器的view
			}
3.當需要再次使用控制器的view時,又會調用loadView方法來創建view
		4.接著會調用一系列的生命周期方法 
		viewDidLoad —> ……
		5.生命周期循環 
		loadView –> viewDidLoad –> ..可見.. –內存警告–> didReceiveMemoryWarning —> viewWillUnload –> viewDidUnload —再次使用—> loadView
所以當我們的程序內存過大時,我們掛載在后臺的QQ有時候會出現已經推出的情況!當我們再次點擊的時候,QQ又重新加載運行起來!






















