InIPhoneDuring development, it is very important to manage the memory correctly. The iPhone has 128 MRAM, but about half of it is used for screen buffering and other system processes. At the same time, the iPhone does not support writing memory to swap files, therefore, the iPhone only has about 64 MB of memory to run applications and is strictly limited by the amount of physical memory. In this way, we basically do not allow any memory leakage in the software we develop.
Because the iPhone has strict requirements on memory, when an object is no longer needed, it needs to release the memory space it occupies in time.
Objective-C memory management uses a very common technique based on Reference Count. In short, each object has an associated integer, which can be called a reference counter or a reserved counter. If you want to use an object and ensure that the object is not released during use, you need to call the function to obtain the "ownership", that is, add 1 to the reference counter, and call the function to release the "ownership" after use, so that the reference counter is reduced by 1. The acquisition and release of "ownership" increases and decreases the reference count. When the reference count is positive, there are still references to the table objects in the Data age. If the reference count is 0, tables can be released.
Differences between copy and retain
Copy: Create an object with an index count of 1 and release the old object.
Retain: Release the old object, assign the value of the old object to the input object, and increase the index count of the input object to 1.
What does the above mean?
Copy actually creates the same object, and retain is not:
For example, an NSString object with the address 0 × 1111 and the content @ "STR"
After copying data to another NSString, the address is 0 × 2222, the content is the same, the new object retain is 1, and the old object does not change
After retain reaches another NSString, the address is the same and a pointer is created, and the pointer is copied.) The content is of course the same. The retain value of this object is + 1.
That is to say, retain is a pointer copy and copy is a content copy. Wow, it's much easier than you think...
Release objects by mistake
Question 1:
- Value = [array objectAtIndex: n]; // obtain the object in an array.
- [Arry removeObjectAtIndex: n]; // unload the object
Because the value obtains the object, but another owner release the object, the value is now invalid data of the swing pointer)
Question 2:
- myArray = [NSArray array];
- ....
- [myArray release];
NSArray returns an automatically released object. Not only should myArray not be release after a period of time, but should be retained when appropriate to prevent the array from being accidentally released by the system.
Question 3:
- rocket = [rocketLauncher aRocket];
- [rocketLauncher release];
Like the Data Collection class object like array, if we get a sub-object of a class without retrying it, this rocket will actually lose its meaning when the original parent class is released.
Autorelease in different memory management environments of Cocoa
H hybrid memory management environment: Garbage Collection) + index Counting Reference Counting)
Although hybrid environments are not recommended in most cases, autorelease requires the following considerations:
Garbage collection in a hybrid environment: the drain method should be used, because release is meaningless in GC mode.
In the index counting environment: drain and release have the same effect for autoreleasepool to automatically release the pool.
Misunderstanding of autorelease
A Cocoa memory management can be divided into index Counting Reference Counting/Retain Count) and Garbage Collection ). The iPhone currently only supports the former, so autorelease has become a "shortcut" for many people ".
But! Autorelease is not "automatically released". Unlike the garbage collection method, the relationship between objects is detected and spam is deleted. However, autorelease is actually "delayed release". After a running cycle is marked as autorelease, it will be released.
Remember to use autorelease with caution and understand autorelease to prevent the object from being released by the system when you still need it.
Memory Management issues involved in Interface Builder
Key points:
If a variable is defined as an IBOutlet in the class, you do not need to instantiate it. The xib loader initializes it.
If a variable is defined as an IBOutlet in the class, you must release it. The xib loader won't help... ...
* Do not initialize two times. The memory overflows and an error occurs when the object is locked.
Questions about index count Reference Counting)
* Retain value = index count Reference Counting)
The NSArray object will retainretain value plus one) any object in the array. When NSArray is uninstalled dealloc), all objects in the array will be executed once to release the retain value minus one ). Not only NSArray, but also Collection class Classes. For example, NSDictionary or even UINavigationController.
The index count of the objects created by Alloc/init is 1. You do not need to retain it again.
[NSArray array], [NSDate date], and other "methods" create an object with an index count of 1, but it is also an automatically released object. So it is a local temporary object, so it doesn't matter. If it is the variable iVar intended to be used in the whole Class), you must retain it.
The "Auto Release" method is executed for the return values of the default class methods. * NSArray in the preceding example)
In the unload method "dealloc" in the class, release all NS objects that are not balanced. * All records that are not autorelisted and whose retain value is 1)
NSString Memory Management
Example:
- aString = @"I am a string that 2 years old, man!";
In this case, strings are stored and managed by the system.
- aString = [NSString stringWithFormat:@"I am a string that %d years old, man!",2];
In the second case, we need to go to the retain and release strings, regardless of the system.
Objective-C Memory Management
1. You initialize the alloc/init object, and you need to release. For example:
- NSMutableArray aArray = [[NSArray alloc] init];
After, you need
- [aArray release];
2. You need to release retain or copy. For example:
- [aArray retain]
After, you need
- [aArray release];
3. the objects to be passed (assign), retain and release you need to consider. For example:
- obj2 = [[obj1 someMethod] autorelease];
When object 2 receives an automatically released value of object 1, or transmits a basic data type NSInteger, NSString): You may want to retain object 2, to prevent it from being automatically released before being used. However, after retain, it is necessary to release it when appropriate.
Why can't I directly call dealloc instead of release?
Dealloc is not equal to free in C. dealloc does not release the memory or reduce the index count Reference counting. Therefore, calling dealloc directly fails to release the memory.
In Objective-C, index counting plays a decisive role.
Objective-C memory management basics
Objective-C introduction to the Cocoa framework
IOS development: Objective-C elegant syntax
Getting started with iPhone application development
10 essential Objective-C class libraries for iOS efficient development