Memory leak in Cone.dual()

30 views
Skip to first unread message

pete.d...@port.ac.uk

unread,
Apr 30, 2014, 10:19:12 AM4/30/14
to sage-s...@googlegroups.com
Hi all,

I'm pretty new to Python, so perhaps I'm doing something wrong, but I've encountered what I believe to be a memory leak in Sage's Cone.dual() method. Below is some very simplified proof of concept code demonstrating the issue:

#!/usr/bin/env sage
generators = []
generators.append( [1,0,0,0] )
generators.append( [0,1,0,0] )
generators.append( [0,0,1,0] )
generators.append( [0,0,0,1] )
for i in range( 0, 1000 ):
cone = Cone( generators )
dual = cone.dual()
print( 'Current memory usage: ' + str( get_memory_usage() ) )


Memory usage steadily increases with each iteration of the for loop. Commenting out the line dual = cone.dual(), or moving the line cone = Cone( generators ) before the for loop keeps memory usage constant. I'm currently using Sage 6.0 on Ubuntu 13.10 32-bit, but have seen the same behaviour in Sage 6.1.1 on Ubuntu 64-bit.

Can anyone shed any light on this? Is it a genuine bug, or am I doing something unwise due to not understanding Python's garbage collection? Can anyone suggest a workaround to force cone and dual to be freed on each iteration of the for loop? I've tried using del cone and cone = None at the end of the loop, but neither helped.

Thanks in advance...
Pete

Volker Braun

unread,
Apr 30, 2014, 11:45:25 AM4/30/14
to sage-s...@googlegroups.com
you can force a garbage collection with

import gc
gc.collect()

But your script doesn't increase memory on Sage-6.2.rc0 even without that.

Nils Bruin

unread,
Apr 30, 2014, 11:47:03 AM4/30/14
to sage-s...@googlegroups.com
On Wednesday, April 30, 2014 7:19:12 AM UTC-7, pete.d...@port.ac.uk wrote:
Hi all,

I'm pretty new to Python, so perhaps I'm doing something wrong, but I've encountered what I believe to be a memory leak in Sage's Cone.dual() method. Below is some very simplified proof of concept code demonstrating the issue:

#!/usr/bin/env sage
generators = []
generators.append( [1,0,0,0] )
generators.append( [0,1,0,0] )
generators.append( [0,0,1,0] )
generators.append( [0,0,0,1] )
for i in range( 0, 1000 ):
cone = Cone( generators )
dual = cone.dual()
print( 'Current memory usage: ' + str( get_memory_usage() ) )


Memory usage steadily increases with each iteration of the for loop. Commenting out the line dual = cone.dual(), or moving the line cone = Cone( generators ) before the for loop keeps memory usage constant. I'm currently using Sage 6.0 on Ubuntu 13.10 32-bit, but have seen the same behaviour in Sage 6.1.1 on Ubuntu 64-bit.

This code:

 generators = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
import gc
from collections import Counter

cone = Cone( generators )
dual = cone.dual()


gc.collect()
pre={id(p) for p in gc.get_objects()}

for i in range( 0, 2000 ):

    cone = Cone( generators )
    dual = cone.dual()

gc.collect()
gc.collect()
gc.collect()

T=Counter([str(type(p)) for p in gc.get_objects() if id(p) not in pre])
[(t,c) for t,c in T.iteritems() if c > 10]

confirms a memory leak in, e.g.,  6.0 (you see all kinds of objects pile up in memory), but in 6.2.rc0 there is not, and I see flat memory usage. So it seems the leak is fixed in a future version.

pete.d...@port.ac.uk

unread,
May 4, 2014, 5:29:03 PM5/4/14
to sage-s...@googlegroups.com
Thanks guys. Manual garbage collection helps, but memory usage still creeps up. Looks like I need to build sage 6.2 from source!
Reply all
Reply to author
Forward
0 new messages