- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我经常用完一些 Jupyter Notebooks 的 RAM,而且我似乎无法释放不再需要的内存。这是一个例子:
import gc
thing = Thing()
result = thing.do_something(...)
thing = None
gc.collect()
如您所料,thing
使用大量内存来做某事,然后我就不再需要它了。我应该能够释放它使用的内存。即使它没有写入我可以从我的笔记本访问的任何变量,垃圾收集器也没有正确释放空间。我发现的唯一解决方法是将 result
写入 pickle,重新启动内核,从 pickle 加载 result
,然后继续。这在运行长笔记本时确实很不方便。如何正确释放内存?
最佳答案
这里有很多问题。第一个是 IPython(当你看到像 Out[67]
这样的东西时,Jupyter 在幕后使用的东西会保留对对象的额外引用。事实上,你可以使用该语法来调用对象并用它做一些事情. eg. str(Out[67])
. 第二个问题是 Jupyter 似乎保留了它自己的输出变量引用,所以只有 IPython 的完全重置才能工作。但这并没有太大的不同只是重新启动笔记本电脑。
虽然有一个解决方案!我编写了一个您可以运行的函数,它将清除所有变量,除了您明确要求保留的变量。
def my_reset(*varnames):
"""
varnames are what you want to keep
"""
globals_ = globals()
to_save = {v: globals_[v] for v in varnames}
to_save['my_reset'] = my_reset # lets keep this function by default
del globals_
get_ipython().magic("reset")
globals().update(to_save)
你会像这样使用它:
x = 1
y = 2
my_reset('x')
assert 'y' not in globals()
assert x == 1
下面我写了一个笔记本,它向您展示了幕后发生的一些事情,以及您如何通过使用 weakref
模块来查看何时真正删除了某些内容。您可以尝试运行它,看看它是否有助于您了解正在发生的事情。
In [1]: class MyObject:
pass
In [2]: obj = MyObject()
In [3]: # now lets try deleting the object
# First, create a weak reference to obj, so we can know when it is truly deleted.
from weakref import ref
from sys import getrefcount
r = ref(obj)
print("the weak reference looks like", r)
print("it has a reference count of", getrefcount(r()))
# this prints a ref count of 2 (1 for obj and 1 because getrefcount
# had a reference to obj)
del obj
# since obj was the only strong reference to the object, it should have been
# garbage collected now.
print("the weak reference looks like", r)
the weak reference looks like <weakref at 0x7f29a809d638; to 'MyObject' at 0x7f29a810cf60>
it has a reference count of 2
the weak reference looks like <weakref at 0x7f29a809d638; dead>
In [4]: # lets try again, but this time we won't print obj, will just do "obj"
obj = MyObject()
In [5]: print(getrefcount(obj))
obj
2
Out[5]: <__main__.MyObject at 0x7f29a80a0c18>
In [6]: # note the "Out[5]". This is a second reference to our object
# and will keep it alive if we delete obj
r = ref(obj)
del obj
print("the weak reference looks like", r)
print("with a reference count of:", getrefcount(r()))
the weak reference looks like <weakref at 0x7f29a809db88; to 'MyObject' at 0x7f29a80a0c18>
with a reference count of: 7
In [7]: # So what happened? It's that Out[5] that is keeping the object alive.
# if we clear our Out variables it should go away...
# As it turns out Juypter keeps a number of its own variables lying around,
# so we have to reset pretty everything.
In [8]: def my_reset(*varnames):
"""
varnames are what you want to keep
"""
globals_ = globals()
to_save = {v: globals_[v] for v in varnames}
to_save['my_reset'] = my_reset # lets keep this function by default
del globals_
get_ipython().magic("reset")
globals().update(to_save)
my_reset('r') # clear everything except our weak reference to the object
# you would use this to keep "thing" around.
Once deleted, variables cannot be recovered. Proceed (y/[n])? y
In [9]: print("the weak reference looks like", r)
the weak reference looks like <weakref at 0x7f29a809db88; dead>
关于Python 垃圾收集有时在 Jupyter Notebook 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49515085/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!