IOS: Understanding of Heap and stack (stack)

Source: Internet
Author: User

The Object-c object allocates memory space in memory as a heap, and the heap memory is released by you, that is, release

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.

Creation of a Stack object

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.

The creation of the heap object

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, and the 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"]; ”。


IOS: Understanding of Heap and stack (stack)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.