Ok first of all I'd like you guys to know this is like my first thread ever so go easy on me if I'm not doing something right :P

Right then , so I've been programming in python for some time now and I love pretty much everything about this language , but there has always been one thing that was bothering me since day one , and that's the way python manages memory

The main problem here is that literally every single one of my bigger apps (ones written with wxPython GUI for instance) have memory issues (memory leaks?).

So take my last program for instance, it's supposed to capture screen-shots and let you decide what you want to do with the captured bitmap (send it to clipboard, send it to other program or simply save it), and the memory buildup for this program is HUGE

It almost seems like memory of every bitmap I capture never gets released, and just keeps adding up.

I have NO clue how deal with this, I tried searching for topics on this matter but found nothing useful and all the tutorials I read mention nothing about how python manages memory

Since deleting objects doesn't seem to do much, my question is what does :| ?

Isn't there a C++'s free() equivalent in python, or something along those lines ?

I'm really clueless about this matter so I would really appreciate it if some one could just point me in the right direction :\

Recommended Answers

All 3 Replies

There is no memory leak in python. Python uses a generational garbage collector which deletes objects when there is no reference left to these objects. You can have look at the gc module which gives you a few functions to interact with the garbage collector, but normally you don't have to.

Of course, it's the programmer's responsibility to ensure that there is no reference to the object. If a living object A has a member B which is a dictionary which contains a value D one of which member is your bitmap object, then the bitmap will not be freed ! The role of the garbage collector is to solve cyclic references.

You should avoid defining __del__ methods for your objects (see gc.garbage).

Every python object stores an integer which is a count of the existing references to the object. An object with a positive refcount will not be freed by the garbage collector. All the objects you can access from your program have a positive refcount (so that you will never see an object with a refcount of 0). However, there is a method sys.getrefcount() which can help you detect hidden references to your object.

I've heard of a special case of 'memory leak' in python: when you load an extension module by importing a shared library, as far as I know, there is no way to 'unload' the library.

Are you creating a list of image objects somewhere?

There is no memory leak in python. Python uses a generational garbage collector which deletes objects when there is no reference left to these objects. You can have look at the gc module which gives you a few functions to interact with the garbage collector, but normally you don't have to.

Of course, it's the programmer's responsibility to ensure that there is no reference to the object. If a living object A has a member B which is a dictionary which contains a value D one of which member is your bitmap object, then the bitmap will not be freed ! The role of the garbage collector is to solve cyclic references.

You should avoid defining __del__ methods for your objects (see gc.garbage).

Every python object stores an integer which is a count of the existing references to the object. An object with a positive refcount will not be freed by the garbage collector. All the objects you can access from your program have a positive refcount (so that you will never see an object with a refcount of 0). However, there is a method sys.getrefcount() which can help you detect hidden references to your object.

I've heard of a special case of 'memory leak' in python: when you load an extension module by importing a shared library, as far as I know, there is no way to 'unload' the library.

Thank you, I think this pretty much covers everything I needed to know : )

(should have visited this forum earlier : D )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.