Objective-C memory management practices

Source: Internet
Author: User

Objective-C memoryThe management practice principle is the content to be introduced in this article. This series is intended to write some of my Iphone DevelopmentMemoryManagement understanding. Is based on the reader'sObjective-COfMemoryOn the basis of a certain understanding of management, the content will include practices,AutoreleasePrinciple Analysis,MemoryLeaked debugging. If youObjective-C memoryI don't know much about the management. I suggest you read the Vince Yuan tutorial first.

Memory Management is hard to understand during IPhone development.Objective-CA memory management mechanism between C # And C ++ is used. C # Is a GC Based on Mark-sweep. C ++ is basically allocated and released by programmers themselves. Ojbective-C is a programmer responsible for the distribution and Release of Mark through Release, retain, alloc) counting and the system. Below are some guidelines to avoid Memory leakage and use released memory to cause program crash.

1. You must be responsible for the objects you have created. Including alloc, newObject, mutableCopy, and so on, or the object you have called retain. For these objects, you must call release or autorelease.

2. You cannot release an Object returned by other functions. In principle, the called function is responsible for this. E.g. NSString str = [NSString stringWithFormat:]. You 'd better follow this rule for your own function. who applies for the function and who releases it, instead of letting the caller release it.

3. If you want to store another object in the property of an object, you must retain or copy it to avoid being release by others.

@ Propertyretain ). The retain keyword here will automatically do this. If you declare the set Method yourself, you need to do this manually.

4. 10 Other Tips for IPhone memory management. Which of the following are useful: Avoid usingAutoreleaseAnd [UIImage imageNamed:] to enable NSZombieEnabled.

5. weak reference. for example, in the Document class, the Page property points to the object of the Page class, and the Parent property in the page class points to the Document, so if the retain count between each other is 1, the two objects will never be Dealloc. The solution is to change page. parent to weak reference, that is, page. parent does not retain Document. UITableView. datasource, notification observers, delegates, and outline view items are all week reference.

Summary:Within Objective-CThe content of the stored management practice principles has been introduced. I hope this article will be helpful!

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.