iOS learning the memory management in the fourth day--objective-c

Source: Internet
Author: User

First of all, the original text for the green jade at the desk written. I'm just studying.

Memory management in OC.

OC uses reference counting and garbage collection to manage memory, assigns a reference counter to each object in OC, and when an object has just been created its initial value is 1, and when a piece of code needs to access an object, the reference counter of that object is incremented by 1 (by retain), and when an object is accessed at the end, The object's reference counter is reduced by 1 (implemented by release), and the memory space occupied by the object is reclaimed when the counter is 0 o'clock. In the NSObject class, there is a Retaincount method that calls the method to get the reference count value of the current object.

Tips: If you need to use Retaincount when testing, you need to modify the settings in Xcode.

Change Yes to No.

  

1. Use a mutable array with a variable string to see the changes in the Retaincount.

?      ? ? (1). Create a mutable array object and create a mutable string Object--the Retaincount of the variable string is 1;

?      ? ? (2). Then add a variable string to the mutable array, observing the change in the retaincount of the variable string-- to each object in the mutable array, Retaincount +1 of the array.

?      ? ? (3). Remove a str in a mutable array and observe the change in Retaincount- The retainCount-1 of a mutable array.

        //test memory Access with a mutable arrayNsmutablearray *array = [Nsmutablearray arraywithcapacity:3]; //a mutable stringnsmutablestring *str = [[Nsmutablestring alloc] initwithstring:@"AA"]; //at this time the RetaincountNSLog (@"Strretain =%d when new Str is created",(int) [str retaincount]);        [Array addobject:str];        [Array addobject:str]; NSLog (@"Strretain =%d after adding two str elements", (int) [str retaincount]); [Array Removeobjectatindex:0]; NSLog (@"after removing an element Strretain =%d", (int) [str retaincount]);

? Changes in the Retaincount of 2.NSString

?    ? The Retaincount change of a string variable declared with NSString is determined by the way you initialize the string.

?    ? ? 1. Use nsstring *str = @ "Ludashi"; define variables

?    ?    ? The string defined in the above method is a constant string whose retiancount is 1 or variable and does not change with Retian or release

Objective-c arc (Automatic Reference counting)

? 1.? Issues to be aware of in arc:

?    ? ? (1). You cannot explicitly invoke the DEALLOC statement, or implement or invoke methods such as retain, release, Retiancount, and Autorelease.

?    ? ? (2). Cannot call @selector (retain), @selector (release), etc.;

?    ? ? (3). Cannot use NSAutoreleasePool object, use @autoreleasepool block to replace;

?    ? ? (4). You cannot define a property with the name of the start new unless you define a different getter name.

? ? 2.ARC Features

?    ? ? Arc adds a new attribute definition keyword, strong instead of retain, which represents a strong reference. Use weak to replace the Assign table for weakness references.

iOS learning the memory management in the fourth day--objective-c

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.