我试图用我的大型Python应用程序调试内存问题.大多数内存都在numpy由Python类管理的数组中,所以Heapy等没用,因为它们不考虑numpy数组中的内存.所以我尝试使用MacOSX(10.7.5)活动监视器(或者top如果你愿意)手动跟踪内存使用情况.我注意到以下奇怪的行为.在普通的python解释器shell(2.7.3)上:
import numpy as np # 1.7.1
# Activity Monitor: 12.8 MB
a = np.zeros((1000, 1000, 17)) # a "large" array
# 142.5 MB
del a
# 12.8 MB (so far so good, the array got freed)
a = np.zeros((1000, 1000, 16)) # a "small" array
# 134.9 MB
del a
# 134.9 MB (the system didn't get back the memory)
import gc
gc.collect()
# 134.9 MB
Run Code Online (Sandbox Code Playgroud)
无论我做什么,Python会话的内存占用量将永远不会再低于134.9 MB.所以我的问题是:
为什么数组的资源大于1000x1000x17x8字节(在我的系统上经验找到)正确地返回给系统,而较小数组的内存似乎永远被Python解释器所困? …