[Python] How to limit the memory usage of a Python process?

Discussão em 'Python' iniciado por Stack, Janeiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to limit the memory usage of a Python service running in Kubernetes. I'm currently testing with Python 3.10 running in WSL2. I want the service to be aware of limitations set by Kubernetes, so it can throw a MemoryError when it's trying to allocate too much memory and handle that error in the code. I'm trying to set RLIMIT_AS as described here. Kubernetes has a limit of 500 MB for the process. But when I set the RLIMIT_AS to 500 MB, the service does not even start. I then wrote a very simple script and checked how low I can set the RLIMIT_AS.

    Script:

    import resource

    limit = 1000 * 1024 * 1024 # 1000 MB
    resource.setrlimit(resource.RLIMIT_AS, (limit, limit))
    print(f"Setting memory limit to {limit} bytes.")
    bytearray(1 * 1024 * 1024) # Allocate 1 MB
    print("Successfully allocated memory.")


    Output:

    Setting memory limit to 1048576000 bytes.
    Traceback (most recent call last):
    File "/mnt/c/Users/xxx/foobar.py", line 6, in <module>
    bytearray(1 * 1024 * 1024) # Allocate 1 MB
    MemoryError

    Process finished with exit code 1


    I have to set the limit to 1048 MB for the script to be successful. "htop" in WSL2 is showing me a VIRT of 1047 MB for the Python script, so it seems RLIMIT_AS has to be greater than VIRT, which is already more than 1 GB for the most simple script.

    PID USER PRI NI VIRT RES SHR S CPU%▽ MEM% TIME+ Command
    56232 xxx 20 0 1046M 81112 26156 S 0.0 0.2 0:00.10 /home/xxx/.virtualenvs/xxx/bin/python3 /mnt/c/Users/xxx/foobar.py



    Why is VIRT so high? How can I limit the real memory usage of the process?

    Continue reading...

Compartilhe esta Página