# Memory usage strictly increasing on Sage interactive shell

Hello,

I executed the following script on the Sage interactive shell on Ubuntu 18.04 (on SageMath version 8.7.beta5):

sage: import gc
sage: t = 100000
sage: l = [n for n in xrange(t,t+100)]
sage: ll = []
sage: for x in l:
....:     s = 0
....:     for n in xrange(0,x):
....:         s += cos(RDF(2)*RDF(pi)*RDF(n)/RDF(x))
....:     ll.append([x, s])
....:     del(s)
....:     print "memory usage: " + str(get_memory_usage()) + ", gc: " + str(gc.collect())
....:
memory usage: 5824.5546875, gc: 277
memory usage: 5828.3046875, gc: 0
memory usage: 5832.0546875, gc: 0
memory usage: 5836.0546875, gc: 0
:
memory usage: 6203.5546875, gc: 0
memory usage: 6207.5546875, gc: 0


As we can see the memory usages, it increases by about 4 MB per each step. Thus, after completed, it increased around 400 MB in total. It is frustrating since it eventually eats up all memories in my system as the size of the input list grows and gc.collect() does not seem to help.

I suspect that there were memory leaks or Sage(or Python) stored all the values of cos() and never freed them for some reasons while the shell not killed.

Can someone please explain what was going on and suggest how to avoid this unwanted memory consumption? Thank you in advance.

edit retag close merge delete

1

I think the problem is not with cos but rather with pi:

import gc
for x in xrange(10^5,10^5+100):
s = sum(RDF(pi) for n in xrange(x))
del(s)
print "memory usage: " + str(get_memory_usage()) + ", gc: " + str(gc.collect())


The memory leak goes away when one replaces RDF(pi) by e.g. RDF(3.14) or RDF.pi(). Something must be going wrong when calling RDF(pi). The same memory leak occurs when replacing the pi in RDF(pi) with any of the other mathematical constants.

( 2019-03-22 13:14:16 +0200 )edit

You're absolutely right! Thank you very much for your comment. If I use pari(pi), it works without memory leaks. However, if gp(pi) used, it just hangs. I still want to know how the leaks happen; I guess that this issue is related with gp('znprimroot(10007)') and pari('znprimroot(10007)') shown on Interfaces--Sage Tutorial v86. By the way, if you put it in Answer, I will mark yours as an answer. Best, jb.

( 2019-03-22 19:40:02 +0200 )edit

If you wait long enough (or run it on a faster computer) then it will work without leaks with gp(pi) as well. I will report the memory leak and post that as an answer.

( 2019-03-22 20:06:32 +0200 )edit

Sort by ยป oldest newest most voted

I reported this memory leak as trac ticket #27536.

A workaround is to replace RDF(pi) by RDF.pi().

more