The basic process of using memory for Linux processes:
As can be seen from the figure, the process heap is not a memory allocation strategy directly based on the Linux kernel, but a heap management strategy based on GLBC (that is, the dynamic memory allocation strategy of GLBC), and the heap management is carried out by GLBC. Therefore, when we call free to release the memory acquired by malloc, it is not directly released to the operating system, but also given to the heap management entity of glibc. In order to optimize the dynamic memory allocation process of user tasks, glibc will optimize the strategy of returning actual physical memory to the system.
It will calculate how much free heap memory the user task currently has (until it touches the heap memory address in use) from the maximum linear address of the heap, for example, in the following figure:
It will think that there is 2048k free memory, and only when this value is greater than a certain threshold hold (64k on 2.3.6) will it return the memory to the system. Unused memory in the middle will not be 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), which means that the memory of the system will be reduced. Unless the previous heap memory is released, the heap manager of glibc can (only possibly) return this memory to the system.