LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to free memory immediately after calling free in C programming (https://www.linuxquestions.org/questions/programming-9/how-to-free-memory-immediately-after-calling-free-in-c-programming-916896/)

golden_boy615 12-03-2011 05:56 AM

how to free memory immediately after calling free in C programming
 
hello
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.

neonsignal 12-03-2011 06:46 AM

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).

johnsfine 12-03-2011 07:09 AM

Quote:

Originally Posted by golden_boy615 (Post 4540906)
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.


All times are GMT -5. The time now is 07:56 PM.