In ios6, The viewdidunload callback method is deprecated. View Apple's documentation and see the following description.
So what should I do with the code originally in viewdidunload? In ios6, how should I handle the memory warning? With these questions, I have found some materials and shared them here.
Analysis
In ios4 and ios5 systems, when the memory is insufficient and the application receives memory warning, the system automatically calls the viewdidunload method of viewcontroller that is not currently on the interface. Normally, the viewcontroller that is not displayed on the interface is uinavigationcontroller.
The viewcontroller that is not on the top of the stack in the push stack, and the Child viewcontroller that is not displayed in the uitabbarviewcontroller. These views
The viewdidunload method is automatically called by the system when the memory warning event occurs.
In ios6, because the viewdidunload event is not triggered in ios6 under any circumstances, Apple recommends that you move the memory reclaim operations to another callback function: didreceivemorywarning. However, if you only move the Code previously written to the viewdidunload function to the didreceivememorywarning function, you will be wrong. The following is an error sample code:
[HTML]View
Plaincopy
- <Span style = "font-size: 18px;">-(void) didreceivememorywarning {
- [Super didreceivememorywarning];
- If ([self isviewloaded] &! [[Self view] window]) {
- [Self setview: Nil];
- }
- } </Span>
This article explains why ios6 does not recommend you set the view to nil (the link needs to be turned over to the wall). The translation is as follows:
Uiview has a member variable of calayer. calayer is used to draw itself to the screen. As shown in:
Calayer is a container class for bitmap images. When uiview calls its own drawrect, calayer creates this bitmap image class.
The memory actually occupies a bitmap image class. calayer only occupies 48 bytes, and uiview only occupies 96 bytes. The bitmap class of an iPad full screen uiview accounts for 12 Mb!
During ios6, when the system sends out memorywarning, the system automatically recycles the bitmap class. However, the uiview and calayer classes are not recycled. In this way, most of the memory is recycled and the drawrect of uiview can be called to re-build the bitmap class.
Memory Optimization
In addition, the article also mentions a memory optimization technique for Apple's operating system, which is explained as follows:
When a memory segment is allocated, it is marked as "in use" to prevent reuse. When the memory is released, the memory will be marked as "not inuse". In this way, the memory may be allocated to other variables when new memory applications exist.
The private member variable type of the specific bitmap content included by calayer is cabackingstore. When memroywarning is received, the memory area of the cabackingstore type will be marked as volatile (volatile, C, and Java language volatile are not the same here). Volatile indicates that this memory may be reused by the original variable again.
In this way, with the above optimization, when receiving memoy warning, although all the bitmap memory contained by calayer is marked as volatile, as long as this memory is not reused, when the bitmap memory needs to be rebuilt, it can be reused directly, instead of calling the drawrect method of uiview again.
Summary
Therefore, for ios6, you do not need to do any previous viewdidunload tasks, and do not need to move the previous viewdidunload code to the didreceivemorywarning method.
Reference a paragraph in WWDC 2012 to say goodbye to viewdidunload:
The method viewwillunload and viewdidunload. we're not going to call them anymore. I mean, there's kind of a cost-benifit equation and analysis that we went through. in the early days, there was a real performance need for us
Ensure that on memory warnings we unloaded views. there was all kinds of graphics and backing stores and so forth that wocould also get unloaded. we now unload those independently of the view, so it isn't that big of a deal for us for those to be unloaded, and
There were so far bugs where there wocould be pointers.