IPhone Memory Management Summary

Source: Internet
Author: User

IPhone memoryThe management summary is the content to be introduced in this article,IPhoneIn Objective-C development, as long as some Apple programming rules are followedMemoryManagement is easier, but there are also a lot of things to be aware.MemoryDebugging is also a headache.

1. For example, if an EXC_BAD_ACCESS error occurs, the error message is displayed to you. There is still a way to know how to identify the error,

Make the following settings:

 
 
  1. Project -> Edit active executable ->Argument  

Add the following four parameters:

 
 
  1. NSDebugEnabled  
  2. NSZombieEnabled  
  3. MallocStackLogging   
  4. MallocStackLoggingNoCompact 

And set them to YES. For example:

At this time, if you have the following code:

 
 
  1. // Release a variable again
  2. NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init];
  3. NSData * data = [NSData dataWithBytes: "asklaskdxjgr" length: 12];
  4. [Data release];
  5. [Pool release];

The following prompt is displayed in the Debug window:

 
 
  1. 2003-03-18 13:01:38.644 autoreleasebug[3939] *** *** Selector 'release'  
  2. ent to dealloced instance 0xa4e10 of class NSConcreteData. 

Although it is possible to roughly determine which type of variable is released repeatedly, there is not enough information. When the project is large and the source code is large, it is not easy to locate,

Run the following command in the console to obtain more information:

 
 
  1. shell malloc_history <pid> <address>" 

Enter the following command:

 
 
  1. shell malloc_history 3939 0xa4e10 

More information is displayed:

 
 
  1. [dave@host193 Frameworks]$ malloc_history 3939 0xa4e10  
  2. Call [2] [arg=32]: thread_a0000dec |0x1000 | start | _start | main |  
  3. +[NSData dataWithBytes:length:] | NSAllocateObject | object_getIndexedIvars |  
  4. malloc_zone_calloc   

At this time, we will know which function has a problem first. here we can see that the NSData in main has a problem.

2. NSArray and other collection classes.

The following code

 
 
  1. ReleaseTest* rt = [[alloc] init];  
  2. NSMutableArray *array = [[NSMutableArray alloc] init] ;  
  3. [array addObject: rt];  
  4. ReleaseTest *rt2 = [array objectAtIndex:0];  
  5.     [rt2 release];     
  6. [array release];  
  7. [rt release];  

Will lead to repeated releaseMemoryThe problem is that rt2 gets a pointer to an object. If it has been released, rt will be released again. In order to follow the principle of Init and Release, rt2 should not be Release.

3. Problems with init and Release.

All objects generated by using the Init method must be Release by themselves.

Objects not generated using the Init method do not need to be responsible for Release. for example, if the [NSString StringWithFormat] method generates an object, you do not need to perform the Release operation on your own. Therefore, you must define the function and set it to autoRelease when returning a class. In this way, the caller does not have to worry about whether to release the object.

4. In the AutoReleasePool, it is best not to assign the AutoRelease object to other objects. Otherwise, after leaving this scope, the object will be Release.

Summary: AboutIPhone memoryThe management summary is complete. I hope that this article will help you.IphoneIf you are interested in development, please go to the iphone development channel to learn more.

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.