Re: Memory management in gtk



On 19 October 2010 06:22, Vishak V. Kurup <vishak kurup nestgroup net> wrote:
I am having a small problem related to GTK. My application is having
around 15 windows.  When I start my application and check the memory
usage using "top" utility it shows around 19MB. When a new window is
created it may increase upto 22 MB (varies each time). And after I
destroy the new window my memory is not getting reduced. Could anyone
please tell me why memory usage is not decreased even after I destroy
widget.

top is not the best way to see memory use of a program, or to check for leaks.

When a program starts, the system hands over a large chunk of ram to
the memory library. This library then hands out the memory to the
program in small bits as the program asks for it. If the chunk is
exhausted, the library asks for more memory from the OS. When memory
is freed, the memory library does not return it to the host operating
system (with certain strange exceptions), instead it keeps it around
for reuse. As a result, you will (almost) never see the memuse fall,
as reported by top, for any program.

To check for leaks, you need to use a special tool. In my opinion, the
most useful under Linux is valgrind. Run it with something like:

export G_DEBUG=gc-friendly

        This makes Glib clear certain memory areas after using them, too.

export G_SLICE=always-malloc

        This completely disables the magazine and slab allocator in Glib,
        and makes it use plain malloc()/free() instead.

valgrind --leak-check=yes \
  myprog ... > myprog-vg.log 2>&1

You will get some false positives. I have a suppressions file here:

  http://www.vips.ecs.soton.ac.uk/development/nip2.supp

Which hides all the false positives from the gtk stack.

John



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