Why can't objgraph capture the growth of np.array()?

柔情痞子 提交于 2019-12-12 14:48:15

问题


See the code:

import objgraph
import numpy as np
objgraph.show_growth()
j = 20
y = []
for i in range(5):
    for l in range(j):
        y.append(np.array([np.random.randint(500),np.random.randint(500)]))
    print 'i:',i
    objgraph.show_growth()
    print '___'
    #objgraph.show_most_common_types(limit=100)
    j += 1

the result is:

i: 1
wrapper_descriptor 1596 +3
weakref 625 +1
dict 870 +1
method_descriptor 824 +1
i: 2
i: 3
i: 4

For the 2,3 and 4 epoch, it shows nothing growing. But it should show that the number of numpy.array grows


回答1:


I'm not that familiar with objgraph specifically, but I think the same issue applies to other Python heap analysis tools such as heapy.

Numpy arrays are implemented in C, and do their own reference counting by internally calling Py_INCREF and Py_DECREF. As such, they are not tracked by the Python garbage collector. Tools like heapy and (presumably) objgraph use the Python garbage collector to track references to objects, so as a result numpy arrays are invisible to them.



来源:https://stackoverflow.com/questions/34563978/why-cant-objgraph-capture-the-growth-of-np-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!