我在IPython中处理一些GB大小的numpy数组。当我删除它们时,我肯定希望它们消失,以便恢复内存。 IPythons输出缓存在那里非常烦人,因为它甚至在删除了最后一个主动预期的对它们的引用后仍保持对象存活。我已经开始了
c.TerminalInteractiveShell.cache_size = 0
在IPython配置中,但这只禁用了对_oh
的条目缓存,还会创建其他变量,如_
,__
等。我也知道%xdel
,但无论如何,我宁愿完全禁用它,因为我很少使用输出历史,所以一个普通的del
会立即再次工作。
看看IPython/core/displayhook.py线209-214,我会说它不可配置。您可以尝试制作PR以添加完全禁用它的选项。
输入
echo "__builtin__._ = True" > ~/.config/ipython/profile_default/startup/00-disable-history.py
你的历史应该消失了。
编辑:
看起来像配置目录的路径有时有点不同,无论是~/.config/ipython
还是~/.ipython/
。所以只需检查你得到的那个并相应地调整路径。该解决方案仍适用于jupyter console
。
似乎我们可以通过放置“;”来抑制输出缓存现在在行尾。
见http://ipython.org/ipython-doc/stable/interactive/tips.html#suppress-output
创建一个ipython配置文件:
!ipython profile create
输出可能是(对于ipython v4.0):
[ProfileCreate] Generating default config file: '/root/.ipython/profile_default/ipython_config.py'
[ProfileCreate] Generating default config file: '/root/.ipython/profile_default/ipython_kernel_config.py'
然后将行'c.InteractiveShell.cache_size = 0'
添加到ipython_kernel_config.py
文件中
!echo 'c.InteractiveShell.cache_size = 0' >> /root/.ipython/profile_default/ipython_kernel_config.py
加载另一个ipython内核并检查是否有效
In [1]: 123
Out[1]: 123
In [2]: _1
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-51-21553803e553> in <module>()
----> 1 _1
NameError: name '_1' is not defined
In [3]: len(Out)
Out[3]: 0