<p dir="auto">Following the example given in the documentation I am using <code class="notranslate">rv_continuous</code> to generate a custom pdf. The following code snippet works as expected in scipy v0.18.1 but exhibits a very large (~GB) memory consumption when run with scipy v0.19.0</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from scipy.stats import rv_continuous
import numpy as np
import resource
import scipy
import sys

print &quot;numpy version: {}&quot;.format(np.version.full_version)
print &quot;Scipy version: {}&quot;.format(scipy.version.full_version)
print &quot;Python {}&quot;.format(sys.version)

class gaussian_gen(rv_continuous):
    &quot;Gaussian distribution&quot;
    def _pdf(self, x):
        return np.exp(-x**2 / 2.) / np.sqrt(2.0 * np.pi)

def print_mem():
    mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    print 'Memory usage: %s (kb)' % mem

print_mem()
gaussian = gaussian_gen(name='gaussian')
print_mem()
values = gaussian.rvs(size=1000)
print_mem()
values = gaussian.rvs(size=5000)
print_mem()"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">scipy</span>.<span class="pl-s1">stats</span> <span class="pl-k">import</span> <span class="pl-s1">rv_continuous</span>
<span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">resource</span>
<span class="pl-k">import</span> <span class="pl-s1">scipy</span>
<span class="pl-k">import</span> <span class="pl-s1">sys</span>

<span class="pl-k">print</span> <span class="pl-s">"numpy version: {}"</span>.<span class="pl-en">format</span>(<span class="pl-s1">np</span>.<span class="pl-s1">version</span>.<span class="pl-s1">full_version</span>)
<span class="pl-k">print</span> <span class="pl-s">"Scipy version: {}"</span>.<span class="pl-en">format</span>(<span class="pl-s1">scipy</span>.<span class="pl-s1">version</span>.<span class="pl-s1">full_version</span>)
<span class="pl-k">print</span> <span class="pl-s">"Python {}"</span>.<span class="pl-en">format</span>(<span class="pl-s1">sys</span>.<span class="pl-s1">version</span>)

<span class="pl-k">class</span> <span class="pl-s1">gaussian_gen</span>(<span class="pl-s1">rv_continuous</span>):
    <span class="pl-s">"Gaussian distribution"</span>
    <span class="pl-k">def</span> <span class="pl-en">_pdf</span>(<span class="pl-s1">self</span>, <span class="pl-s1">x</span>):
        <span class="pl-k">return</span> <span class="pl-s1">np</span>.<span class="pl-en">exp</span>(<span class="pl-c1">-</span><span class="pl-s1">x</span><span class="pl-c1">**</span><span class="pl-c1">2</span> <span class="pl-c1">/</span> <span class="pl-c1">2.</span>) <span class="pl-c1">/</span> <span class="pl-s1">np</span>.<span class="pl-en">sqrt</span>(<span class="pl-c1">2.0</span> <span class="pl-c1">*</span> <span class="pl-s1">np</span>.<span class="pl-s1">pi</span>)

<span class="pl-k">def</span> <span class="pl-en">print_mem</span>():
    <span class="pl-s1">mem</span> <span class="pl-c1">=</span> <span class="pl-s1">resource</span>.<span class="pl-en">getrusage</span>(<span class="pl-s1">resource</span>.<span class="pl-v">RUSAGE_SELF</span>).<span class="pl-s1">ru_maxrss</span>
    <span class="pl-k">print</span> <span class="pl-s">'Memory usage: %s (kb)'</span> <span class="pl-c1">%</span> <span class="pl-s1">mem</span>

<span class="pl-en">print_mem</span>()
<span class="pl-s1">gaussian</span> <span class="pl-c1">=</span> <span class="pl-en">gaussian_gen</span>(<span class="pl-s1">name</span><span class="pl-c1">=</span><span class="pl-s">'gaussian'</span>)
<span class="pl-en">print_mem</span>()
<span class="pl-s1">values</span> <span class="pl-c1">=</span> <span class="pl-s1">gaussian</span>.<span class="pl-en">rvs</span>(<span class="pl-s1">size</span><span class="pl-c1">=</span><span class="pl-c1">1000</span>)
<span class="pl-en">print_mem</span>()
<span class="pl-s1">values</span> <span class="pl-c1">=</span> <span class="pl-s1">gaussian</span>.<span class="pl-en">rvs</span>(<span class="pl-s1">size</span><span class="pl-c1">=</span><span class="pl-c1">5000</span>)
<span class="pl-en">print_mem</span>()</pre></div>
<p dir="auto">The output of the snippet for version 0.18.1 is:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="numpy version: 1.12.1
Scipy version: 0.18.1
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609]
Memory usage: 70048 (kb)
Memory usage: 70048 (kb)
Memory usage: 70048 (kb)
Memory usage: 70592 (kb)"><pre class="notranslate"><code class="notranslate">numpy version: 1.12.1
Scipy version: 0.18.1
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609]
Memory usage: 70048 (kb)
Memory usage: 70048 (kb)
Memory usage: 70048 (kb)
Memory usage: 70592 (kb)
</code></pre></div>
<p dir="auto">while 0.19.0 looks as follows:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="numpy version: 1.12.0
Scipy version: 0.19.0
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609]
Memory usage: 69672 (kb)
Memory usage: 69672 (kb)
Memory usage: 426952 (kb)
Memory usage: 2215576 (kb)"><pre class="notranslate"><code class="notranslate">numpy version: 1.12.0
Scipy version: 0.19.0
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609]
Memory usage: 69672 (kb)
Memory usage: 69672 (kb)
Memory usage: 426952 (kb)
Memory usage: 2215576 (kb)
</code></pre></div>
<p dir="auto">I just noticed that it appears to be working in master, but I figured it might be good to post this issue in any case, just so that people can find it or if it was only fixed by accident. Output when run on master:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="numpy version: 1.12.1
Scipy version: 1.0.0.dev0+ab9eff3
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609]
Memory usage: 75972 (kb)
Memory usage: 75972 (kb)
Memory usage: 75972 (kb)
Memory usage: 75972 (kb)"><pre class="notranslate"><code class="notranslate">numpy version: 1.12.1
Scipy version: 1.0.0.dev0+ab9eff3
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609]
Memory usage: 75972 (kb)
Memory usage: 75972 (kb)
Memory usage: 75972 (kb)
Memory usage: 75972 (kb)
</code></pre></div>
<p dir="auto">Before opening this issue I asked a question about it at <a href="http://stackoverflow.com/questions/43181237/how-to-keep-memory-consumption-at-bay-when-using-scipy-stats-rv-continuous" rel="nofollow">stackoverflow</a></p>
<p dir="auto">After updating Scipy to 0.19.0 I have experienced an increase in memory consumption which seems to be related to integrate.quad. The following code calculates some quadratures and checks the available RAM before and after:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
from scipy.integrate import quad
import psutil
import gc

xx_max = np.linspace(1,1000,num=100)
print(&quot;Memory available: %f GiB&quot;%(psutil.virtual_memory().available/1024.**3))
[[quad(lambda x:x**2,0,x) for x in xx_max] for _ in range(3000)]
print(&quot;Memory available after quad: %f GiB&quot;%(psutil.virtual_memory().available/1024.**3))
gc.collect()
gc.collect()
gc.collect()
print(&quot;Memory available after garbage collecting: %f GiB&quot;%(psutil.virtual_memory().available/1024.**3))"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">from</span> <span class="pl-s1">scipy</span>.<span class="pl-s1">integrate</span> <span class="pl-k">import</span> <span class="pl-s1">quad</span>
<span class="pl-k">import</span> <span class="pl-s1">psutil</span>
<span class="pl-k">import</span> <span class="pl-s1">gc</span>

<span class="pl-s1">xx_max</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">linspace</span>(<span class="pl-c1">1</span>,<span class="pl-c1">1000</span>,<span class="pl-s1">num</span><span class="pl-c1">=</span><span class="pl-c1">100</span>)
<span class="pl-en">print</span>(<span class="pl-s">"Memory available: %f GiB"</span><span class="pl-c1">%</span>(<span class="pl-s1">psutil</span>.<span class="pl-en">virtual_memory</span>().<span class="pl-s1">available</span><span class="pl-c1">/</span><span class="pl-c1">1024.</span><span class="pl-c1">**</span><span class="pl-c1">3</span>))
[[<span class="pl-en">quad</span>(<span class="pl-k">lambda</span> <span class="pl-s1">x</span>:<span class="pl-s1">x</span><span class="pl-c1">**</span><span class="pl-c1">2</span>,<span class="pl-c1">0</span>,<span class="pl-s1">x</span>) <span class="pl-k">for</span> <span class="pl-s1">x</span> <span class="pl-c1">in</span> <span class="pl-s1">xx_max</span>] <span class="pl-k">for</span> <span class="pl-s1">_</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-c1">3000</span>)]
<span class="pl-en">print</span>(<span class="pl-s">"Memory available after quad: %f GiB"</span><span class="pl-c1">%</span>(<span class="pl-s1">psutil</span>.<span class="pl-en">virtual_memory</span>().<span class="pl-s1">available</span><span class="pl-c1">/</span><span class="pl-c1">1024.</span><span class="pl-c1">**</span><span class="pl-c1">3</span>))
<span class="pl-s1">gc</span>.<span class="pl-en">collect</span>()
<span class="pl-s1">gc</span>.<span class="pl-en">collect</span>()
<span class="pl-s1">gc</span>.<span class="pl-en">collect</span>()
<span class="pl-en">print</span>(<span class="pl-s">"Memory available after garbage collecting: %f GiB"</span><span class="pl-c1">%</span>(<span class="pl-s1">psutil</span>.<span class="pl-en">virtual_memory</span>().<span class="pl-s1">available</span><span class="pl-c1">/</span><span class="pl-c1">1024.</span><span class="pl-c1">**</span><span class="pl-c1">3</span>))</pre></div>
<p dir="auto">When I run this with scipy 0.19 I find my system losses around 1GiB, while this does not happen if I use the prior release instead. The memory is freed after the process exists, but asking the garbage collector to do its work does not help.<br>
The system I used is a 64-bit Arch Linux, using both python2 and python3 from clean virtualenvs.</p>