IOS memory warning memory Warning Level

Source: Internet
Author: User

Memory warning

We all know that many resources on mobile devices are scarce, especially when the memory is usually relatively small, and iphone4 only has 512 MB. In addition, ios4.0 and later support for multi-task, this problem is even more prominent. Therefore, we designProgramPay attention to the management of memory, reduce unnecessary overhead, beware of leakage.

Due to a serious memory leak in a small project, the program often exits after a short period of time. After the system's memry warning level is received several times during the debugging, then we receive a memory Warning Level 2 and exit the program. Generally, xcode4 is used as a tool to track and find that an image resource forgets the release. The problem was solved, but I didn't plan to bring it back. Then I searched for information about the memory warning level on the Internet, and found some useful data on stackoverflow, which was written by kennytm. I 'd like to share with you:

The system has four memory warnings, which are defined as follows:

Typedef Enum {

Osmemorynotificationlevelany =-1,

Osmemorynotificationlevelnormal = 0,

Osmemorynotificationlevelwarning = 1,

Osmemorynotificationlevelurgent = 2,

Osmemorynotificationlevelcritical = 3

} Osmemorynotificationlevel;

But how are these warnings changed? When the threshold value is reached, it will jump from one level to another,Article.

Generally, we receive the most memory Warning Level 1 in the program. This proves that the system memory is tight, and our program must make a corresponding response to release unnecessary memory. Generally, if we handle it properly, the memory warning stops and returns to normal. If we ignore the situation after receiving memory Warning Level 1, the system may attempt to notify Level 1 several times. If we still do not handle it, the system will issue a higher level of memory Warning Level 2. The general result is that our app is forced out and the system recovers the memory. Of course, when the system sends a Level 2 warning, it will also try to clean up the memory, such as closing unnecessary background programs. Obviously, we should not expect the system to clean up itself. This is like if your arm is broken, your body will certainly have a self-repair function, but if the wound is large, it must be handled manually.

I have not seen the level 3 warning yet. According to the stack over flow statement above, "When Level 3 warning occurs, the system kernel will take over, it is very likely that the main interface process (springborad) of iOS will be shut down and even restarted. "In this case, we cannot receive the process in our program, which is normal and our system's own things have been killed, we must have been killed.

The original address written by kennytm: http://stackoverflow.com/questions/2915247/iphone-os-memory-warnings-what-do-the-different-levels-mean

If you open the connection above, you can see the source file defining osmemoryicationicationlevel. You may notice that there is a function named osmemorynotifcurrentlevel () in it (),Can be used to obtain the current memory alarm level, but the header file # import <libkern/osmemorynotification. h> needs to be added. On the Internet, there is an openSource codeFor memwatcher, let's take a look. To tell the truth, I didn't understand it.

I have said so much, hoping to help you find out what the memory warning is. Generally, our program will not encounter a memory warning.CodeNote that in all alloc, retain, and copy fields, you can write release or directly create the autorelease object (which is not recommended for personal use). Before the release, you should develop a good habit of checking Memory leakage.

By the way, if a memory warning occurs during running, the program usually calls applicationdidreceivemorywarning in appdelegate first, and then the program notifies the viewcontroller to call its didrecievememorywarning method, at this time, we must plant and release unnecessary resources.

~~~~~~~~~~~~~~~~~~~~~~ Updated on January 1, March 9 ~~~~~~~~~~~~~~~~~~~~~~

Today, I wrote a small example to test memory leakage. The idea is as follows: In viewdidload of uiviewcontroller. view to add a custom private leaked uiview (enable a thread in the initialization function without stopping the home picture ). After running the program on my iPhone 4, the warning of level 1 will appear in less than one minute, and level 2 will be reported in about 10 seconds, and the program will be exited in about 5 seconds. As follows:

This also proves that the level 3 warning we mentioned above is generally unable to receive this inference.

Svmemorywarningviewcontroller. m

 //  
// Svmemorywarningviewcontroller. m
// Svmemorywarning
//
// Created by MAPLE on 3/9/12.
// Copyright (c) 2012 smileevday. All rights reserved.
//

# Import " Svmemorywarningviewcontroller. h "
# Import <Libkern/osmemorynotification. h>
# Import " Svleakmemoryview. h "

@ Interface Svmemorywarningviewcontroller ()

@ End

@ Implementation Svmemorywarningviewcontroller

-( Void ) Viewdidload
{
[Super viewdidload];
// Do any additional setup after loading the view, typically from a nib.

Svleakmemoryview * view = [[svleakmemoryview alloc] initwithframe: cgrectmake ( 0 , 0 , 320 , 480 )];
[Self. View addsubview: View];
[View release];
}

-( Void ) Viewdidunload
{
[Super viewdidunload];
// Release any retained subviews of the main view.
}

-(Bool) shouldautorotatetointerfaceorientation :( uiinterfaceorientation) interfaceorientation
{
Return Interfaceorientation = uiinterfaceorientationportrait;
}

-( Void ) Didreceivememorywarning
{

Nslog ( @" Recieve memory warning " );
Nslog ( @" ~~~~~~~~~~~~~~ Level ~~~~~~~~~~~~~~~ % D " ,( Int ) Osmemorynotifcurrentcurrentlevel ());
}

@ End

 

Svleakmemoryview. m

 # Import   "  Svleakmemoryview. h  " 
# Import <Quartzcore/quartzcore. h>

@ Implementation Svleakmemoryview

-( ID ) Initwithframe :( cgrect) Frame
{
Self = [Super initwithframe: frame];
If (Self ){
// Initialization code

Uiimageview * imageview = [[uiimageview alloc] initwithframe: frame];

Nsstring * filepath = [[nsbundle mainbundle] pathforresource:@" Car " Oftype: @" JPG " ];
Imageview. Image = [[uiimage alloc] initwithcontentsoffile: filepath];

Imageview. layer. contentsgravity = kcagravityresizeaspect;
[Self addsubview: imageview];

Nsstring * filepath2 = [[nsbundle mainbundle] pathforresource: @" Treehighresolution " Oftype:@" JPG " ];
Dispatch_queue_t loadimagequeue = dispatch_queue_create ( " Load Image " , Null );

_ Block uiimageview * testiv = [[uiimageview alloc] initwithframe: cgrectmake ( 0 , 0 , 320 , 480 )];
Dispatch_async (loadimagequeue, ^ {
While (True ){
Uiimage * image = [[uiimage alloc] initwithcontentsoffile: filepath2];
Testiv. Image = image;
}
});
}
Return Self;
}

/*
// Only override drawrect: If you perform custom drawing.
// An empty implementation adversely affects performance during animation.
-(Void) drawrect :( cgrect) rect
{
// Drawing code
}
*/

@ End
 


 
 
 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.