Sage: Ticket #16411: Memory leak in polynomial evaluation
https://trac.sagemath.org/ticket/16411
<p>
The following was reported at sage-support:
</p>
<pre class="wiki">sage: C.<x,y,z> = GF(2)[]
sage: f = x^4+x*y^3+z^6
sage: a = f(1,0,0)
sage: get_memory_usage()
176.08984375
sage: for i in xrange(1000000):
....: a = f(1,0,0)
....:
sage: get_memory_usage()
198.08984375
sage: for i in xrange(1000000):
....: a = f(1,0,0)
....:
sage: get_memory_usage()
222.08984375
</pre><p>
In the following I am citing Leif's comments from sage-devel:
</p>
<p>
In <code>singular_polynomial_call(&res, self._poly, _ring, coerced_x, MPolynomial_libsingular_get_element)</code> we have
</p>
<pre class="wiki">struct spolyrec
{
poly next; // next needs to be the first field
number coef; // and coef the second --- do not change
this !!!
unsigned long exp[VARS]; // make sure that exp is aligned
};
</pre><p>
where both <code>next</code> and <code>coef</code> are pointers, and <code>VARS</code> is usually zero, so
<code>exp</code> is an "open-ended" array, such that the effective size of the struct varies.
</p>
<p>
The leak depends on the values (and the amount also on the field
and the function). My impression is also that it appears whenever
res!=NULL, i.e., the result is non-zero.
</p>
<p>
The code in singular_polynomial_call() (in
src/sage/libs/singular/polynomial.pyx) explicitly prevents Singular from
reclaiming the memory:
</p>
<pre class="wiki"> ...
ret[0] = res_id.m[0]
from_id.m[0] = NULL
res_id.m[0] = NULL
id_Delete(&to_id, r)
id_Delete(&from_id, r)
id_Delete(&res_id, r)
return 0
</pre><p>
(<code>from_id.m[0]</code> was set to the input parameter <code>p</code>, so that's ok.)
</p>
<p>
Either it should make a garbage-collected copy of it (the result
ret / ret[0]) instead, or the caller has to clean up afterwards.
</p>
<p>
The docstring is quite misleading w.r.t. 'ret', as only the address of a
pointer to be changed is passed to the function, while the struct it
later points to always gets allocated by the callee, not the caller.
</p>
en-usSagehttps://trac.sagemath.org/chrome/site/logo_sagemath_trac.png
https://trac.sagemath.org/ticket/16411
Trac 1.1.6SimonKingThu, 29 May 2014 11:17:46 GMT
https://trac.sagemath.org/ticket/16411#comment:1
https://trac.sagemath.org/ticket/16411#comment:1
<p>
I am not sure if this is a problem to be reported upstream. Probably not, it seems like the culprit is on our side, in <code>sage.libs.singular.polynomial</code>.
</p>
TicketleifThu, 29 May 2014 15:54:49 GMT
https://trac.sagemath.org/ticket/16411#comment:2
https://trac.sagemath.org/ticket/16411#comment:2
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/16411#comment:1" title="Comment 1">SimonKing</a>:
</p>
<blockquote class="citation">
<p>
It seems like the culprit is on our side, in <code>sage.libs.singular.polynomial</code>.
</p>
</blockquote>
<p>
Yep, that's my impression at least. If the caller should clean up, it's <code>sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular.__call__()</code> (and probably more).
</p>
<p>
We'd presumably have to change both functions though.
</p>
<p>
(I haven't looked at the modules in whole; perhaps there are more potential leaks. <code>new_MP()</code> looked suspicious to me as well, but isn't used / called in the given example.)
</p>
Ticketvbraun_spamSun, 10 Aug 2014 16:51:03 GMTmilestone changed
https://trac.sagemath.org/ticket/16411#comment:3
https://trac.sagemath.org/ticket/16411#comment:3
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.3</em> to <em>sage-6.4</em>
</li>
</ul>
TicketSimonKingThu, 23 Jul 2015 14:45:23 GMTstatus, milestone changed; reviewer set
https://trac.sagemath.org/ticket/16411#comment:4
https://trac.sagemath.org/ticket/16411#comment:4
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>needs_review</em>
</li>
<li><strong>reviewer</strong>
set to <em>Simon King</em>
</li>
<li><strong>milestone</strong>
changed from <em>sage-6.4</em> to <em>sage-duplicate/invalid/wontfix</em>
</li>
</ul>
<p>
With <a class="needs_work ticket" href="https://trac.sagemath.org/ticket/18905" title="defect: fix more leaks found in #18897 (needs_work)">#18905</a>, I get
</p>
<pre class="wiki">sage: C.<x,y,z> = GF(2)[]
sage: f = x^4+x*y^3+z^6
sage: a = f(1,0,0)
sage: get_memory_usage()
1028.44921875
sage: for i in xrange(1000000):
....: a = f(1,0,0)
....:
sage: get_memory_usage()
1028.44921875
</pre><p>
So, I suggest to close it as a duplicate.
</p>
TicketSimonKingThu, 23 Jul 2015 14:45:48 GMTstatus changed
https://trac.sagemath.org/ticket/16411#comment:5
https://trac.sagemath.org/ticket/16411#comment:5
<ul>
<li><strong>status</strong>
changed from <em>needs_review</em> to <em>positive_review</em>
</li>
</ul>
<p>
(duplicate of <a class="needs_work ticket" href="https://trac.sagemath.org/ticket/18905" title="defect: fix more leaks found in #18897 (needs_work)">#18905</a>)
</p>
TicketvbraunSun, 26 Jul 2015 12:39:24 GMTstatus changed; resolution set
https://trac.sagemath.org/ticket/16411#comment:6
https://trac.sagemath.org/ticket/16411#comment:6
<ul>
<li><strong>status</strong>
changed from <em>positive_review</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>duplicate</em>
</li>
</ul>
Ticket