Objective-c Memory Management Module

Source: Internet
Author: User

Objective-c can fully automate the memory management of the program, relying mainly on the management mechanism of reference counting in the Objective-c

First of all:

The guidelines for memory management are: If you use Class A for an object. alloc,[mutable]copy[withzone],retaion Then you have to use the corresponding class B. Release or autorelease

Explanation: The use of the Class A keyword in each program requires that the object memory be freed from the keywords that use class B.

1. Memory management of arrays

An object declaration, the object's Retaincount = 1, and when the object is added to a mutable array, the Retaincount of the corresponding object is incremented by 1. Why is it? The reason is that when an object is added to an array, that element of the array gets ownership of the object. So the reference count of the object is incremented by one. When the array is released, iterate through each element in the array, releasing each element once, ensuring that the object is freed.

 Nsmutablearray *doglist= [[Nsmutablearray alloc] init]; // doglist-->retaincount = 1  dog  *dog1 = [[Dog alloc] init]; // dog1--retaincount = 1  //  add dog1 to Doglist   [Doglist Addobject:dog1]; // dog1--retaincount = 2   [Doglist Addobject:dog1]; // dog1--retaincount = 3   [dog1 release];  // --->retaincount 2 minus one  [doglist release];  //  release doglist itself, and every element of it has been released 

Extension: keyword @autoreleasepool an auto-free pool that works like an array, and when you talk about an object in method Autorelease [Dog1 autorelease], the compiler automatically adds the object to an array, Release the object again, and when the program finishes, it will automatically release all objects within the array.

2. Memory management of strings

String Object memory management is difficult to grasp from the reference count.

When taking: as the following code, you will be surprised to find: Retaincount =-1; Why is that?

1NSString *STR1 = [[NSString alloc] initwithstring:@"Hello World"];2NSLog (@"retainCount1 =%li", Str1.retaincount); RetainCount-13 [str1 release];4         5         6NSString *STR2 =[NSString stringwithstring:str1];7NSLog (@"RetainCount2 =%li", Str2.retaincount); RetainCount-18         9NSString *STR3 =[[NSString alloc] initwithstring:str2]; RetainCoutn-1TenSTR3 release];

NSString *STR7 = [[nsstring alloc] init];

NSLog(@ "STR7 retaincount =%li", STR7. Retaincount);

NSLog(@ "STR7 =%p", STR7); Print address and you will find 0x7fff75d4cd00 which is in the stack area of the TM used to say objects are not created in the stack memory area

nsmutablestring *mulstr1 = [[nsmutablestring alloc] initwithstring: STR5];

NSLog(@ "mulStr1 Retaincount =%li", mulStr1.  Retaincount); Retaincount = 1

NSLog(@ "MULSTR1 =%p", mulStr1);     

nsmutablestring *mulstr2 = [mulStr1 retain];

NSLog(@ "mulStr2 Retaincount =%li", mulStr2.  Retaincount); Retaincount = 2

NSLog(@ "MULSTR2 =%p", MULSTR2);

All right:

The conclusion is given first:

If the string is created in the stack and data segment, The value of Retaincount is 1, while the mutable string is created in heap memory, its retaincount is 1, and can be Retain method, theRetaincount value is increased by one

Although the reference count of the string object is not well grasped, the direct reference to the management rule can be effectively handled.

3.

Objective-c Memory Management Module

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.