Re: Memory cleanup



I have a gtk  application that serves as a GUI for a data handling
utility we use in house. It converts data format from one type to
another and in doing so it creates a huge list with all the elements,
sorts it in a specified order and spits it out to a file. The utility
runs from the command line but I addapted it to run from the GTK gui. In
short, a click on a button calls the function that runs the sorting and
stuff. I monitor the utility with top to keep an eye on memory use and
the first time the function is called it's memory usage climbs to 90
megs, which is correct considering the size of this database, however I
use the free() call to remove allocated memory at the end of the
function when control returns to gtk_main() but top still shows 90M of
ram in use by th program. Correct me if I'm wrong (and I probably am)
but shouldn't all that memory be destroyed/reclaimed/made free by the
kernel after I exit those calls? BTW, the variables are declared local
to the function not global to the program.

The answer to your question is: not necessarily.

AFAIK, on most Unices application memory can't be returned to the kernel except
under certain limited conditions (you can only move the top-of-memory pointer
up and down with the brk() or sbrk() system call). It's sufficiently hard that
glibc doesn't bother.

However, there's another way to allocate memory, by mmap'ing private anonymous
regions. When these are freed, the memory *is* returned to the kernel. glibc
uses mmap to allocate memory blocks above a certain size; I don't know exactly
what.

So, leaving memory leaks aside (the usual cause of leaked memory), your 90MB
will only be returned to the kernel if it's composed of memory blocks larger
than the critical size (assuming you're using glibc, of course; other libc's
may do something completely different).

If you run your memory-hungry routine again, does it eat more memory or reuse
the old memory?

-- 
+- David Given --------McQ-+ "There is no expedient to which a man will not
|  Work: dg tao-group com  | resort to avoid the real labour of thinking." ---
|  Play: dg cowlark com    | Thomas Edison                                      
+- http://www.cowlark.com -+ 





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]