On 23/03/16 15:39, Nils Bruin wrote:
On Wednesday, March 23, 2016 at 9:17:58 AM UTC-7, vdelecroix wrote:

Hello,

Some friend just sent an e-mail to me mentioning a memory leak. Here is
a minimal example

sage: x = polygen(ZZ)
sage: K = NumberField(x**3 - 2, 'cbrt2', embedding=RR(1.2599))
sage: w = K.gen()
sage: import resource
sage: resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
180720
sage: for _ in range(10000): test = w > 1
sage: resource.getrusage(resource.RUSAGE_SELF).ru_maxrss


I tried python-level analysis:

import gc
from collections import Counter
gc.collect()
pre={id(c) for c in gc.get_objects()}

x = polygen(ZZ)
K = NumberField(x**3 - 2, 'cbrt2', embedding=RR(1.2599))
w = K.gen()
gc.collect()
pre={id(c) for c in gc.get_objects()}
for _ in range(20000): test = w > 1
gc.collect()
gc.collect()
post=Counter(type(o) for o in gc.get_objects() if id(o) not in pre)
sorted(post.iteritems(),key=lambda t: t[1])

C=[c for c in gc.get_objects() if id(c) not in pre and type(c) is tuple and
type(c[0]) is sage.rings.real_mpfi.RealIntervalFieldElement]

  This finds a whole bunch of one-element tuples with a realintervalfield
element. If I use objgraph_showbackrefs(C[100]) or something similar, I
only  get the references via C! So it seems somehow these objects survived
"gc.collect" without actually having objects referring to them. That would
point towards an incref/decref error somewhere.



I am not sure about the Python analysis (though it might be some other problem).

With the code (which does not involve number fields anymore)
{{{
import gc
import resource

R = PolynomialRing(ZZ, 'x', implementation='NTL')
x = R.gen()
p = x**2 - 3

gc.collect()
n0 = len(gc.get_objects())
mem0 = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss

def stats():
    global n0, mem0
    gc.collect()
    print len(gc.get_objects()) - n0,
    print resource.getrusage(resource.RUSAGE_SELF).ru_maxrss - mem0

for _ in range(10):
    stats()
    for _ in range(5000): a = p(2)
}}}

The result I got is

57 0
68 508
68 1300
63 2620
63 2620
63 3356
63 4148
58 4676
58 5468
58 6236

Hence the memory usage (right column) is not reflecting any problem from the garbage collection (left column).

An other important remark is that if we use the FLINT implementation everything is fine. In other words using

R = PolynomialRing(ZZ, 'x', implementation='FLINT')

I got

60 0
54 0
54 0
54 0
54 0
54 0
49 0
49 0
49 0
49 0

Vincent

--
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to