------Android Training, Java training, look forward to communicating with you! ----------
1.OC Memory Management
1) Scope: Manage any inherited NSObject objects that are not valid for other basic data types (heap area), or cause memory leaks
2) Principle: Any object may be useful to one or more owners, as long as an object has at least one owner, it will continue to exist
3) The reference counter of the object, retain (+1), Release (-1), Retaincount to get the value of the reference counter
4) Classification of memory Management: MRC (Manual management), ARC (Automatic Management), garbage collection (not supported)
2. Use of reference counters
Dealloc: Releasing objects
First call [Super Dealloc]
We create a project, the default is ARC mode, first modify
//
// main.m
// memory management
//
// Created by fanyafang on 15/11/6.
// Copyright © 2015 itcast. All rights reserved.
//
#import <Foundation / Foundation.h>
#import "Person.h"
int main (int argc, const char * argv []) {
@autoreleasepool {
Person * p = [[Person alloc] init];
NSUInteger count = [p retainCount];
NSLog (@ "% lu", count);
// Person * p2 = p; // unchanged
// Person * p2 = [p retain]; // plus 1
[p retain]; // plus 1
[p release]; // minus one
[p release]; // minus one automatically call dealloc method
// Prove that the space of p is released, you can override the dealloc method in the Person class
}
return 0;
}
3. Principles of Memory management
1) As long as someone uses this object, the object will not be recycled.
2) As long as you want to use this object, then you should let the object's reference counter plus 1
3) When you do not want to use this object, you should let the object's reference counter minus 1
4) who created (new, malloc, copy) who release
5) who retain who release
4. If an object has been released, this object becomes a zombie object, then to use this object is meaningless, default does not error, need to turn on zombie object detection
Can't use retain to revive zombie objects
5. Object Nil class nil NULL nsnull is an object
The way to avoid using zombie objects is to assign a value of nil to an object after it is disposed
6. Memory Management for multiple objects
Release the instance object first in the Set function, and then retain the incoming object
If it's the same object, you don't need release and retain.
if (_car!=car) {
[_car release];
_car=[car retain];
}
In Dealloc, the release instance object
[_car release];
[Email protected] Parameters
1. Atomicity of Atomic nonatomic
2. Read and write ReadWrite readonly
3. Memory management Assign retain copy
Replace the Get Method name Setter=isvip,get method name Getter=isvip
Use of [email protected]
When importing a header file with import, all classes referencing the header file will need to be recompiled if the contents of the headers have changed
Use the @class class name instead of import
Tells the compiler that XXX is a class and does not need to be recompiled
However, there will be a prompt error, workaround, import again in the. m file
@class can solve the problem of circular introduction, Class A and Class B import each other, use class will not error
9. Loop retain will cause two objects to be memory leak
Prevention method: 1) Let an object be released more than once (note the order)
2) Recommended method: One end uses assign, the other end uses retain
10.NSString objects
[Email protected]
If a method needs to put back a newly created object, when does the object release?
Method is not freed internally because doing so will immediately release the object and return an empty object, and the caller will not voluntarily release the object
There was a memory leak at this point.
1) Create an auto-free pool @autoreleasepool {}
2) [Object autorelease];//join the auto-release pool, send message, release the call object when the pool ends
Dark Horse programmer-------Objective-c Basics 3