OC basics: memory (Memory Management)

Source: Internet
Author: User

OC basics: memory (Memory Management)

Auto Release pool:

@ Autoreleasepool {

}

 

Memory Management Mechanism who is polluted and who manages

Garbage collection mechanism: gc (Garbage collection), memory managed by the system, developers do not need to manage.

OC has been supporting the garbage collection mechanism since version 2.0, but the iOS development platform does not support the garbage collection mechanism.

 

Objects inherited from NSObject need memory management

Memory Management through reference counters in OC

 

Two Methods of memory management by referencing counters

1. MRC :( maual reference count): manual reference count means that developers manage memory by reference count

2. ARC :( Automatic reference count), Automatic reference count, memory managed by the system automatically by reference count

ARC was created based on MRC.

When an object is generated using alloc, the reference count is changed from 0 to 1.

 

 

Print reference count

RetainCount: reference count

Print retainCount under ARC.

RetainCount occupies 4 bytes

When the retainCount value is 0, the dealloc method is automatically called.

 

-(Void) dealloc {

NSLog (@ "% @ the object has been destroyed", _ name );

[Superdealloc]; // The super dealloc must be called and written at the bottom.

}


 

Zombie object: the occupied memory has been recycled. Zombie objects cannot be used any more.

Wild pointer: pointer to a zombie object.

 

NULL Pointer; no pointer pointing to anything (nil, NULL, 0). For example: per1 = nil;

No error will be reported for NULL pointer operations

 

Alloc corresponds to dealloc retain corresponds to relase

 

 

Differences between autorelease and release

1. Compared with release, autorelease performs a minus operation on the reference counter, but not immediately minus one, but minus one at a certain time in the future (when the pool is automatically released)

2. essence of autorelease; when using autorelease for an object, the reference count of this object will not be reduced by one immediately, the object will be placed in the automatically released pool, and will be reduced by one only when the release pool exists.

 

For (int I = 0; I <1000000; I ++ ){

Person * person = [[Personalloc] init]; // 1

// The array will make a reference count for the added object + 1

// The array will perform the release operation on all elements added before releasing itself (reference count-1)

[Array addObject: person]; // 2

[Person release]; // 1

}

 

Use of copy:

The premise that the object uses copy: This class follows NSString and must implement the method in the Protocol.

Copy the content of a memory area to the new memory space? Go, copied? Regional introduction? The Count remains unchanged. The reference count of the new memory area is 1.

 

 


 

 

-(Id) copyWithZone :( NSZone *) zone {

// Copy a pointer (address)

// Return [self retain];

 

// Deep copy: copy the content.

// Return a new object, which occupies the same space as the original object and contains the same content.

Person * p = [[PersonallocWithZone: zone] init];

P. name = self. name;

Return p;

}


 

 

 

New: Apply for memory and change retainCount from 0 to 1

 

-------------------

 

// Memory management principles

// + 1: alloc, copy, new, retain

//-1: release, autorelease

// Once the reference counter of an object is 0, the system automatically calls the dealloc method, and then the object cannot be operated.

// Who polluted and who managed


 

 

// Two methods for automatically releasing the pool

// 1.

@ Autoreleasepool {

 

}

// 2. Another form of automatic release pool (if you want to release the pool you have created)

NSAID utoreleasepool * pool = [[NSAID utoreleasepoolalloc] init];

 

[Pool release];


 

 

 

 

 

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.