sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.48k stars 488 forks source link

Memory leak for letterplace implementation of free algebras #17494

Open simon-king-jena opened 9 years ago

simon-king-jena commented 9 years ago

At #17435, I noticed the following:

sage: L.<x,y,z> = FreeAlgebra(GF(25,'t'), implementation='letterplace')
sage: p = x^4+x*y*x*z+2*z^2*x*y
sage: for i in range(20):
....:     m = get_memory_usage()
....:     for j in range(300):
....:         z = p^7
....:     print get_memory_usage()-m
....: 
2.0
2.0
2.0
2.0
0.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
0.0
2.0
2.0

This leak even pertains when #16958 is applied.

Upstream: Fixed upstream, in a later stable release.

CC: @malb

Component: memleak

Issue created by migration from https://trac.sagemath.org/ticket/17494

simon-king-jena commented 9 years ago
comment:1

Here is the corresponding computation in Singular (as shipped with Sage):

./sage -singular
                     SINGULAR                                 /  Development
 A Computer Algebra System for Polynomial Computations       /   version 3-1-7
                                                           0<
 by: W. Decker, G.-M. Greuel, G. Pfister, H. Schoenemann     \   Aug 2013
FB Mathematik der Universitaet, D-67653 Kaiserslautern        \
> LIB "freegb.lib";
// ...
> ring r = 0,(x,y,z),dp;
> def R = makeLetterplaceRing(30);
> setring R;
> poly p = x(1)*x(2)*x(3)*x(4) + x(1)*y(2)*x(3)*z(4) + 2*z(1)*z(2)*x(3)*y(4);
> poly q = lpPower(p,7);
> memory(0); memory(1);  memory(2);
3494312
8634368
8667136
> q = lpPower(p,7);
> memory(0); memory(1);  memory(2);
5393504
10731520
10764288
> q = lpPower(p,7);
> memory(0); memory(1);  memory(2);
7292696
12828672
12861440
> q = lpPower(p,7);
> memory(0); memory(1);  memory(2);
9191888
14925824
14958592
> Auf Wiedersehen.

So, the leak is in Singular, not in the wrapper.

simon-king-jena commented 9 years ago

Changed upstream from Not yet reported upstream; Will do shortly. to Reported upstream. Developers acknowledge bug.

simon-king-jena commented 9 years ago

Changed upstream from Reported upstream. Developers acknowledge bug. to Fixed upstream, in a later stable release.

simon-king-jena commented 9 years ago
comment:2

If I understand correctly, the leak is fixed in a later version of singular. Hope it will be in Sage soon...

rwst commented 9 years ago
comment:3

Maybe related: http://ask.sagemath.org/question/29444/high-memory-usage-when-substituting-variables/

nbruin commented 9 years ago
comment:4

Replying to @rwst:

Maybe related: http://ask.sagemath.org/question/29444/high-memory-usage-when-substituting-variables/

I think that points to a more basic and hence more severe memory leak.

import gc, collections
gc.collect()
old=set( id(a) for a in gc.get_objects())
R.<x,y,z>=ZZ[]
f=12^2*(x^4+y^4)-15^2*(x^2+y^2)*z^2+350*x^2*y^2+9^2*z^4
pt=[1,2,3]
m=get_memory_usage()
for i in xrange(10^5):
    temp=f(x=x+pt[0]*z,y=y+pt[1]*z,z=pt[2]*z) #This also uses a lot of memory.
    if (i% 100) == 0:
        print get_memory_usage()-m

gc.collect()
new=collections.Counter( str(type(a)) for a in gc.get_objects() if id(a) not in old)
new

This code clearly shows leaking and it shows the leak is not in python reference counted objects. I would find it hard to believe that such a basic leak in normal polynomial arithmetic would go unnoticed in Singular, so I would expect the error is somewhere in the sage/singular interface.