'How to limit physical memory in python3?

I am trying to limit the memory usage of a python script, so it gets killed if it exceeds a threshold.

I tried using resource.setrlimit but the module seems to be limited.

resource.setrlimit(resource.RLIMIT_RSS,...) doesn't seem to work on newer Linux kernels.

resource.setrlimit(resource.RLIMIT_AS,...) works but it also counts virtual and shared memory, thus killing the process when it doesn't need to.

I need a solution that works directly in python, without using control groups or other tools.

Any way I might achieve this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source