0

Im actually trying to fix my memory problems within my python gtk app.

Ive read many articles about memory usage in python but I cant get it all, so I hope you can help me here.

So if I start my application and open the settings window, I have am mem usage of 32368 KB.

Now if i close the settings window, the mem usage is 32368 KB, too.

here is a code example how i open and close the settings window:

main.py

...
    def show_settings(self):
        self.settings = Settings()
        self.settings.window.connect("destroy", self.clear_settings)

    def clear_settings(self, widget, Data=None):
        del self.settings


class Settings():
    def __init__():
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        ...

Now ive tried to analyze alive references with objgraph. Thats the result:

Open Settings Window

Open Settings Window

After Closing Settings Window

After closing settings

Can someone help how to free the mem?

ali_m
  • 71,714
  • 23
  • 223
  • 298
HappyHacking
  • 878
  • 1
  • 12
  • 28

2 Answers2

2

Memory allocation in Python occurs at multiple levels. First, memory is allocated from the system by the system allocator component of the C library (this is what will show up in memory usage figures given by Unix ps or Windows task manager). That allocated memory is then allocated in smaller chunks by C library calls like malloc, which is in turn, allocated if even smaller chunks by Python's own memory allocator (for storing objects). Furthermore, Python maintains different allocation pools for different objects (to optimise for certain objects, e.g. str vs int).

For memory to be returned to the system, it has to unallocated by Python's allocator, then by the C library's heap allocator and page allocator. When this happens is hard to determine as it depends of many factors, such the system you're running on, the version of the C library, Python's own version and your application's allocation pattern.

Allocators normally keep some free memory (known as free lists) in order to speed up allocations by avoiding to make system calls (which are expensive) every time new memory is required. Each allocator will have some thresholds which determine when memory is actually allocated from or release to the next level.

So you simply cannot expect to see the system memory allocation (i.e. ps / task manager output) change by deleting an object.

If you have specific memory allocation issues, some as huge temporary objects, and you want to minise your memory usage over time, then you'll need to ask a more specific question :)

isedev
  • 18,848
  • 3
  • 60
  • 59
0

I see nothing wrong here. Your Settings instance has disappeared after being freed, which is normal. How are you measuring the memory usage ?

liberforce
  • 11,189
  • 37
  • 48
  • every time i open the settings window, ~18kB new memmory is alocated (measured with ps auxww). But its never release. now if you do this several times, the mem usage increases constantly – HappyHacking Jan 29 '13 at 11:59
  • Measuring correctly memory usage is hard, specially when libraries are involved. I for a "big picture" measure, I use `ps -eo rss,cmd --sort=rss` to see at the bottom the applications that use most [RSS](http://en.wikipedia.org/wiki/Resident_set_size) memory. But with caches, special [SLAB](http://en.wikipedia.org/wiki/Slab_allocation) memory allocators and all other memory management stuff, I agree with isedev that you can't take for granted that freeing some memory on your side should have an instant impact on memory consumption. – liberforce Jan 29 '13 at 12:33
  • BTW, are you using Windows, Linux, something else? – liberforce Jan 29 '13 at 12:34
  • So no special issue. On Windows, there were some memory leaks until recent GTK 2.24.14. – liberforce Jan 29 '13 at 14:11