OBJECTIVE-C Basic 2: Memory Management Basics

Source: Internet
Author: User
Tags compact

1. Memory Storage Area

C, C + + inside. Stack area: Stores temporary variables and objects. Heap Area: stores dynamically allocated objects. Static variable Store: stores static variables and constant objects.

The memory storage area of OC is the same as C and C + +.

2. Why memory Management

Wrote C, C + + programs are aware that memory management is always a big pain point in C + + programs, the project crashes all from memory-related operations, especially pointer operations and memory operations, a little attention will result in a memory access violation caused the program crashes. So how to do memory management, personally think that there are the following principles: as far as possible to use the system to provide us with the package object, do not use the original, such as String instead of using char*, string can avoid the majority of the project strings violations, can be allocated on the stack is not allocated on the heap, Use the automatic release function of the stack to help us release; For complex memory operations such as multiple classes or multiple threads referencing the same piece of memory, add a reference count to the memory, and the memory operation must be empty.

We know that all objects in OC are dynamically allocated, so how do you manage those objects in OC? the answer is to use reference counting to manage .

Object memory Management in 3.OC

The life cycle of an object is managed by reference counting in OC.

-(Instancetype) retain objc_arc_unavailable; Citation plus 1

-(OneWay void) release objc_arc_unavailable; Reference minus 1

-(Instancetype) autorelease objc_arc_unavailable; Automatically release objects

-(Nsuinteger) Retaincount objc_arc_unavailable; Returns the current reference technology

When object B has object A, it is best to drop the reference count with retain when setting object A, and to reduce the reference technique within the Dealloc method.

Note the following code in (void) SetA: (A *) Pain implementation

-(void) SetA: (A *) pAIn

{

[PAIn retain];

[PA release];

PA = PAIn;

}

It is strongly recommended to keep the pain, then release the PA, and then assign a value that effectively avoids the pain and PA pointing to the same object.

The code is as follows:

@interface a:nsobject-(void) disinfo; @end @implementation A-(void) disinfo{    NSLog (@ "I ' m A haha!");} @end @interface b:nsobject{@private a    * PA;} -(void) SetA: (A *) pain;-(void) DisA; @end @implementation B (void) dealloc{    [PA release];    [Super Dealloc];} -(void) SetA: (A *) pain{    [pAIn retain];    [PA release];    PA = PAIn;} -(void) disa{       [PA disinfo];} @endint Main (int argc, const char * argv[]) {        A * PA = [A new];    b* PB = [B new];    [PB SETA:PA];    [PB DisA];    [PB release];    [PA release];    return 0;}

In the above example we need to manually drop the retain and release methods, that is, we need to know when to retain and when to release, it is clear that in the case of complex business logic needs to trace the problem of reference count will take more time. So, how do you make objects automatically retain and release? The person who wrote the Windows COM program knows CCOMPTR this smart pointer class, how does OC do it? The following is revealed.

Automatic memory release in 4.OC

The auto-release pool @autoreleasepool and NSAutoreleasePool mate objects in the cocoa Autorelease method to automatically free memory.

1) Use Autoreleasepool and autorelease. The code is as follows:

     @autoreleasepool    {        A * PA = [A new];        [PA autorelease];        b* PB = [B new];        [PB autorelease];        [PB SETA:PA];        [PB DisA];    }

The above code has the advantage over the code in 3: Call the Autorelease method directly after the PA and PB is created to add the object to the auto-release eat, the code is compact, no longer have to worry about when release.

2) Use NSAutoreleasePool and autorelease. The code is as follows:

nsautoreleasepool* pool = [NSAutoreleasePool new];    A * PA = [A new];    [PA autorelease];    b* PB = [B new];    [PB autorelease];    [PB SETA:PA];    [PB DisA];    [Pool release];

The code is similar to the code used to @autoreleasepool the keyword.

So in the face of two ways, which one should we choose? Personally think there are several principles:

1) for object Operation Compact code with @autoreleasepool, because his efficiency is higher than nsautoreleasepool

2) Object A has some example objects and these objects remain in an array such as Nsarray, with NSAutoreleasePool, because only the Dealloc object of A is called the release method of NSAutoreleasePool. The objects stored in the Nsarray are automatically released, and it is clear that @autoreleasepool does not have the ability to do so.

OBJECTIVE-C Basic 2: Memory Management Basics

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.