<div dir="ltr">I figured the reference to the object through the local scope would also be tracked by the GC somehow, since the entire stack frame can be regarded as a separate object itself, but apparently not.</div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Sep 15, 2014 at 1:06 PM, Mads Ipsen <span dir="ltr"><<a href="mailto:mads.ipsen@gmail.com" target="_blank">mads.ipsen@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thanks to everybody for taking time to answer!<br>
<br>
Best regards,<br>
<br>
Mads<br>
<div class="HOEnZb"><div class="h5"><br>
On 15/09/14 12:11, Sebastian Berg wrote:<br>
> On Mo, 2014-09-15 at 12:05 +0200, Eelco Hoogendoorn wrote:<br>
>><br>
>><br>
>><br>
>> On Mon, Sep 15, 2014 at 11:55 AM, Sebastian Berg<br>
>> <<a href="mailto:sebastian@sipsolutions.net">sebastian@sipsolutions.net</a>> wrote:<br>
>>          On Mo, 2014-09-15 at 10:16 +0200, Mads Ipsen wrote:<br>
>>          > Hi,<br>
>>          ><br>
>>          > I am trying to inspect the reference count of numpy arrays<br>
>>          generated by<br>
>>          > my application.<br>
>>          ><br>
>>          > Initially, I thought I could inspect the tracked objects<br>
>>          using<br>
>>          > gc.get_objects(), but, with respect to numpy objects, the<br>
>>          returned<br>
>>          > container is empty. For example:<br>
>>          ><br>
>>          > import numpy<br>
>>          > import gc<br>
>>          ><br>
>>          > data = numpy.ones(1024).reshape((32,32))<br>
>>          ><br>
>>          > objs = [o for o in gc.get_objects() if isinstance(o,<br>
>>          numpy.ndarray)]<br>
>>          ><br>
>>          > print objs                # Prints empty list<br>
>>          > print gc.is_tracked(data) # Print False<br>
>>          ><br>
>>          > Why is this? Also, is there some other technique I can use<br>
>>          to inspect<br>
>>          > all numpy generated objects?<br>
>>          ><br>
>><br>
>>          Two reasons. First of all, unless your array is an object<br>
>>          arrays (or a<br>
>>          structured one with objects in it), there are no objects to<br>
>>          track. The<br>
>>          array is a single python object without any referenced objects<br>
>>          (except<br>
>>          possibly its `arr.base`).<br>
>><br>
>>          Second of all -- and this is an issue -- numpy doesn't<br>
>>          actually<br>
>>          implement the traverse slot, so it won't even work for object<br>
>>          arrays<br>
>>          (numpy object arrays do not support circular garbage<br>
>>          collection at this<br>
>>          time, please feel free to implement it ;)).<br>
>><br>
>>          - Sebastian<br>
>><br>
>><br>
>><br>
>><br>
>><br>
>><br>
>> Does this answer why the ndarray object itself isn't tracked though? I<br>
>> must say I find this puzzling; the only thing I can think of is that<br>
>> the python compiler notices that data isn't used anymore after its<br>
>> creation, and deletes it right after its creation as an optimization,<br>
>> but that conflicts with my own experience of the GC.<br>
>><br>
>><br>
><br>
> Not sure if it does, but my quick try and error says:<br>
> In [15]: class T(tuple):<br>
>     ....:     pass<br>
>     ....:<br>
><br>
> In [16]: t = T()<br>
><br>
> In [17]: objs = [o for o in gc.get_objects() if isinstance(o, T)]<br>
><br>
> In [18]: objs<br>
> Out[18]: [()]<br>
><br>
> In [19]: a = 123.<br>
><br>
> In [20]: objs = [o for o in gc.get_objects() if isinstance(o, float)]<br>
><br>
> In [21]: objs<br>
> Out[21]: []<br>
><br>
> So I guess nothing is tracked, unless it contains things, and numpy<br>
> arrays don't say they can contain things (i.e. no traverse).<br>
><br>
> - Sebastian<br>
><br>
><br>
><br>
>> _______________________________________________<br>
>> NumPy-Discussion mailing list<br>
>> <a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
>> <a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
><br>
><br>
><br>
> _______________________________________________<br>
> NumPy-Discussion mailing list<br>
> <a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
> <a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
><br>
<br>
</div></div><span class="im HOEnZb">--<br>
+---------------------------------------------------------+<br>
| Mads Ipsen                                              |<br>
+----------------------+----------------------------------+<br>
| Gåsebæksvej 7, 4. tv | phone:              <a href="tel:%2B45-29716388" value="+4529716388">+45-29716388</a> |<br>
| DK-2500 Valby        | email:      <a href="mailto:mads.ipsen@gmail.com">mads.ipsen@gmail.com</a> |<br>
| Denmark              | map  :   <a href="http://www.tinyurl.com/ns52fpa" target="_blank">www.tinyurl.com/ns52fpa</a> |<br>
+----------------------+----------------------------------+<br>
</span><div class="HOEnZb"><div class="h5">_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
</div></div></blockquote></div><br></div>