Python GC 后内存未回收
Mac 中的 Python:我试图理解 gc.collect()
并且编写了这段代码。
我的期望是 diff
的值应该为零,但事实并非如此。
import gc
import numpy as np
import psutil
def create_and_destory():
np.random.randn(10000)
if __name__ == '__main__':
gc.collect()
start_process = psutil.Process().memory_info().rss
create_and_destory()
gc.collect()
end_process = psutil.Process().memory_info().rss
diff = end_process - start_process
print('diff value is: {}'.format(diff))
差异值为 36864
即使在强制触发垃圾收集后也无法完全了解此行为的原因。
PS:
仅在 MAC 中返回非零值。在 Linux 发行版中我得到了零!
Python in Mac: I was trying to understand gc.collect()
and i wrote this code.
My expectation is that value of diff
should be zero, but it isn't.
import gc
import numpy as np
import psutil
def create_and_destory():
np.random.randn(10000)
if __name__ == '__main__':
gc.collect()
start_process = psutil.Process().memory_info().rss
create_and_destory()
gc.collect()
end_process = psutil.Process().memory_info().rss
diff = end_process - start_process
print('diff value is: {}'.format(diff))
diff value is 36864
Couldn't quite get the reason for this behavior even after forceful trigger of garbage collection.
PS:
This returns a non-zero value only in MAC. In linux distros am getting a zero!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论