Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 08-25-2009, 01:55 AM   #1
LQ Newbie
Registered: Jul 2009
Posts: 20

Rep: Reputation: 0
releasing memory allocated to a process


i'm trying to release the memory allocated to the process. i have used malloc to allocate memory.

i'm using free() and malloc_trim(0) but the memory is not released back to the OS.

is there a way to release back the allocated memory

Old 08-25-2009, 06:56 AM   #2
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
I never tried malloc_trim, so I don't know how well it performs its documented function.

Usually you don't worry about giving the memory back prior to the process terminating. Once the process terminates, the memory is released whether you freed it or not.

If the chunks of memory you freed were mixed with chunks of memory you didn't free, then malloc_trim cannot release the freed chunks back to the OS. Memory is allocated from the OS to malloc as large ranges of contiguous 4KB pages. Malloc subdivides that memory to fill the requests from you program. Normally malloc only returns memory to the OS when one of those contiguous ranges of 4KB pages is completely freed. I think it could return the 4KB pages at the end of such a range while keeping the ones at the beginning (assuming all the remaining used chunks are at the beginning) but I don't know if it does so.

Why do you want the memory released prior to process termination?

If you want more control over when the memory is released back to the OS, you may need to manage the original allocation from the OS yourself as well. You can allocate a multiple of 4KB from the OS the same way malloc does and if necessary, you can subdivide that into the chunks your program wants to use.

You can also get the source code to malloc and see its rules for what can be returned by malloc_trim and you might change the code to fit your needs better.


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
[SOLVED] Memory leak: How risky not to free allocated memory. kaz2100 Linux - General 1 12-24-2008 12:00 AM
releasing memory DotHQ Linux - Server 2 11-28-2006 09:34 AM
About allocated memory in C++ Ephracis Programming 2 12-28-2004 02:56 AM
updatedb not releasing memory? Mephisto Slackware 2 07-18-2004 11:37 PM
new kernel not releasing memory plisken Linux - General 1 10-22-2003 06:22 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:58 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration