Handle IPhone application memory leakage

Source: Internet
Author: User

Garbage collection is available on the IPhone. Remember to release the dynamically applied memory. In this article, we will summarize the possible memory leakage situations and detection methods.
 
To put it bluntly, there is such a dynamically applied memory, but wood has any pointer pointing to it.
 
For example, in C ++:
 
1. MyClass * foo = new MyClass;
2. foo = NULL;
 
Similarly in objective-C:
 
1. MyClass * foo = [[MyClass alloc] init];
2. foo = nil; // Memory leakage!
 
 
Or there is a class like this:
 
1. @ interface Foo: NSObject
2 .{}
3. @ end
4.
5. @ implementation Foo
6.-(id) init
7 .{
8. if (self = [super init]) {
9. NSMutableString * string = [[NSMutableString alloc] initWithString: @ "Leaker !!! "];
10. string = nil; // leaked! After this sentence, the string object will not be released until the application exits.
11 .}
12. return self;
13 .}
14.-(void) dealloc
15 .{
16. [super dealloc];
17 .}
18. @ end
 
What if memory leakage occurs?
A: It's a small leak. It's okay! When the application exits, the memory used will be cleared, including leaks. In addition, the IPhone application will not apply for shared memory, but will not affect the entire system, and will not cause system-wide memory leaks. If a large number of leaks occur and memory leaks occur repeatedly, at most applications will crash. However, if you want to release it to the AppStore, some memory leaks will be rejected by Apple .......
 
First, let's take a look at how memory is managed in the IPhone. First, automatic garbage collection is available for wood! Second, there are reference records!
The NSObject object has a property: retainCount, so all objects must inherit from NSObject to have a unified reference count ....
 
Remember the following eight things:
• 1 when retainCount = 0, the dealloc method of the object will be called and the object will be released.
• 2 never show calls to the dealloc Method
• 3 [obj retain]; // obj. retainCount ++ reference count + 1
• 4 [obj release]; // obj. retainCount -- reference count-1
• 5 MyClass * obj = [[MyClass alloc] init]; // obj. retainCount = 1 Initial Value: 1
• 6 for attributes with retain features, such as the following:
1. @ interface MyClass: NSObject {
2. NSObject * foo;
3 .}
4. @ property (noatomic, retain) NSObject * foo;
5. @ end
6.
7. @ implementation MyClass
8. @ synthesize foo;
9 ....
10. // The following two lines are correctly written
11. self. foo = bar; // [foo release]; foo = bar; [foo retain]; a release is sent to the original object and a retain is sent to the new object.
12. self. foo = nil; // [foo release]; a release message is sent to the object.
13.
14. // the following line is incorrectly written
15. foo = bar; // This method is incorrect if no release is sent to foo and no retain is sent to bar.
16 ....
17. @ end
Refer:
Apple's documents and specific implementation explanations

• 7 for objects created using the stringWithString method, when a Runloop ends, the object will be sent a release message. Note that there are two points. First, understand Runloop, to prevent objects from being released after Runloop is completed, use the instruments tool to detect memory leakage. The use of autorelease will also be treated as Memory leakage .......
• 8 for NSMutableArray, when addObject is added, the reference count of the object added to the array will be + 1. When the array itself is released, it sends a release message to all objects in the array. Www.2cto.com
 
Instruments can be used to detect memory Leaks. Leaks can be used for various tools.
Easy to use: Use Xcode (4.3) to open the corresponding project, Product-> Profile, and select the Leaks tool. In the future, it will dynamically record the locations where memory leaks occur during application operation. Note the following:
1. for objects that have been autorelisted, the Leak tool will also regard it as a Leak. You only need to know if it is okay.
2. Click Extended Detail. For each leak, you can view the call stack. Double-click an item in the call stack to locate the code, which is easy to check for errors and modify. The following two figures show the situation:





Author: dawn110110

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.