Memory leaks / Help with memprof & ps output



Jason Tackaberry writes:
 > Why do I get the feeling this is a FAQ ...  well, I did some archive
 > searches and didn't get any satisfactory answers, so here goes ...

It is, but not a GTK FAQ.

 > I am writing a program (using gtk 2.0) that needs to deal with loading a
 > large number of images into memory (using gdk_pixbuf calls).  Everything
 > works fine, except after calling g_object_unref on the pixbufs, it seems
 > that the memory isn't getting released to the operating system.  

Remember, you're looking at the virtual space, not (necessarily) the
physical memory.  Unix-related OSes regard virtual space as
essentially free:  once a process has used to malloc to get some
virtual space from the OS, it doesn't ever give it back (until the
process dies, anyway).

Remember processes maintain their own free space pools (through
malloc/free).  When it calls malloc, an attempt is made to satisfy the
request through the local free space pool; it's only if that fails
that the process requests more space from the OS.  When you call free,
it just gives the space back to its local pool; it doesn't return it
to the OS.  Trying to do so would make memory management a bigger mess
than it already is (I invite you to read the code for malloc...);
you'd have to notice when a whole page was freed, return it, and then
keep track of the hole in the process's space to keep from getting seg
faults.  And it very rarely buys you anything anyway, very few
processes allocate a bunch of space, free it all, run a while longer,
and go away.  They either free it all and then die, or end up
requesting it again later.  Leaving it in the process's pool lets
malloc go faster later.

Meanwhile, it doesn't actually cost a significant amount of physical
memory.  If you have some space you haven't used for a while (which
had better be the case if you've freed it!), it migrates out to disk
as other processes fault.
-- 
Joseph J. Pfeiffer, Jr., Ph.D.       Phone -- (505) 646-1605
Department of Computer Science       FAX   -- (505) 646-1002
New Mexico State University          http://www.cs.nmsu.edu/~pfeiffer
Southwestern NM Regional Science and Engr Fair:  http://www.nmsu.edu/~scifair



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