The Objective-c object allocates space in memory in a heap , and the heap memory is released by you, that is, release
Stacks are automatically freed by compiler management, and variables defined in the method (function body) are usually within the stack, so if your variable is to span a function you need to define it as a member variable.
1. Stack : The compiler automatically allocates releases, stores the function's parameter values, and local variables are equivalent. It operates in a manner similar to a stack in a data structure.
2. Heap Area : Typically released by programmers, if the programmer does not release it, it may cause a memory leak. The heap is different from the stack in the data structure, and its class is linked to the list.
The computer memory used by an application in an operating system IOS is not uniformly allocated space, and the running code uses space in three different memory areas, divided into three segments:"text segment","stack segment","heap Segment ".
The segment "text segment" is the memory segment in which the application code exists when the application runs. Each instruction, every single function, procedure, method, and execution code exists in this memory segment until the application exits. In general, you don't really have to know anything about this paragraph.
When the app starts, the function main () is called and some of the space is allocated in "stack." This is the memory space of another segment allocated for the application, which is the memory allocated for the function variable storage needs . Each time a function is called in the app,part of the stack is assigned to " stack" , which is called "frame". The local variable of the new function is assigned here.
As the name suggests,"stack" is a last-in, first-out (LIFO) structure. When the function calls other functions, the"stack frame" is created, and when the other function exits, the "frame" is automatically destroyed.
The "Heap" segment, also known as the "data" segment , provides a way to preserve the execution of mediation through functions, with global and static variables stored in the heap until the app exits .
To access the data you create in the heap, you require at least one pointer to be saved in the stack because your CPU accesses the data in the heap through pointers in the stack .
You can assume that a pointer in a stack is just an integer variable that holds the data for a specific memory address in the heap. In fact, it's a little bit more complicated, but it's the basic structure.
In short, the operating system accesses objects in a heap segment using pointer values from a stack segment. If a stack object's pointer is not available, the object in the heap cannot be accessed. This is also the cause of memory leaks.
In the stack and heap sections of the IOS operating system , you can create data objects.
the advantages of the stack object are two points, one is to create fast, the other is simple management, it has a strict life cycle . The disadvantage of a stack object is that it is inflexible. The length of the creation is how big it always is , what function was created when it was created, and its owner has always been it . Unlike the heap object , which has multiple owner, more than one owner is equivalent to a reference count. Only the heap object is managed with the reference count method.
Stack creation of objects
As long as the remaining space on the stack is greater than the space the stack object requested to create, the operating system will provide this memory space for the program, otherwise the exception prompt stack will be reported overflow.
Heap creation of objects
The operating system is managed with a linked list for the memory heap segment. The operating system has a linked list that records the free memory address, and when the application is received, it iterates through the list, looking for the first space that is larger than the requested heap node, and then removes the node from the list of idle nodes and assigns the node's space to the program .
For example:
The NSString object is the object in the stack, andthe object nsmutablestring is the object in the heap. the memory allocated at the time of creation is fixed and non-modifiable, the latter being allocated memory length is variable, can have multiple owner, applicable to the Count Management memory management mode .
Two kinds of object creation method also different, the former directly create "NSString * [email protected]" welcome "; ", while the latter need to first allocate the re-initialization" nsmutablestring * mstr1=[[nsmutablestring alloc] initwithstring:@ "Welcome"]; ".
The difference between stacks in iOS
Management method:
For the stack, is automatically managed by the compiler, without our manual control, for the heap, the release of the work programmer control, easy to produce memory Leak.
Application Size:
Stack: under Windows, the stack is the data structure to the low address extension, which is a contiguous area of memory. This sentence means that the top of the stack of the address and the maximum capacity of the stack is the system pre-defined, under Windows, the size of the stack is 2M (also said 1M, in short, the compiler determines a constant), if the application of space beyond the stack of space, it Overflow Therefore, the space to get the stack is small.
Heap: A heap is a data structure that extends to a high address, and is a discontinuous area of memory. This is because the system is stored with a linked list of free memory address, is naturally discontinuous, and the chain of the list of traversal direction is from the low address to high address. Heap laughter is limited by the amount of virtual memory available in the computer system. Thus, the space of the heap is more flexible and relatively large.
Problem with fragmentation:
For the heap, frequent new/delete is bound to cause memory space discontinuity, resulting in a large number of fragments, so that program efficiency is reduced. For the stack, there is no problem, because the stack is advanced out of the queue, they are so one by one correspondence, so that there will never be a memory quickly ejected from the stack.
allocation Method :
The heap is dynamically allocated and there are no statically allocated heaps. Stacks are allocated in two ways: static allocation and dynamic allocation. Static allocations are done by the compiler, such as the allocation of local variables. The dynamic allocation is assigned by the Alloc function, but the dynamic allocation of the stack is different from the heap, and his dynamic allocation is freed by the compiler without our manual implementation.
Allocation efficiency:
Stack is the data structure provided by the machine system, the computer will provide support on the underlying stack, allocate the address of the special register storage stack, and the stack stack has special instruction execution, which determines the efficiency of the stack is high. The heap is provided by the C + + function library, and his mechanism is very complex.
IOS--------understanding of heap and stack storage space