IOS: heap and stack

Source: Internet
Author: User

IOS: heap and stack

Objects in Object-c are allocated with memory space in heap mode, and the heap memory is released by you, that is, release.

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.

For example:

The NSString object is the object in the stack, and the NSMutableString object is the object in the heap. The former has fixed and unchangeable Memory Length allocated during creation, and the latter has variable Memory Length allocated. It can have multiple owners and is suitable for the memory management mode of counting management.

The two types of objects have different creation methods. The former directly creates "NSString * str1 = @" welcome ";", the latter needs to allocate and then initialize "NSMutableString * mstr1 = [[NSMutableString alloc] initWithString: @" welcome "];".


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.