First, Memory simple introduction
Memory structure
1. Run-time assignment
(1) Stack: a local variable created temporarily by the user to store the program (Advanced post-out).
(2) Heap: Dynamic allocation of memory segments.
2. Compiler assignment
(1) BSS segment: Holds uninitialized global variables and static variables.
(2) Data segment: Initialized global variables and static variables.
(3) Code snippet: an area of code execution.
Address from low to High: heap-and-Stack, BSS section, data segment, code segment
Memory allocation method
1, from the static storage area allocation. Memory is allocated at the time of program compilation, and the entire running period of the program is present in this block. For example, global variables, static variables.
2, create on the stack. When executing a function, the storage units of local variables within the function can be created on the stack, which are automatically freed when the function is executed at the end. The stack memory allocation operation is built into the processor's instruction set and is highly efficient, but allocates limited memory capacity.
3, allocated from the heap, also known as dynamic memory allocation. The lifetime of dynamic memory is determined by the programmer and is very flexible to use, but if space is allocated on the heap, it is the responsibility to reclaim it, otherwise the running program will have a memory leak, and frequently allocating and releasing different sizes of heap space will result in heap fragments.
Second, OC Memory management (iOS7.0 after the manual management, you can understand the relevant principles)
1, the memory management reason: Because the mobile device memory is extremely limited, so each app occupies a limited amount of memory, when the app consumes more memory, the system will issue a memory warning, then need to reclaim some of the memory space that does not need to continue to use, such as recycling some of the objects and variables are no longer used.
2. Memory Management scope: Any object that inherits NSObject is invalid for other basic data types.
The essential reason is that the storage space is not the same, the object is stored in the heap, and the other local variables are mainly stored in the stack, when the code block at the end of the code block involved in all the local variables will be recycled, the pointer to the object is also recycled, the object has no pointer pointing, but still in memory, causing memory leaks.
3, the basic structure of the object:每个OC对象都有自己的引用计数器,是一个整数表示对象被引用的次数,即现在有多少东西在使用这个对象。对象刚被创建时,默认计数器值为
1
,当计数器的值变为
0
时,则对象销毁。
在每个OC对象内部,都专门有
4
个字节的存储空间来存储引用计数器。
引用计数器的作用:
判断对象是否需要回收的唯一依据就是计数器是否为0
,若不为
0
则存在。
4. Reference counter action when sending messages to an object
Retain message: Make reference counter +1, change the method to return the object itself
Release message: Make reference counter-1 (does not represent releasing objects)
Retaincount message: Get the object's current reference counter value
5.当一个对象的引用计数器为
0
时,那么它将被销毁,其占用的内存被系统回收。当对象被销毁时,系统会自动向对象发送一条dealloc消息,一般会重写dealloc方法,书写格式如下:
1 -(void) dealloc2{3 //Must be 必须调用调用此方法
called 4 }
6. Precautions:
Wild pointer error: Access to a piece of bad memory (already recycled, unusable memory).
Zombie objects: Objects that occupy memory have been reclaimed, zombie objects can no longer be used. (Turn on zombie object detection)
Null pointer: No pointer to anything (storage is 0,null,nil), sending a message to a null pointer does not give an error
Note: You cannot use [P retaion] to revive a zombie object.
7. Memory Management Principles
(1) As long as someone else is using an object, the object will not be recycled.
As long as you want to use this object, then you should let the object reference counter +1.
When you do not want to use this object, you should let the object reference counter-1.
(2) who created, who release
If you create an object through Alloc,new,copy, you must call the release or Autorelease method
You don't create it, you don't have to be responsible.
(3) who retain, who release. As soon as you call the retain, no matter how the object is generated, you have to call release.
Third, the circular reference problem in memory management and the solution
If you use #import to include each other, a circular reference is formed. At this point you can use a @class to replace one of the #improt to solve the circular reference problem, improve performance!
@class just tell the compiler to handle the following name as a class when compiling.
Writing specification: @class class name;
Function: Declares a class that tells the compiler that a name is a class.
Specific usage: 1. Use @class in. h files to declare classes.
2. Use #import to include everything in the class when you really want to use it in. m files.
3, the two ends circular reference solution: One side uses the retain, one end uses the assign (uses the assign in the Dealloc also does not have to release again).
OC Basics (12) Memory Simple introduction and OC memory management