iOS memory management

Source: Internet
Author: User

1.

iOS memory management uses a manual recycling mechanism, each time alloc Init/new/copy executes, the memory counter for an object will be +1, and the object performs a release operation 1. When the counter is 0 o'clock, the object is recycled. If the counter is currently counted as 0, the program will crash if the release is still executed.

If the method obtained by the variable does not belong to one of alloc new copy three, it can be considered to be set to Autorelease

Alloc: The result of sending a alloc message like a class is to assign the Class A large enough memory to hold the instance variable. Alloc also initializes the memory to 0. A piece of memory that has just been allocated is not immediately available and needs to be initialized before it can be used.

Init: Gets a piece of memory from the operating system and prepares it for storage objects. The call to be nested with the alloc, because the object returned by the initialization method might be different from the assigned object.

iphone does not have dynamic memory exchange, if you allocate some dynamic memory in the heap such as nsstring, even if the system memory is not enough, the system will not write it to disk to sort out some memory, instead of a memory warning. Dynamic memory swapping is too demanding for handheld systems and is therefore not enabled.
Compare the system of an iOS memory management article: http://blog.csdn.net/KOmyself/article/details/7549229

The spatial allocation of constant strings in memory differs from other objects in that they do not have a reference counting mechanism, so they can never be freed, [(Retain count) returns 0XFFFFFFFF]

An object that is referenced by multiple locations after creation (which may be referenced in an array or an instance variable elsewhere) only frees up memory when it is determined to be referenced, and the foundation provides an ingenious solution for tracking the number of references to an object: the reference count. (1 is created, each time the object must be persisted when the "myfraction retain" message is sent, the number of references plus 1 is no longer required, send the "myfraction release" message, so that the number of object references is reduced by 1 retain to 0 o'clock free memory to the object " Send Dealloc message ")

Adding an object to any type of collection causes the object to have a reference count of 1, which is why the object is released after each addsubview, which is not freed and can still be referenced. However, the copy operation retain the same size as the assignment value. Adding an object to the auto-free pool (release) does not affect its number of references and is only marked for later release.

 

2.

The difference between assign, retain, copy, Atomic, nonatomic:

Transferred from: http://blog.csdn.net/zcl369369/article/details/7551500

1). Suppose you allocate a piece of memory with malloc and assign its address to pointer a, and then you want pointer b to share the memory, so you assign a to (assign) B. At this time A and B point to the same piece of memory, I ask when a no longer need this memory, can you directly release it? The answer is no, because a does not know whether B is still using this memory, and if A is released, then B will cause the program to crash when it uses this memory.

2). Understand the problem of assign in 1, then how to solve? The simplest method is to use a reference count (reference counting), or the above example, we set a reference count for that memory, and when the memory is assigned and assigned to a, the reference count is 1. The reference count increases to 2 when a is assigned to B. If a no longer uses this memory, it only needs to subtract the reference count by 1, indicating that it no longer owns the memory. b The reference count is also reduced by 1 when the memory is no longer used. When the reference count becomes 0, the memory is no longer referenced by any pointers, and the system can release it directly.

3). Above two points is actually the difference between assign and retain, assign is directly assigned, which may cause 1 problems, when the data is int, float and other native types, you can use assign. Retain as described in 2, using a reference count, retain causes a reference count plus 1, release causes a reference count minus 1, when the reference count is 0 o'clock, the Dealloc function is called and memory is recycled.


4). Copy is used when you don't want a and b to share a piece of memory. A and B each have their own memory.


5). Atomic and nonatomic are used to determine whether the compiler-generated getter and setter are atomic operations. In a multithreaded environment, atomic operations are necessary, otherwise they may cause incorrect results. Adding the Atomic,setter function will change to the following:

if (Property! = newvalue) {            [property release];            property = [NewValue retain];        }  

iOS memory management

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.