Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memleak in conversion for univariate polynomial rings #8444

Closed
simon-king-jena opened this issue Mar 5, 2010 · 14 comments
Closed

Memleak in conversion for univariate polynomial rings #8444

simon-king-jena opened this issue Mar 5, 2010 · 14 comments

Comments

@simon-king-jena
Copy link
Member

At sage-support, Ben Linowitz reported a problem with memory. Apparently it boils down to the following problem:

sage: R.<x> = QQ[]
sage: M = get_memory_usage()
sage: for n in range(50000):
....:     Mnew = get_memory_usage()
....:     if Mnew > M:
....:         print n, Mnew-M
....:         M=Mnew
....:     a = R(1)
....:
0 1.51171875
6673 0.12890625
8785 0.12890625
10897 0.12890625
13009 0.12890625
15121 0.12890625
17233 0.12890625
19345 0.12890625
21457 0.12890625
23569 0.12890625
25681 0.12890625
27793 0.12890625
29905 0.12890625
32017 0.12890625
34129 0.12890625
36241 0.12890625
38353 0.12890625
40465 0.12890625
42577 0.12890625
44689 0.12890625
46801 0.12890625
48913 0.12890625

This is with sage 4.3.3 on sage.math.

So, the first 6673 everything is good. Then, regularly after 2112 rounds there is a leak of 135168 Byte. As Yann has observed, we have 135168==2112*64. Thus it seems that the memory is allocated in chunks, but the leak is 64 Byte in each round.

The leak does not occur for multivariate rings:

sage: R.<x,y> = QQ[]
sage: M = get_memory_usage()
sage: for n in range(50000):
....:     Mnew = get_memory_usage()
....:     if Mnew > M:
....:         print n, Mnew-M
....:         M=Mnew
....:     a = R(1)
....:
0 1.5

Component: memleak

Keywords: univariate polynomial ring coercion, pari, sig_on

Author: Gonzalo Tornaría

Reviewer: Paul Zimmermann

Merged: sage-4.4.alpha0

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

@simon-king-jena simon-king-jena added this to the sage-4.4 milestone Mar 5, 2010
@simon-king-jena

This comment has been minimized.

@simon-king-jena

This comment has been minimized.

@simon-king-jena

This comment has been minimized.

@sagetrac-mhampton
Copy link
Mannequin

sagetrac-mhampton mannequin commented Mar 5, 2010

comment:5

Sage-4.3.3 does not have this problem on my mac running 10.6.2.

@simon-king-jena
Copy link
Member Author

comment:6

For the record:

Gonzalo Tornaria stated at sage-devel:

"I think this is caused by a duplicate _sig_on in the bottom part of

pari.__call__."

@tornaria
Copy link
Contributor

Attachment: trac_8444.fix_pari_memleak.patch.gz

fix memory leak due to dup call to _sig_on

@tornaria
Copy link
Contributor

Changed keywords from univariate polynomial ring coercion to univariate polynomial ring coercion, pari, sig_on

@tornaria
Copy link
Contributor

Author: Gonzalo Tornaria

@tornaria
Copy link
Contributor

comment:7

The attached patch seems to fix the memleak. I worked on top of 4.3.3; all tests pass with this patch, and the snippet in the ticket description goes through without extra allocations:

sage: R.<x> = QQ[]
sage: M = get_memory_usage()
sage: for n in range(50000):
....:     Mnew = get_memory_usage()
....:     if Mnew > M:
....:         print n, Mnew-M
....:         M=Mnew
....:     a = R(1)
....:     
0 1.6015625
sage: 

Here's the full commit log which explains the patch:

#8444: fix memory leak due to dup call to _sig_on in the bottom part of PariInstance.__call__

At the bottom of PariInstance.__call__ both _sig_on (first) and _sig_str
(later) are used. In fact, both count as calls to _sig_on (actually _sig_on is
equivalent to _sig_str(NULL)) and these are not reentrant, i.e. nesting is not
supported (anyway, there's only one implicit _sig_off in the call to new_gen).

A double _sig_on, as defined in interrupt.h, is usually equivalent to a single
one -- however, for the pari library these macros are overrided as _pari_sig_on
(defined in misc.h and pari_err.h) adding a call to pari's own error catching
function. Calling the err_catch() function twice without the corresponding
call to err_leave() results in a memory leak which is reported in #8444.

The patch is one-liner: removing the first _sig_on fixes the memory leak.

Note: this line was added by changeset 10536:423520e7d069 as part of a large
effort to add lots of missing calls to _sig_on in pari interface. However, I
think in this case it was just an oversight because the already existing call to
_sig_on was only implicit in the call to _sig_str.

@zimmermann6
Copy link

Reviewer: Paul Zimmermann

@zimmermann6
Copy link

comment:8

Dear Gonzalo,

great work! It is very important to fix such issues, thanks a lot.

Paul

@jhpalmieri
Copy link
Member

comment:9

Merged "trac_8444.fix_pari_memleak.patch" into 4.4.alpha0.

@jhpalmieri
Copy link
Member

Merged: sage-4.4.alpha0

@fchapoton
Copy link
Contributor

Changed author from Gonzalo Tornaria to Gonzalo Tornaría

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants