1, memory management Simple Introduction
1, why should I have memory management? malloc selloc dealloc ' need to go back to review
The general memory 4s is 512m memory; 6 is 1024m memory;
When memory is too large, memory is exhausted. There is a program flash back.
2, OC Memory Management range:
Manages any inherited NSObject objects that are not valid for other basic data types.
3, the object type is dynamically allocated during the program running, stored in the heap area, memory management is mainly the memory management of the objects in the heap area.
4, OC Memory Management principle in order to prevent memory leaks
The reference counter for the object:
Each OC object has its own reference counter, which is an integer that indicates the number of times the object has been referenced, that is, how much of the object is now in use.
When an object has just been created, the default counter value is 1, and the object is destroyed when the value of the counter changes to 0 o'clock. The memory corresponding to this object will be freed.
5, Memory management classification:
MRC, manual management;
ARC, automatic reference counting management;
6, the principle of memory management (must adhere to this principle) remember two points:
1), who created (Alloc,new), who release or autorelease;
2), who retain,mutablecopy (copy), who release or autorelease;
7. Contents of memory Management Research
1) Wild Hands (Zombie object)
2) Memory leaks
2, single object memory management
1. Wild pointer: An uninitialized pointer or a pointer to the memory that has been disposed of.
Zombie objects: Objects that occupy memory that have been reclaimed are called Zombie objects, and zombie objects can no longer be used.
(By default, Xcode does not check zombie objects at all times to improve encoding efficiency, so the option to detect zombie objects is not turned on by default)
Interview questions: nil and nil and null, Nsnull difference:
Nil: is an object value; If we want to set an object to be empty, we use nil;
A null pointer to an Objective-c object. (#define NIL ((ID) 0))
Nil: Is the value of a class object, and nil is used if we want to set an object of type class to null; A null pointer to an OBJECTIVE-C class.
Null is a generic pointer to a pointer of type C, null pointer to anything else. (#define NULL ((void *) 0))
NSNull is a class in objective-c, used in situations where nil is not possible; A class defines a singleton object used to represent null values in collection Obje Cts
(which don ' t allow nil values).
[NSNull NULL]: The singleton instance of NSNull.
[NSNull NULL] is an object that he uses in situations where nil can not be used.
2. Memory leaks (the point of the stack is released, the space in the heap is not released, and the space in the heap is leaked)
Cause of leakage:
1, after the object has been created, there is no release;
The sum of 2,retain or alloc is greater than that of release or autorelease;
3. Improper use of nil; when retaincount>0, the object is assigned nil;
4. Retain the incoming object in the method.
3, multiple object memory management,memory management of Set method
1) Basic data type or C language construction type: Direct assignment
int float double long struct enum
-(void) Setage: (int) Age
{
_age=age;
}
2) OC Object type
-(void) Setcar: (Car *) car
{
1. First determine if the original object is passed in.
if (Car!=_car) {
2 Make a release to the old object once
[_car release];//If there is no old object, it has no effect
3. Do a retain on a new object
_car=[car retain];
}
}
4, @property parameters
1) whether to generate the Set method (not generated if it is a read-only property)
ReadOnly: Read only, generate getter declarations and implementations
ReadWrite: Default, generate both setter and getter declarations and implementations
2) multithreading management (Apple has blocked multithreaded operations to some extent)
Nonatomic: High performance, generally use this
Atomic: Low performance, default
3) memory Management related parameters
Retain: Release old value for object, retain new value (for OC object type)
Assign: Direct assignment (default, for non-OC object types)
Copy:release old value, copy new value
Verify that assign is actually directly assigned if it works on the object
4) name of the set and get methods
Modifies the name of the set and get methods, primarily for Boolean types. Because a method name that returns a Boolean type is typically preceded by an IS, modifying a getter whose name is typically used in a Boolean type.
@property (nonatomic,assign, setter=abc:,getter=haha) int age
1,getter=gettername,setter=settername, set the method name of setter and getter, unless you want to name it yourself, you will usually use the default. So as not to cause confusion.
2,readwrite,readonly (Generate only getter methods), set the limits available for access levels.
2,assign, which is directly assigned when the setter method is called, does not perform any retain operations.
3,retain, when invoking the setter method, first release the old value, and then execute retain on the new value given, which is equivalent to a copy of the pointer once.
4,copy,setter method for copy operation, as with the retain process, first release the old value, and then copy the new object, Retaincount is 1. is actually to create a new object.
5,nonatomic, non-atomic access, no synchronization, multi-threaded concurrent access improves performance. Atomic is a thread-protection technique used by OBJC, basically to prevent the data from being read by another thread when the write is not completed. This mechanism is resource-intensive, so nonatomic is a very good choice on small devices such as the iphone, if there is no communication programming between multiple threads.
[Email protected] and the use of cyclic retain
1. #import和 the difference between @class.
1. Difference in function
#import会包含引用类的所有信息 (content), including variables and methods for referencing classes
@class just tell the compiler that there is such a class, what information is in this class, completely unaware
2. The difference in efficiency
If there are hundreds of headers #import the same file, then once the initial header file changes slightly, the subsequent reference to the file of all the classes will need to recompile, compile efficiency is very low relatively speaking,
This is not the case with the @class method.
2. The scene of the cyclic retain
For example, a object retain B object, B object retain the A object loop retain the disadvantage of this will cause a object and B object can never be released
Solutions for circulating retain
When both ends are referenced, one end should be retain, one end with assign
6.NSString of memory management
NSString *teststr1 = @ "a";//Constant area
NSString *TESTSTR2 = [NSString stringwithstring:@ "a"];//constant area
NSString *TESTSTR3 = [NSString stringwithformat:@ "B"];//Heap area
NSString *TESTSTR4 = [[NSString alloc] initwithstring:@ "C"];//constant area
NSString *TESTSTR5 = [[NSString alloc] initwithformat:@ "D"];//Heap area
NSString *TESTSTR6 = [[NSString alloc] init];//no meaning
Seven. OC Foundation strengthens--1. Memory management 2. Wild pointer, memory leak 3.set method memory management [email protected] parameters [email protected] and loop retain use 6. NSString Memory Management