oc--Memory Management

Source: Internet
Author: User

------------Memory Management---------------

Memory Management classifications: ARC (automatic memory Management) and MRC (Manual memory Management)

ARC: Memory management Things have a system to do

MRC: Memory management Things are done by programmers

To manually manage memory, first turn the program into a manual memory management state

Retain make reference count +1

Release make reference count-1

Alloc/copy/new will make the reference count +1, but only for creating objects

Retaincount: Record the value of the reference counter

Note: The principle of Memory: a reference count of +1 will have-1, maintain balance.

Wild pointer: pointer to zombie object

Null pointer: nil when saved

When the Retaincount value of an object is 0 , when it is released, the system calls the Dealloc method

@implementation person

Note: The system automatically calls the method, through this method to determine whether the object has been released

-(void) dealloc{

This line of code must be written as long as the Dealloc method is called, and must be written on the last side of the method

[Super Dealloc];}

Person *person = [[Person alloc]init]; Start creating, Retaincount==1

[Person retain]; Retaincount +1

[Person release]; RetainCount-1

Note: Sending the release message to an object does not mean releasing the object, just making retainCount-1.

   Releasing the object is when the object is retaincount==0.

--------An object is used by multiple objects

Overriding the Dealloc method

This class uses another class as the attribute @property (nonatomic,retain) of the *room;

-(void) dealloc{

When the object where the member variable resides is freed, the contents of the object should also be released together

[_room release];

[Super Dealloc];}

-------------copy,mutablecopy----------

Note: The first step is to comply with the Copy,mutablecopy-related protocol (nscoping,nsmutbalecoping).

NSString has complied with this protocol, so it can be used directly, but the custom class must first obey and implement the relevant protocol.

NSString *string1 = @ "good for You";

Copies a copy of the original string and puts it into a newly opened space. This new string is mutable-a variable string is obtained by mutablecopy

nsmutablestring *mutstring1 = [string1 mutablecopy];

[MutString1 setstring:@ "OK"];

NSString *newstring = [mutString1 copy];

---------Automatic Release Pool-----------

Auto-free pool: A program can have many auto-free pools. An auto-free pool can be nested

Objects created in the nested auto-release pool are referenced by the current auto-release pool.

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];//old version

[Pool release];

@autoreleasepool {//new version

Person *person = [[Person alloc]init];

When sending an autorelease message to an object, the system will say that the object is placed in an array of the current auto-release pool

[Person Autorelease];

[Person release];

}//when the current auto-release pool ends, he sends a release message to every object inside.

//Using the automatic release pool reason: Because sometimes the object created is not easy to release immediately, so give him send Autorelease message, to the end of the automatic release of the pool released together }

Note: You cannot use release when you are not using Alloc,copy,new,retain because you want to keep the retaincount balance.

Do not take the array, the dictionary retaincount value to do something (judge the object's owner number)

AddObject of the array: The method will give the element an automatic reference count of +1 when used

oc--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.