Clear memory allocated by plotting in IPython

自闭症网瘾萝莉.ら 提交于 2019-12-05 18:16:23

After reading tcaswell's comment, I investigated a bit more what IPython is storing. This is my method:

## start afresh

## what variables are there before calling plot()?
## make sure you copy() otherwise it's just a reference.
before = vars().copy()

plt.plot(range(10))

## now, what's available after?
after = vars().copy()

## print out the differences
for k in after.keys():
  if k not in before.keys():
    print k

## or a bit more compact
print set(after.keys()) - set(before.keys())

Result is:

before
_i2
_i3
_2

Where _i* are string of the inputs and _n (with n a number) are the strings of the outputs. Removing those items and garbage collecting does not seem to free up memory though.

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