Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-03-2011, 06:56 AM   #1
Registered: Dec 2008
Distribution: Ubuntu Fedora
Posts: 445

Rep: Reputation: 18
how to free memory immediately after calling free in C programming

how can I free allocated memory with malloc immediately because in my program when I free allocated memory as I see in top or htop or ps out put it memory usage does not change and as I understood programs does not free memory immediately after calling free how can I change this behavior ?

Thanks for any help.
Old 12-03-2011, 07:46 AM   #2
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Buster (Fluxbox WM)
Posts: 1,390
Blog Entries: 52

Rep: Reputation: 359Reputation: 359Reputation: 359Reputation: 359
In the C language, free happens immediately, and the space is given back to the C heap (unlike a garbage collected language, where the space is not reclaimed until the next collection pass).

However, the usual implementation of the heap itself is such that if it runs out of space, it is increased in size. It never decreases in size, even if used space is given back to the heap. It is done this way because it is an efficient implementation.

This is not typically a problem (ignoring pathological fragmentation), because the areas no longer being used will eventually be paged out of real memory.

But it does mean that you cannot use programs like top to monitor what is happening, because they cannot tell that part of the C heap is no longer being used, and will continue to show the current total memory space of the process (including both used and unused parts of the C heap).

Last edited by neonsignal; 12-03-2011 at 07:59 AM.
Old 12-03-2011, 08:09 AM   #3
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Originally Posted by golden_boy615 View Post
how can I free allocated memory with malloc immediately
To free more memory immediately, you could write your own version of malloc. Even then, without significant waste during allocation, you couldn't return each chunk immediately when freed. Malloc must get and release whole 4096 byte pages of memory. But your program gets and releases chunks that are not page aligned.

Why do you want to free allocated memory immediately? In most programs there would be very little benefit (and large cost) to doing so.

You might have a program that allocates a lot of memory early, then finishes with that memory, then continues to run for a long time using little memory. That produces an unnecessarily load on the system memory until Linux figures out the stale pages should be swapped out, then there is the cost of that swapping, then additional cost when the process shuts down. That still doesn't add up to enough to justify a big programming effort unless there is extra reason to really care about the system impact of that program.

In the unlikely case that it is worth programming effort, you could use a pool allocation that you manage yourself for the large memory use that will be returned early. If you get a big enough chunk from malloc, then malloc will usually allocate it in a different way and release it immediately when your program frees it. You may need to look at malloc source code (and/or experiment) to see what that size is for your version of malloc. Alternately, you can get any page aligned multiple of the page size directly from the kernel and return it directly to the kernel bypassing malloc.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Free Knowledge requires Free Software and Free File Formats LXer Syndicated Linux News 0 06-26-2011 08:22 AM
Like to understand the linux "free" command with respect to cache and free memory bruzzese Linux - Server 2 03-28-2010 01:52 PM
[SOLVED] Free-ing memory problem. C/C++, free() sevs Programming 6 03-13-2010 01:31 PM
LXer: Free Me: a DVD about free culture and free software LXer Syndicated Linux News 0 03-12-2007 01:01 PM
Does free( ) immediatly free the memory? Rayven Programming 24 11-06-2006 03:24 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:50 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration