UNIX advanced environment programming (15) process and memory allocation & lt; old palace tower & gt;, old palace tower

Source: Internet
Author: User

UNIX advanced environment programming (15) process and memory allocation

Angular building is a place that many Photography enthusiasts often visit. It is calm and peaceful in the sunset.

 

First, let's take a look at the basic concepts of processes, including the layout and content of processes in the memory.

In addition, you also need to know how the runtime allocates additional memory for dynamic data structures (such as linked lists and binary trees.

One process 1 Process and program

Process: an instance of an executable program.

Program: a file containing a series of information that describes how to create a process at runtime. Contains the following information:

Process redefinition: A process is an abstract entity defined by the kernel and allocates various system resources for executing programs to the entity.

From the kernel perspective, a process consists of the user memory space and a series of kernel data structures. The user memory space contains the variables used by program code and code, the kernel data structure is used to maintain the Process status information.

2 typical process memory Layout

The memory allocated by each process is composed of many parts, which are usually called segments )". As shown in:

Separate initialized global variables and static variables from uninitialized global variables and static variables. The main reason is that when the program is stored on disk, there is no need to allocate storage space for uninitialized variables. On the contrary, the executable file only needs to record the location of the uninitialized data segment and the required size until the space is allocated by the program loader at runtime.

Note that the memory layout is discussed in virtual memory rather than in physical memory.

Some details about the virtual memory will be discussed later.

 

2. Memory Allocation 1. memory allocation on the heap

Heap: a variable-length continuous virtual memory starts at the end of the uninitialized data segment of the process and increases or decreases with memory allocation and release. The top boundary of the current memory of the heap is called program break )"

Program break is a very important concept, because the actual action of allocating and releasing memory is to change the program break position of the process.

The starting position of the program break (the heap size is 0) is located after the end of the uninitialized data segment.

Details: after a new memory is allocated, the program break position increases, and the program can access any memory address in the new allocated area. At this time, the physical memory page has not been allocated. The memory will automatically allocate new physical memory pages when the process attempts to access these virtual memory addresses for the first time.

Functions malloc and free

Malloc function declaration

#includevoid *malloc(size_t size); 

Purpose: allocate the parameter size byte memory on the heap.

Returned value: the pointer to the starting address of the newly allocated memory is returned. If the pointer fails, NULL is returned.

Free function declaration

#includevoid free(void *ptr);

Role: Release the memory block pointed to by the ptr parameter. This parameter should be the address returned by malloc or one of other memory allocation functions.

Note: In general, free does not reduce the program break position, but increases the memory to the idle memory list for subsequent use by the malloc function. Because:

  • The released memory block is usually located in the middle of the heap, rather than the top of the heap. Therefore, it is impossible to reduce the program break.
  • It minimizes the number of times that the kernel call adjusts the program break system call.
  • Generally, the program will hold the allocated memory or repeatedly release and re-allocate, instead of releasing all the memory and then running for a while.

The glibc implementation of the free function calls sbrk () to reduce the program break address only when the idle memory on the top of the heap is "large enough, as for "sufficient" and otherwise, it depends on the Control Parameter of the malloc function package behavior (KB is a typical value ). This reduces the number of calls that must be initiated to sbrk.

Implementation of malloc and free

Implementation of malloc ()

Implementation of free ()

First, understand the structure of the memory blocks returned by malloc and the memory blocks in the idle list.

To know the size of each memory block, when malloc allocates a memory block, it will allocate several additional bytes to store and record the integer of this memory size. This integer is located at the beginning of the memory block, and the memory address actually returned to the caller is exactly behind this length record byte. As shown in:

To manage the idle memory list, free () uses the space of the memory block to store the linked list pointer and adds itself to the list. As shown in:

Therefore, after memory is frequently allocated and released, the linked list in the heap may look like this. The idle memory in the idle linked list will be mixed with the allocated memory.

 

Three programming considerations

Through more knowledge about memory, we should know why we need to follow the following rules during normal programming.

 
Although the underlying principles may not be involved in our daily work, by understanding these basic principles, we can clear them and write code exactly :)

 

References:

Linux/Unix System Programming Manual (previous) Chapter 6th and Chapter 7th

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.