Sungju's Slow Life

Personal journal


Why free() does not return allocated physical memory to the OS?

If your application have to fight with huge amount of memory resources, you will find this unexpected result sometimes.

Even though you free()ed every memory into the Kernel, your process still holding that memory when you check with vmstat or free command.

It can be act differently among different Linux distributions and different library set. But, normally you can see this.

It’s happen because of optimization inside malloc() library. They usually does not return memory because it is possible to use this areas again. So, malloc library keept it for later use. But, if you just use those memory once and never use that again, it will be waste your memory and sometimes reduce the performance.

You can reclaim to actually freeing the memory by calling malloc_trim() API.



Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: