IOS's understanding of heap memory and stack and Analysis of instruments memory Phenomena

Source: Internet
Author: User
Currently, the developed applications use the arc technology, but the memory is still very large. With the instruments used to detect the memory, the memory of the program is always more than 1 MB at the beginning, this is not the case. On the contrary, we find that when you keep running your program cyclically, after a certain period of time, it will stabilize around a certain value. Why? I wonder if I want to release all the view controllers and it will return to the first 1.0 MB?
Then I constantly search for posts to see if there is any relevant knowledge. I found that the computer memory used by applications in IOS is not allocated in a unified manner, the space used to run the code is divided into three segments in three different memory areas: "Text Segment", "Stack segment", and "heap segment ".

Objective-C objects are allocated in heap mode in the memory, and the heap memory is released by you, that is, release.

The stack is automatically released by the compiler. The variables defined in the method (function body) are usually in the stack, therefore, if your variables need to be cross-function, you need to define them as member variables.

1. STACK: the stack is automatically allocated and released by the compiler, storing the parameter values of the function and the equivalence of local variables. The operation method is similar to the stack in the data structure.

2. Heap: Generally, it is allocated and released by the programmer. If the programmer does not release the heap, memory leakage may occur. Note heap is different from the stack in the data structure. Its Class is similar to the linked list.

The computer memory used by applications in IOS is not uniformly allocated. the space used for running code is divided into three segments in three different memory regions: "Text Segment", "stack segment" and "heap segment ".

Text Segment is the memory segment of the application code when the application is running. Each instruction, every single function, process, method, and Execution code exists in this memory segment until the application exits. Generally, you don't have to know anything about this section.

When the application starts, the main () function is called, and some space is allocated to the "stack. This is the memory space of another segment allocated to the application, which is allocated for the purpose of storing function variables. Each time a function is called in an application, a part of the "stack" is allocated in the "stack", which is called "frame ". The local variables of the new function are allocated here.

As shown in the name, "stack" is a post-import, first-out (LIFO) structure. When a function calls other functions, "stack frame" is created. When other functions exit, this "frame" is automatically destroyed.

The "heap" section is also called the "data" section. It provides a process for saving the intermediary throughout the function execution. Global and static variables are stored in the "heap" until the application exits.

To access the data you have created in the heap, you must have at least one pointer stored in the stack, because your CPU accesses the data in the heap through the pointer in the stack.

You can think that a pointer in the stack is only an integer variable, saving the data of the specific memory address in heap. In fact, it is a little complicated, but this is its basic structure.

In short, the operating system uses the pointer value in the stack segment to access objects in the heap segment. If the pointer of the stack object does not exist, the object in heap cannot be accessed. This is also the cause of Memory leakage.

You can create data objects in both the stack and heap segments of the IOS operating system.

Stack objects have two main advantages: Fast creation and simple management, and strict lifecycle. The disadvantage of stack objects is that they are not flexible. The length of a function is always large. The owner of a function is always the owner of the function. Unlike a heap object that has multiple owners, multiple owners are equivalent to reference count. Only the heap object uses the reference count method to manage it.

Create a stack object

As long as the remaining space on the stack is larger than the space applied to be created by the stack object, the operating system will provide the program with this memory space. Otherwise, an exception will be reported, prompting stack overflow.

Create a heap object

The operating system uses a linked list to manage heap segments in the memory. The operating system has a linked list that records idle memory addresses. When receiving a program application, it will traverse the chain table to find the first heap node with a larger space than the applied, delete the node from the idle node linked list and allocate the space of the node to the program.

Summary: in short, the memory that is stable in a range is the memory allocated by each class and is released until the end of the program.

Related Article

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.