iOS memory management (i)

Source: Internet
Author: User

Recently there is time, just the basic knowledge of iOS to comb a bit, to record memory-related knowledge.

Before I understood memory management, I thought it was necessary to have some knowledge of the heap area and the stack area first.

Stack area: is automatically managed by the compiler memory allocation, the release of the area of the process, the storage function parameter values, local variables and so on. The stack is a contiguous area in memory, and its size is determined.

Heap area: We need to dynamically allocate, release, that is, our memory management of the protagonist.

Let's take a look at a simple example.

NSString *String = [NSString alloc] init];

We declare a variable of type nsstring and open up space for it, and let a nsstring type pointer string point to the space we open. The pointer string itself is stored in the stack, and &string is the address of the string pointer in the stack, and the string pointer points to the address of the NSString object we opened up in the heap space. In OC, all objects are essentially structs (the size of the struct cannot be changed dynamically, which is why the category cannot add member variables), so *string is the structure of the NSString object. A summary is that when you declare an object, the pointer to the object is stored in the stack, and the system finds the object itself in the heap by the object pointer in the stack area.

After concluding the above topic, let's look at another question. When we have a one-to-one relationship between pointers and objects, we can explain the memory management problem very well, so how do we manage our memory space when we have two pointers pointing to the same object and even multiple pointers to the same object?

nsmutablestring *stra = [[Nsmutablestring alloc] initwithformat:@ "memory management"];

nsmutablestring *STRB = [stra retain];

NSLog (@ "Stra:%@", stra);

NSLog (@ "Pointer to address objca:%p, objcb:%p", STRA,STRB);

NSLog (@ "The address of the pointer itself objca:%p, objcb:%p", &STRA,&STRB);

NSLog (@ "Retaincount objca:%d, objcb:%d", [Stra retaincount],[strb retaincount]);

[Stra release];

NSLog (@ "Retaincount objca:%d, objcb:%d", [Stra retaincount],[strb retaincount]);

We have defined two nsmutablestring types of pointers stra and STRB, but essentially pointers stra and STRB point to the same Nsmutablestring object. In this case, if the object memory is freed immediately after the stra is exhausted, then the problem will occur when STRB accesses the object, and it is time for iOS memory management to cope with this situation. The memory management mechanism of iOS is implemented by the reference counter (Retaincount), and in my understanding it appears that the Retaincount number of an object is how many pointers are currently pointing to the object, and the NSObject object in the example is pointing at both the pointer Stra and STRB , so the retaincount of the object is 2. What does [Stra release] mean? This method is to tell the system, stra the use of the object has been completed, at this time the object's retaincount will be reduced by one, the result is as follows.

  

To summarize, iOS requires a memory management mechanism because an object in iOS and a pointer to this object are stored in memory in the heap and stack areas, so in order to ensure that an object is not mistakenly released at the wrong time, This causes the pointer in the stack to be released when the object is accessed, and iOS introduces Retaincount to control when the object is released. The number of Retaincount is essentially how many pointers the current object is pointing to. Figuring out what we're going to do when we use retain,assign,strong,weak,copy these keywords is a good understanding of what they mean.

 

iOS memory management (i)

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.