Memory Management
1. What is memory management?
The process of managing memory allocation during the process of running the program, when memory needs to apply for a piece of memory space, do not need to be released.
2. How to manage memory
To manipulate memory at the point of view of assigning object ownership.
3. Two ways of memory management
A. MRR (Manual Retain Release) manual management, implementation mechanism: reference counting (reference counting mechanism).
B. ARC (auto Reference counting) automatic reference count, implementation mechanism: The system in the program compile phase automatically added the method of releasing objects.
4. Two situations that cause memory errors
A. Release an object that is in use.
B. Unused objects are not released, causing a memory leak.
5. Basic principles of memory management
A. objects created with methods that begin with "alloc", "new", "copy", "mutablecopy" have ownership and should be responsible for releasing .
B. You can use retain (the Stter method of the property and the Init method) instead of the object that you created yourself, but you want to get ownershipof the object.
C. When an object is not in use, it should release its own share of ownership (release autorelease).
d. You cannot release an object that you do not have ownership of .
6. You can use Autorelease to implement deferred release, where (return an object in a method)
-(NSString *) fullname{ *string = [[[[NSString alloc] Initwithformat:@ "%@:%@ " , Self.name, self.address] autorelease]; return string ;} -(void) printfullname{ *temp = [self fullName]; NSLog (@ "%@", temp);}
7. The difference between release and Autorelease
A. Advantages of release: Memory can be released immediately
Autorelease Advantages: Can delay release, easy to operate, small error probability. Memory is increased over a period of time.
B. Usage habits within the enterprise: Autorelease
8. How to free up resources owned by an object
By overriding the Dealloc method of the parent class in the implementation file of the class to implement releasing the resources owned by an object itself
A. The Dealloc method is that when the owner of this object is 0, the system automatically calls the object's Dealloc method and cannot actively invoke the
B. In the Dealloc method, release the ownership of your object before calling [Super Dealloc] to release yourself
9. Actual operation
A. Use accessor methods as much as possible to manipulate property variables (self.name).
B. Try not to use the accessor method in the Dealloc method.
Ten. Retain cycle
A object has a B object inside, B object has a object, when a to release their own time, must wait for the B object to release their own ownership of a, b objects to release their own time, must wait for the a object to release their own ownership, so both can not release.
Solution: Large strong reference small (retain strong) small weak reference large (weak).
11. You can not use Retaincount to see the owner of an object, only to ensure that the code is in accordance with the principle of memory management is OK.
To override the setter method:
-(void) Setfirstname: (NSString *) afirstname{ if (firstName! = afirstname) { [ FirstName release]; = [afirstname retain];} } -(NSString *) firstname{ return firstName;}
2015.12.21 memory Management (management)