<p dir="auto"><em>Original ticket <a href="http://projects.scipy.org/scipy/ticket/35" rel="nofollow">http://projects.scipy.org/scipy/ticket/35</a> on 2006-03-09 by trac user russel@..., assigned to unknown.</em></p>
<p dir="auto">Run the following program and watch it's memory usage.  Removing the jacobian from the call to leastsq<br>
keeps memory usage constant.  It also leaks at the same rate if the jacobian is transposed and col_deriv=0.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import numpy as N
from scipy.optimize import leastsq

x=N.arange(0,1,0.0001)
y=(x-0.3)**2

def residual(p, x, y):
  return (p[0]*x+p[1])*x+p[2]-y

def jacobian(p, x, y):
  jac=N.zeros(shape=(len(p),len(x)),dtype=float)
  jac[0]=x*x
  jac[1]=x
  jac[2]=1
  return jac

def fit_j(x, y):
  p=N.ones(3, dtype=float)
  fit, mesg = leastsq(residual, p, args=(x, y), Dfun=jacobian, col_deriv=1)
  return p

def test_leak(nfit=10000):
  for k in xrange(nfit):
    p=fit_j(x,y)

if __name__==&quot;__main__&quot;:
  test_leak()"><pre class="notranslate"><code class="notranslate">import numpy as N
from scipy.optimize import leastsq

x=N.arange(0,1,0.0001)
y=(x-0.3)**2

def residual(p, x, y):
  return (p[0]*x+p[1])*x+p[2]-y

def jacobian(p, x, y):
  jac=N.zeros(shape=(len(p),len(x)),dtype=float)
  jac[0]=x*x
  jac[1]=x
  jac[2]=1
  return jac

def fit_j(x, y):
  p=N.ones(3, dtype=float)
  fit, mesg = leastsq(residual, p, args=(x, y), Dfun=jacobian, col_deriv=1)
  return p

def test_leak(nfit=10000):
  for k in xrange(nfit):
    p=fit_j(x,y)

if __name__=="__main__":
  test_leak()
</code></pre></div>
<p dir="auto"><a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/jperkin/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/jperkin">@jperkin</a> added a patch to the pkgsrc package for scipy with the commit message:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Apply a couple of patches to fix SunOS."><pre class="notranslate"><code class="notranslate">Apply a couple of patches to fix SunOS.
</code></pre></div>
<p dir="auto">I can't evaluate the patch or know details about the problem. Here's the patch:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="--- scipy/special/_round.h.orig 2018-05-05 17:10:11.000000000 +0000
+++ scipy/special/_round.h
@@ -9,6 +9,10 @@
 #include &quot;_c99compat.h&quot;
 #include &quot;cephes/dd_idefs.h&quot;
 
+#if defined(__cplusplus)
+using std::isinf;
+using std::isnan;
+#endif
 
 double add_round_up(double a, double b)
 {"><pre class="notranslate"><code class="notranslate">--- scipy/special/_round.h.orig 2018-05-05 17:10:11.000000000 +0000
+++ scipy/special/_round.h
@@ -9,6 +9,10 @@
 #include "_c99compat.h"
 #include "cephes/dd_idefs.h"
 
+#if defined(__cplusplus)
+using std::isinf;
+using std::isnan;
+#endif
 
 double add_round_up(double a, double b)
 {
</code></pre></div>