简体   繁体   English

Jupyter 中的 MemoryError 但不是 Python

[英]MemoryError in Jupyter but not in Python

I'm running我在跑

NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"

with plenty of memory有很多 memory

             total        used        free      shared  buff/cache   available
Mem:           125G        3.3G        104G        879M         17G        120G

64 bit Anaconda https://repo.anaconda.com/archive/Anaconda3-2022.10-Linux-x86_64.sh 64 位 Anaconda https://repo.anaconda.com/archive/Anaconda3-2022.10-Linux-x86_64.sh

I have set max_buffer_size to 64GB in both jupyter_notebook_config.json and jupyter_notebook_config.py, and just to make sure specify it on the command line:我在 jupyter_notebook_config.json 和 jupyter_notebook_config.py 中都将 max_buffer_size 设置为 64GB,只是为了确保在命令行中指定它:

jupyter notebook --certfile=ssl/mycert.perm --keyfile ssl/mykey.key --no-browser --NotebookApp.max_buffer_size=64000000000

And also并且

cat /proc/sys/vm/overcommit_memory
1

I run a simple memory allocation snippet:我运行一个简单的 memory 分配片段:

    size = int(6e9)
    chunk = size * ['r']
    print (chunk.__sizeof__()/1e9)

as a standalone.py file and it works:作为一个 standalone.py 文件,它可以工作:

python ../readgzip.py
48.00000004

happily reporting that it allocated 48GB for my list.高兴地报告说它为我的列表分配了 48GB。

However, the same code in a jupyter notebook only works up to 7.76GB:但是,jupyter notebook 中的相同代码最多只能运行 7.76GB:

    size = int(9.7e8)
    chunk = size * ['r']
    print (chunk.__sizeof__()/1e9)
7.76000004

and fails after increase the array size from 9.7e8 to 9.75e8并将数组大小从 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: 

Also, on my home Windows11 machine with 64GB of memory I can easily run the code above and allocate 32GB of memory.此外,在我的家用 Windows11 机器上,64GB 的 memory 我可以轻松运行上面的代码并分配 32GB 的 memory。

Seems like, I'm missing something about the Jupyter setup on Linux好像,我在 Linux 上遗漏了一些关于 Jupyter 设置的信息

What am I missing?我错过了什么?

Thank you谢谢

On linux (and possibly other OSs, but I'm not sure), MemoryError doesn't mean that the machine's memory has exhausted (in which case OOM killer would usually be invoked), but rather that the process has reached a limit (AKA ulimit , obsolete) over which the kernel is not willing to allocate it with additional memory.在 linux(可能还有其他操作系统,但我不确定)上, MemoryError并不意味着机器的 memory 已耗尽(在这种情况下通常会调用OOM 杀手),而是进程已达到限制(AKA ulimit ,已过时),kernel 不愿意为其分配额外的 memory。

You can use python's resource library to check the current process's limits (and possibly set them, having sufficient permissions).您可以使用 python 的资源库来检查当前进程的限制(并可能设置它们,具有足够的权限)。 Here's an example:这是一个例子:

$ 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
>>>

Increasing Jupyter-Notebook's limits should be done from outside the process itself, as it is generally not recommended running python processes with superuser privileges.增加 Jupyter-Notebook 的限制应该从进程本身外部完成,因为通常不建议使用超级用户权限运行 python 进程。

Read more about linux's prlimit(1) utility and the getrlimit(2) system call.阅读有关 linux 的prlimit(1)实用程序和getrlimit(2)系统调用的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM