Get the App
SLTechnology News&Howtos  ›  Servers  › 

How the Linux user process frees memory

Shulou Source: shulou.com Published: 2022-06-01 18:33:51 11月16日 Update

This article mainly explains "how Linux user process releases memory". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "how Linux user process releases memory" together.

Linux user processes are how to free memory

The basic flow of Linux processes using memory:

As can be seen from the figure, the heap of the process is not directly based on the memory allocation policy of the Linux kernel, but is based on the heap management policy of glibc (that is, glibc's dynamic memory allocation policy). The heap management is carried out by glibc. So when we call free to free the memory that malloc gets, we don't release it directly to the operating system, but return it to the heap management entity of glibc, which optimizes the policy of returning the actual physical memory to the system in order to optimize the dynamic memory allocation process for user tasks.

So when does glibc's heap manager return physical memory to the system?

It calculates how much free heap memory the user task currently has (until it hits an active heap memory address), starting with the largest linear address of the heap and working backwards, for example in this figure:

It assumes that there is 2048k of memory available for release, and it returns it to the system only if this value is greater than a certain threshold (64k on 2.3.6). The "unused" memory in the middle is not returned to the system, so it is impossible for the system to reuse this physical memory page (we assume that the system has no swap area and swap file), that is, the system memory will be reduced for this reason, unless the heap memory before it is released with free, and glibc's heap manager will be able to return this memory to the system (only possible).

Therefore, we should be careful when using malloc/free, especially if a lot of memory is allocated at initialization, but no more memory is needed after that, and the memory does not reach the threshhold value, or there is a block of memory at the highest linear address of the heap that is not released, but all the heap memory in front of it is released; in this case, the user task will waste some physical memory, which is intolerable in a resource-constrained embedded system.

Thank you for reading, the above is "Linux user process is how to release memory" content, after the study of this article, I believe that everyone on Linux user process is how to release memory this problem has a deeper understanding, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Memory system user process management physics policy task address allocation learning that is content dynamic situation time is linear but in the graph Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Docker vpn Linux MySQL