<p dir="auto">Some operations on recarray views of structured arrays leak memory.</p>
<h3 dir="auto">Reproducing code example:</h3>

<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
import resource

# make a random structured array/recarray
arr = np.array(np.vstack(np.random.normal(size=10000000)), dtype=[(&quot;x&quot;, float), (&quot;y&quot;, float)])
rarr = arr.view(np.recarray)

# selection on structured array (works as expected, no memory leak)
def select_arr(rarr):
    arr_selection = arr[arr[&quot;x&quot;]&gt;0.0]
    return

# selection on recarray (leaks memory)
def select_rarr(rarr):
    rarr_selection = rarr[rarr.x&gt;0.0]
    return

while True:
    #select_arr(rarr) # memory stays constant
    select_rarr(rarr) # memory continuously increases
    print resource.getrusage(resource.RUSAGE_SELF).ru_maxrss"><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">import</span> <span class="pl-s1">resource</span>

<span class="pl-c"># make a random structured array/recarray</span>
<span class="pl-s1">arr</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>(<span class="pl-s1">np</span>.<span class="pl-en">vstack</span>(<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">normal</span>(<span class="pl-s1">size</span><span class="pl-c1">=</span><span class="pl-c1">10000000</span>)), <span class="pl-s1">dtype</span><span class="pl-c1">=</span>[(<span class="pl-s">"x"</span>, <span class="pl-s1">float</span>), (<span class="pl-s">"y"</span>, <span class="pl-s1">float</span>)])
<span class="pl-s1">rarr</span> <span class="pl-c1">=</span> <span class="pl-s1">arr</span>.<span class="pl-en">view</span>(<span class="pl-s1">np</span>.<span class="pl-s1">recarray</span>)

<span class="pl-c"># selection on structured array (works as expected, no memory leak)</span>
<span class="pl-k">def</span> <span class="pl-en">select_arr</span>(<span class="pl-s1">rarr</span>):
    <span class="pl-s1">arr_selection</span> <span class="pl-c1">=</span> <span class="pl-s1">arr</span>[<span class="pl-s1">arr</span>[<span class="pl-s">"x"</span>]<span class="pl-c1">&gt;</span><span class="pl-c1">0.0</span>]
    <span class="pl-k">return</span>

<span class="pl-c"># selection on recarray (leaks memory)</span>
<span class="pl-k">def</span> <span class="pl-en">select_rarr</span>(<span class="pl-s1">rarr</span>):
    <span class="pl-s1">rarr_selection</span> <span class="pl-c1">=</span> <span class="pl-s1">rarr</span>[<span class="pl-s1">rarr</span>.<span class="pl-s1">x</span><span class="pl-c1">&gt;</span><span class="pl-c1">0.0</span>]
    <span class="pl-k">return</span>

<span class="pl-k">while</span> <span class="pl-c1">True</span>:
    <span class="pl-c">#select_arr(rarr) # memory stays constant</span>
    <span class="pl-en">select_rarr</span>(<span class="pl-s1">rarr</span>) <span class="pl-c"># memory continuously increases</span>
    <span class="pl-k">print</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></pre></div>
<h3 dir="auto">Error message:</h3>
<p dir="auto">Memory usage increases until either the process is killed by OS or the system crashes.</p>
<h3 dir="auto">Numpy/Python version information:</h3>
<p dir="auto">This behaviour was observed in:<br>
numpy.<strong>version</strong> = 1.15.2<br>
sys.version = 2.7.5 (default, Jul 13 2018, 13:06:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]</p>
<p dir="auto">I tested with an older version of numpy that is also installed on my system and did not see a memory leak:<br>
numpy version = 1.7.1 sys.version = 2.7.5 (default, Jul 13 2018, 13:06:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]</p>
<p dir="auto"><em>Original ticket <a href="http://projects.scipy.org/numpy/ticket/1133" rel="nofollow">http://projects.scipy.org/numpy/ticket/1133</a> on 2009-06-08 by <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/rc/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/rc">@rc</a>, assigned to unknown.</em></p>
<p dir="auto">This ticket sumarizes the discussion in the numpy mailing list [1], concerning allowing non-unique arrays as function arguments, better naming conventions and documentation.</p>
<ol dir="auto">
<li>
<p dir="auto">the functions with _nu suffix should be merged with the versions expecting unique arrays as input, example:</p>
<p dir="auto">def intersect1d(ar1, ar2, assume_unique=False):<br>
if not assume_unique:<br>
return intersect1d_nu(ar1, ar2)<br>
else:<br>
... # the current code</p>
</li>
</ol>
<p dir="auto">The name of the keyword argument is yet to be resolved, other proposition so far were:</p>
<p dir="auto">isunique=False  or just unique=False (Josef)<br>
ar1_unique=False, ar2_unique=False (rc)</p>
<ol dir="auto">
<li>
<p dir="auto">deprecate the *_nu functions</p>
</li>
<li>
<p dir="auto">alias setmember1d_nu as <code class="notranslate">in1d</code> or <code class="notranslate">isin1d</code>, because the function is a "in" and not a set operation</p>
</li>
<li>
<p dir="auto">guarantee that setdiff1d works for non-unique arrays (even when<br>
implementation changes), and change documentation</p>
</li>
<li>
<p dir="auto">union1d, ediff1d and unique1d are defined for non-unique arrays - document it properly</p>
</li>
<li>
<p dir="auto">define np.setxor1d(np.unique(a), np.unique(b)) to become<br>
np.setxor1d(a, b, assume_unique=False)</p>
</li>
<li>
<p dir="auto">module name</p>
<blockquote>
<blockquote>
<p dir="auto">rename arraysetops to something easier to read like setfun. I think it<br>
would only affect internal changes since all functions are exported to<br>
the main numpy name space<br>
+1e-4  (I got used to arrayse_tops)</p>
</blockquote>
</blockquote>
</li>
</ol>
<p dir="auto">+0 (internal change only). Other numpy/scipy submodules containing a bunch of functions are called *pack (fftpack, arpack, lapack), *alg (linalg), *utils. *fun is used comonly in the matlab world.</p>
<ol dir="auto">
<li>keep docs in sync with correct usage</li>
</ol>
<p dir="auto">[1] <a href="mailto:numpy-discussion@scipy.org">numpy-discussion@scipy.org</a>, thread "extract elements of an array that are   contained in another array?"</p>