[PyQt] Is PyQt really freeing memory?

Giovanni Bajo rasky at develer.com
Mon Aug 3 01:25:16 BST 2009

On dom, 2009-08-02 at 02:12 +0200, Albert Cervera i Areny wrote:
> I've been having problems with my application consuming too much memory after 
> some time running and today decided to take a deeper look. I've ended up with 
> the attach test.py script which either demonstrates PyQt is not freeing memory 
> appropiately when signals are involved or I simply don't understand how this 
> works.
> As you can see the script creates lists with 100.000 QObjects and prints the 
> memory used. As python won't free memory but reuse what has already been freed 
> I expect a call like:
> list = []
> fill in the list with lots of data
> to take as much memory as:
> list = []
> fill in the list with lots of data
> list = []
> fill in the list with the same lots of data
> If you give it a try, you'll realize that this is true if you create 100.000 
> objects with no signal connections. But if you connect and disconnect  a 
> signal for those objects, the memory used after the second fill is larger than 
> after the first one.
> It seems to me that some data is being leaked in connect() and disconnect() 
> functions (which, by the way, take up a lot of memory).
> Here's the output of the script in my system:
> $ python test.py one no
> Executing 'one' without signals
> Memory:  21564
> $ python test.py one yes
> Executing 'one' with signals
> Memory:  64992
> $ python test.py two yes
> Executing 'two' with signals
> Memory:  125592
> $ python test.py two+remove yes
> Executing 'two+remove' with signals
> Memory:  93880
> $ python test.py three+remove yes
> Executing 'three+remove' with signals
> Memory:  122808
> So "two+remove yes" should be using 64992 Kb (just like "one yes") but it uses 
> 50% more. The same happens with "three+remove yes", again more 30 Mb!

Looks like a genuine memory leak to me. I tried stuffing gc.collect() in
there (after you free the object list) but, as expected, it does not
make a difference.
Giovanni Bajo
Develer S.r.l.

More information about the PyQt mailing list