Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quit Python program when it hits memory limit

Tags:

python

numpy

I have a couple of Python/Numpy programs that tend to cause the PC to freeze/run very slowly when they use too much memory. I can't even stop the scripts or move the cursor anymore, when it uses to much memory (e.g. 3.8/4GB) Therefore, I would like to quit the program automatically when it hits a critical limit of memory usage, e.g. 3GB.

I could not find a solution yet. Is there a Pythonic way to deal with this, since I run my scripts on Windows and Linux machines.

like image 351
HyperCube Avatar asked Jan 20 '14 15:01

HyperCube


People also ask

How do I force a Python program to quit?

To stop a python script, just press Ctrl + C.

Is there a memory limit for Python?

Python doesn't limit memory usage on your program. It will allocate as much memory as your program needs until your computer is out of memory. The most you can do is reduce the limit to a fixed upper cap. That can be done with the resource module, but it isn't what you're looking for.

What happens when Python runs out of memory?

End of dialog window. A programming language will raise a memory error when a computer system runs out of RAM Random Access Memory or memory to execute code. If it fails to execute a Python script, the Python interpreter will present a MemoryError exception for the Python programming.


1 Answers

You could limit the process'es memory limit, but that is OS specific.

Another solution would be checking value of psutil.virtual_memory(), and exiting your program if it reaches some point.

Though OS-independent, the second solution is not Pythonic at all. Memory management is one of the things we have operating systems for.

like image 95
Bartosz Marcinkowski Avatar answered Sep 23 '22 18:09

Bartosz Marcinkowski