Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How the Linux user process frees memory

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 213

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report