为什么 objgraph 无法捕获 np.array() 的增长?

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

查看代码:

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

结果是:

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

对于 2,3 和 4 epoch,它没有显示任何增长。但它应该表明 numpy.array 的数量在增长

我不是特别熟悉 objgraph,但我认为同样的问题也适用于其他 Python 堆分析工具,例如 heapy.

Numpy 数组是用 C 实现的,并且自己做 reference counting by internally calling Py_INCREF and Py_DECREF. As such, they are not tracked by the Python garbage collectorheapy 和(大概)objgraph 等工具使用 Python 垃圾收集器来跟踪对对象的引用,因此 numpy 数组对它们是不可见的。