Good applications should release some resources that can be re-created with system memory warning. In iOS, we can receive system memory warning messages from the application delegate object, View Controller, and other classes.
1. Application delegate object
To receive memory warning messages in the application delegate object, you must override applicationdidreceivememorywarning: method. Appdelegate code snippet:
-(Void) applicationdidreceivememorywarning :( uiapplication *) Application {nslog (@ "applicationdidreceivememorywarning :");}
2. View Controller
To receive memory warning messages in the View Controller, rewrite the didreceivememorywarning method. Code snippet of viewcontroller:
-(Void) didreceivememorywarning {nslog (@ "call didreceivememorywarning in viewcontroller"); [Super didreceivememorywarning]; // release the member variable [_ listteams release];}
Note that the resource release code should be placed under the [Super didreceivememorywarning] statement.
3. Other classes
Notifications can be used in other classes. When a memory warning occurs, the IOS system sends a uiapplicationdidreceivememorywarningnotification Notification. All classes that have registered the uiapplicationdidreceivemorywarningnotification notification in the notification center receive a memory warning notification. Code snippet of viewcontroller:
-(Void) viewdidload {[Super viewdidload]; nsbundle * bundle = [nsbundle mainbundle]; nsstring * plistpath = [bundle pathforresource: @ "team" oftype: @ "plist"]; // obtain all data in the attribute list file nsarray * array = [[nsarray alloc] initwithcontentsoffile: plistpath]; self. listteams = array; [array release]; // receives memory warning notifications. Call the handlememorywarning method to handle nsicationicationcenter * center = [nsnotificationcenter defacenter center]; [center addobserver: Self selector: @ selector (handlememorywarning) Name: uiapplicationdidreceivememorywarningnotification object: Nil];} // handle memory warnings-(void) handlememorywarning {nslog (@ "handlememorywarning in viewcontroller calls");}
In the viewdidload method, we register the uiapplicationdidreceivememorywarningnotification message and call the handlememorywarning method to receive the alarm information. These codes can be written in other classes, and the didreceivememorywarning method can be rewritten in viewcontroller. In this example, we will introduce the uiapplicationdidreceivememorywarningnotification alarm message in a schematic way.
The memory warning is not often displayed on the device. Generally, we cannot simulate it. However, the simulator has a function that can simulate the memory warning, start the simulator, select the simulator menu hardware → simulate the memory warning, at this time, we will see the memory warning in the output window.
16:49:16. 419 respondmemorywarningsample [38236: c07] received memory warning.
16:49:16. 422 respondmemorywarningsample [38236: c07] Calling applicationdidreceivememorywarning in appdelegate:
16:49:16. 422 respondmemorywarningsample [38236: c07] handlememorywarning call in viewcontroller
16:49:16. 423 respondmemorywarningsample [38236: c07] Call didreceivememorywarning in viewcontroller