OBJECTIVE-C Memory Management

Source: Internet
Author: User
Scope of memory management
All OC objects (inherited from the NSObject class)

Essential reason: Because the storage space of objects and other data types in the system is different, other local variables are mainly stored in the stack, and objects are stored in the heap.
When the code block ends, all local variables involved in this code block will be recycled, and the pointer to the object will also be recycled. At this time, the object no longer has a pointer to it, but still exists in memory, which will cause a memory leak

2. How do we perform memory management on OC objects?
By "reference counter" of the operation object
Reference counter
3.1 What is a reference counter?
1> Each OC object has its own reference counter
2> it is an integer (long type, takes 8 bytes)
3> Literally, it can be understood as "the number of times an object is referenced"
4> It can also be understood as: it indicates how many people are using this object
3.2 What does a reference counter do?
The system uses the "reference counter" to determine whether the current object can be released
How the object's "reference counter" operates
1> retain, +1
2> release, -1
3> retainCount, get the value of the object reference counter
4. dealloc method
When the object is about to be destroyed, the system automatically sends a dealloc message to the object
Therefore, whether the dealloc method is called can determine whether the object is destroyed
Overriding the dealloc method, you must call [super dealloc] and call it at the end, do not call the dealloc method directly
5. Wild pointer \ null pointer \ zombie object
Zombie objects: objects that have been destroyed (objects that can no longer be used)
Wild pointer: pointer to a zombie object (unavailable memory)
Null pointer: no pointer to storage space (nil is stored in it, which is 0)
Note: Sending a message to a null pointer has no effect, and it will not prompt an error. Nil Nil NULL [NSNULL null];
6. Multi-Object Memory Management
Set method memory management
 -(void) setCar: (Car *) car {
    if (_car! = car) {
       [_car release];
       _car = [car retain];
    }
 }


Dealloc method memory management
-(void) dealloc {
   [_car release];
   [super dealloc];
}

7. Apple's official memory management principles:
Who created who released,
If you create an object via alloc, new or copy, mutableCopy
Then you have to call release or autorelease
Who retains who releases
As long as you call retain, you must call release once
8. @ property's modified keywords
1> Control the memory management of the set method
retain: release old value, retain new value (for OC objects), to be used with nonatomic
assign: Assign directly without any memory management (default, for non-OC object types)
copy: release old value, copy new value (usually for NSString *)
2> Control whether the set method needs to be generated
readwrite: Generate both set and get methods (default)
readonly: only the get method is generated
3> Multi-thread management
atomic: low performance (default)
nonatomic: high performance (recommended for developing software for iOS systems, atomic for Mac software)
4> Control the name of the set method and get method
setter: Set the name of the set method, there must be a colon:
getter: set the name of the get method
9. @class usage
Why use @class .h
Resolving circular dependencies with @class
10. Loop retain problem in memory management
Solution: use retain on one end and assign on one end
Note: After using assign, there is no need for release in dealloc
11. Introduction to Automatic Release Pool
1. Creation of automatic release pool
The first
     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
     // code
     [pool release]; // [pool drain]; for mac
1
2
3
Second
@autoreleasepool
 {
   // Start represents creating an automatic release pool
 } // End represents the destruction of the automatic release pool

2. Benefits of automatic release pool
No need to care about release time
Doesn't matter when the release
3.autorelease basic usage
(1) The object will be placed in an automatic release pool
(2) When the automatic release pool is destroyed, a release will be made to all objects in the pool
(3) will return the object itself
(4) After calling the autorelease method, the object's counter is not affected (affected during destruction)

————————————————
Copyright statement: This article is an original article by the CSDN blogger "Early Young Man". It follows the CC 4.0 BY-SA copyright agreement. Please reprint the original source link and this statement.
Original link: https://blog.csdn.net/zl18603543572/article/details/87868991
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.