Ask Your Question

Memory usage strictly increasing on Sage interactive shell

asked 2019-03-21 22:57:45 +0200

jb gravatar image

updated 2023-01-09 23:59:50 +0200

tmonteil gravatar image


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 flag offensive close merge delete



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))
    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.

rburing gravatar imagerburing ( 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.

jb gravatar imagejb ( 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.

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

1 Answer

Sort by ยป oldest newest most voted

answered 2019-03-22 20:09:13 +0200

rburing gravatar image

I reported this memory leak as trac ticket #27536.

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

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-03-21 22:57:45 +0200

Seen: 988 times

Last updated: Mar 22 '19