Talk about iOS memory management--continuous updates
Source: Internet
Author: User
<span id="Label3"></p><p><p>This article mainly talk about the development of iOS memory management, shortcomings, please advise, learn from each other Exchange. iOS development, can never avoid memory management, whether we consciously consider this matter, but as long as we write the OC program, it is related to memory Management.</p></p><p><p>first, What is Memory management?</p></p><p><p>second, memory management Mode one: MRC</p></p><p><p>(i) Reference counter</p></p><p><p>(ii) Principles</p></p><p><p>(iii) alloc, new, copy, mutablecopy, retain, release, dealloc</p></p><p><p>Alloc Internal implementation</p></p><p><p>Reference count Table</p></p><p><p>(d) autorelease</p></p><p><p>three, memory management Mode Ii: ARC</p></p><p><p>(a) __strong</p></p><p><p>(ii) __weak and __unsafe_unretained</p></p><p><p>(c) __autoreleaseing</p></p><p><p>Four</p></p><p><p></p></p><p><p>first, What is Memory management?</p></p><p><p>Memory management is the management of Memory. First look at a situation: we use code to create an object a, corresponding to allocate a piece of memory space to it, using the object a, and the system to say, the system is notified, to recover memory space. That's the way it ought. however, when using object a more than one person, Zhang San first run out, John Doe is still in use, Zhang San at this time notification system, "i do not use object a", the system received the notification, the memory space, but also using the object a John Doe can not be used (wild pointer), John Doe Heart Shadow area ... To solve this problem, Apple officially introduced the "reference counter", the reference counter is used to record how many "people" are currently using an object, then the decision to reclaim memory space becomes--the value of the reference counter is 0. There is a person starting with the object, the reference counter of the object is + 1, no, just-1. This solves the memory management problem when multiple people use the same object.</p></p><p><p>The problem of memory management, then, is focused on the management of reference Counters.</p></p><p><p>second, memory management Mode one: MRC</p></p><p><p>(a) how to think about manual memory management</p></p><p><p>As we all know, MRC is the manual management of reference counting Mode. Under mrc, Oc's memory management think Mode:</p></p><p><p>1, self-generated objects, own hold;</p></p><p><p>2, not their own generated objects, they can also hold;</p></p><p><p>3, the object that he no longer uses, releases;</p></p><p><p>4. Objects not owned by themselves cannot be released. (wild pointers, such as only one pool, but used two times in a row autorelease METHOD)</p></p><p><p>Explain: hold the meaning is retain, let counter +1, released means release, let counter-1.</p></p><p><p>(ii) alloc, new, copy, mutablecopy, dealloc</p></p><p><p>Use alloc, new, copy, mutablecopy to get the object, when not used, to Release. When the release method is called, the internal judge, If the counter is released after the counter is 0 o'clock, call the Object's dealloc method, revoke (reclaim, release) memory space.</p></p><p><p>(c) autorelease</p></p><p><p>Simply put, the Autorelease method of invoking object A in Autoreleasepool is to use object a, but not retain, the reference counter is not + 1, and object A is added to an array of the Auto-free pool, which is equivalent to Registering. When the Auto-release pool is drain, the Dealloc method of the Auto-release pool is called, and the Delloc method calls another method, iterates over the object in the array, and executes the Object's release Method.</p></p><p><p>three, memory management Mode Ii: ARC</p></p><p><p>later, Apple officially introduced the arc mode, people liberated a lot, because no longer write retain, release and other code to manage Memory. As long as the corresponding modifier is used to decorate the pointer to the memory space of the object, the system will automatically manage the memory for Us.</p></p><p><p>(a) __strong</p></p><p><p>(ii) __weak and __unsafe_unretained</p></p><p><p>(c) __autoreleaseing</p></p><p><p>Four</p></p><p><p></p></p><p><p>Talk about iOS memory management--continuous updates</p></p></span>
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