iOS development How to check for memory leaks

Source: Internet
Author: User

this article reprinted to http://mobile.51cto.com/iphone-423391.htmin the development of memory leaks are unavoidable, but also we need to minimize, because memory leaks can greatly affect the stability of the program!!! Recently a burst of queries in the crazy query memory leaks, summed up there are several ways for everyone to reference.

AD:2014WOT Global Software Technology Summit Beijing Station course video release

In the development of memory leaks are unavoidable, but also we need to minimize, because memory leaks can greatly affect the stability of the program!!! Recently a burst of queries in the crazy query memory leaks, summed up there are several ways for everyone to reference.

1. Static analysis

With static analysis, we can get the most initial insight into some of the code's non-canonical places or memory leaks, which is our first step in detecting memory leaks. Of course, there are some warnings that we don't care about to skip.

2. Check for memory leaks with instruments

This method can roughly locate where we have a memory leak. The method is to complete a loop operation, if the memory growth of 0 proves that our program does not have a memory leak in the cycle operation, if memory growth is not 0 that proves that there is a possibility of memory leaks, of course, specific problems need to be analyzed.

3. Code Test Memory leaks

Before we do this, we have to notice whether we have freed all objects owned by the object in the Dealloc method. Observe whether the object's generation and destruction are paired. This is exactly how init (the method of creating the object) and Dealloc will be triggered (simply by walking once to create the object and then dealloc the object once).

Here are some of the more secretive cases of memory leaks that you encounter:

1. Two objects are owned by each other: that is, object a Retain/addsubview the B object, and the B object also retain/addsubview a object. Note: Delegate do not use the Retain property, and the Assign attribute will also result in owning each other.

2. Sometimes it is necessary to release with Removefromsuperview: Specifically, maybe my a object has a B object, B object is added to the C object, and in our design the life cycle of the B object should be the same as the A object; this time only one sentence [b release]/ SELF.B = nil is not able to release the B object (in general, release will make its retaincount-1,[super dealloc] will again all Subview retainCount-1, and B is not a subview, All the last time-1 is gone); so we need to add [b Removefromsuperview] before.

I hope you all share some of the more secret memory leaks that you've encountered.

iOS development How to check for memory leaks

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.