Overview
In the process of development, many programmers do not pay attention to the management of objects or variable memory, causing the system or application to run for a period of time, it is slow, or crash. Let's talk about how memory is managed during programming.
What aspects does memory management contain?
Effective memory management typically contains two things:
Memory allocation: When a program creates an object, it needs to allocate memory for the object. Adopt a reasonable design, minimize the creation of objects, and reduce the memory overhead during the creation process.
Memory Reclamation: When objects are no longer needed by the program, the system must reclaim the memory occupied by these objects in a timely manner so that the program can use the memory again.
Before iOS 5, iOS developers needed to spend their time dealing with memory-recycling-related issues. After IOS 5, the features of the heart are introduced: auto-reference technology (ARC). With ARC, programmers can greatly improve the efficiency of development by not needing to focus on the content of memory recycling.
Objective-c memory recycling mechanisms are available in the following ways
- Manual reference Count MRC
- Automatic reference Technology ARC
Manual reference count
Now that the project is created by default arc, how can you change the project to MRC mode?
To create a project, open the project's configuration interface, select Search Automatic Reference, Target, Buld Setting, set
If you set the automatic reference count to No, you can manually reference the count.
Objective-c uses a count called reference count to track the state of an object: Each object has an integer associated with it, which is referred to as a reference counter. Normally, when a piece of code needs to access an object, the reference count of the object is added 1, and when the code is not accessing the object, the reference count of the object is reduced by 1, indicating that the code is not accessing the object. When an object has a reference count of 0, it means that the object is not needed by the program, and the memory used by the object is reclaimed by the system.
The Delloc method of the modified object is automatically called before the system destroys the object to perform some reclamation operations.
When the object is destroyed, the object no longer exists, and if there is a pointer to the destroyed object, the pointer is called a dangling pointer, also known as a wild pointer, a zombie pointer, a method that calls the pointer to the object, the program will often appear unknown results, or even cause the program to crash.
In the manual reference count, change the reference count of the object as follows:
- When a program invokes a method whose name starts with Alloc, new, copy, and Mutablecopy, the object's reference count is added by 1.
- When the program calls the object's retain method, the object's reference count is added by 1.
- When the program calls the object's release mode, the object's reference count is reduced by 1.
The method for reference counting is provided in NSObject:
- Retain: Reference count +1
- Release: Reference count-1
- Autorelease: Does not change the value of the reference counter of the object, just adds the object to the self-aware release pool.
- Retaincount: Returns the value of the reference count for this object.
Look at the test code:
1 //
2 // Item.m
3 // MRC
4 //
5 // Created by 万 齐 鹣 on 15/7/27.
6 // Copyright (c) 2015 Wan Qiyu. All rights reserved.
7 //
8
9 #import "Item.h"
10
11 @implementation Item
12
13-(instancetype) init
14 {
15 self = [super init];
16 if (self) {
17 NSLog (@ "init method, the reference count is:% ld", self.retainCount);
18}
19 return self;
20}
twenty one
22-(void) dealloc
twenty three {
24 NSLog (@ "Call to destroy method");
25 [super dealloc];
26}
27
28 @end
//
// main.m
// MRC
//
// Created by 万 齐 鹣 on 15/7/27.
// Copyright (c) 2015 Wan Qiyan. All rights reserved.
//
#import <Foundation / Foundation.h>
#import "Item.h"
int main (int argc, const char * argv []) {
@autoreleasepool {
// insert code here ...
Item * item = [[Item alloc] init]; // reference count 1
NSLog (@ "% ld", item.retainCount);
[item retain]; // reference count 2
NSLog (@ "% ld", item.retainCount);
[item retain]; // reference count 3
NSLog (@ "% ld", item.retainCount);
[item release]; // reference count 2
NSLog (@ "% ld", item.retainCount);
[item retain]; // reference count 3
NSLog (@ "% ld", item.retainCount);
[item release]; // reference count 2
NSLog (@ "% ld", item.retainCount);
[item release]; // reference count 1
NSLog (@ "% ld", item.retainCount);
[item release]; // reference count 0
}
return 0;
}
Results of the operation:
Summary of manual memory management rules
- If you need to keep an object from being destroyed, you can use retain. After you have finished using the object, you need to release it with release.
- Sending a release message to an object does not destroy the object, and the object is destroyed only if the object's reference count is reduced to 0 o'clock. The system then sends a DEALLOC message to the object to release the object.
- Any objects that use the retain or copy, Mutablecopy, Alloc, or new methods, and those that have the retain and copy attributes, need to override the Dealloc method so that they can be released when the object is freed.
- When the auto-release pool is emptied, it also does something for the automatically freed object. Each time the system sends release messages to each object in the pool when the auto-free pool is freed. If the object reference count in the pool drops to 0, the system sends a DEALLOC message to destroy the object.
- If the object is no longer needed in the method, but it needs to be returned, you can send an autolease message to this object to mark the object as deferred release. The autolease message does not affect the reference count of the object.
- When the application terminates, all objects in memory are freed, regardless of their release in the brake release pool.
- When the application is developed, the auto-free pool is created and emptied as the application runs. In this case, if you want to automatically free the pool when it is emptied after the object can also exist, the object needs to use the Retain method, as long as the reference count of these objects is greater than the data sent autorelease message, can be engaged in the pool cleanup survived.
OBJECTIVE-C Memory Management