我在运行
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
内存充足
total used free shared buff/cache available
Mem: 125G 3.3G 104G 879M 17G 120G
64 位 Anaconda https://repo.anaconda.com/archive/Anaconda3-2022.10-Linux-x86_64.sh
我已在 jupyter_notebook_config.json 和 中将 max_buffer_size 设置为 64GB jupyter_notebook_config.py,只是为了确保在命令行上指定它:
jupyter笔记本 --certfile=ssl/mycert.perm --keyfile ssl/mykey.key --no-browser --NotebookApp.max_buffer_size=64000000000
还有
cat /proc/sys/vm/overcommit_memory
1
我运行一个简单的内存分配片段:
size = int(6e9)
chunk = size * ['r']
print (chunk.__sizeof__()/1e9)
作为独立的 .py 文件,它可以工作:
python ../readgzip.py
48.00000004
很高兴地报告它为我的列表分配了 48GB。
但是,在 Jupyter Notebook 中相同的代码最多只能运行到 7.76GB:
size = int(9.7e8)
chunk = size * ['r']
print (chunk.__sizeof__()/1e9)
7.76000004
将数组大小从 9.7e8 增加到 9.75e8 后失败
---------------------------------------------------------------------------
MemoryError Traceback (most recent call last)
/tmp/ipykernel_12328/3436837519.py in <module>
1 size = int(9.75e8)
----> 2 chunk = size * ['r']
3 print (chunk.__sizeof__()/1e9)
MemoryError:
此外,在我的 64GB 内存的家用 Windows11 机器上,我可以轻松运行上面的代码并分配 32GB 内存。
似乎我缺少有关 Linux 上 Jupyter 设置的一些信息
我错过了什么?
谢谢
最佳答案
在linux(可能还有其他操作系统,但我不确定)上,MemoryError
并不意味着机器的内存已耗尽(在这种情况下,通常会调用OOM killer),而是进程已达到限制(又名 ulimit ,已过时),超过该限制内核不愿意为其分配额外的内存。
你可以使用python的resource库来检查当前进程的限制(并可能设置它们,具有足够的权限)。这是一个例子:
$ prlimit --as
RESOURCE DESCRIPTION SOFT HARD UNITS
AS address space limit unlimited unlimited bytes
$ prlimit --pid=$$ --as=$((1024*1024*20)):
prlimit --as
RESOURCE DESCRIPTION SOFT HARD UNITS
AS address space limit 20971520 unlimited bytes
$ python
Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import resource
>>> resource.getrlimit(resource.RLIMIT_AS)
(20971520, -1)
>>> longstr = "r" * 1024*1024*10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
MemoryError
>>> longstr = "r" * 1024*1024*3
>>> resource.setrlimit(resource.RLIMIT_AS, (1024*1024*30, resource.RLIM_INFINITY))
>>> resource.getrlimit(resource.RLIMIT_AS)
(31457280, -1)
>>> longstr = "r" * 1024*1024*10
>>> len(longstr)
10485760
>>>
增加 Jupyter-Notebook 的限制应该从进程本身外部完成,因为通常不建议以 super 用户权限运行 python 进程。
了解有关 Linux 的更多信息 prlimit(1)实用程序和 getrlimit(2)系统调用。
关于python - Jupyter 中存在 MemoryError,但 Python 中没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74999834/