Python | How to put limits on Memory and CPU Usage
This article aims to show how to put limits on the memory or CPU use of a program running. Well to do so, Resource module can be used and thus both the task can be performed very well as shown in the code given below:
Code #1 : Restrict CPU time
# importing libraries import signal import resource import os # checking time limit exceed def time_exceeded(signo, frame): print("Time's up !") raise SystemExit(1) def set_max_runtime(seconds): # setting up the resource limit soft, hard = resource.getrlimit(resource.RLIMIT_CPU) resource.setrlimit(resource.RLIMIT_CPU, (seconds, hard)) signal.signal(signal.SIGXCPU, time_exceeded) # max run time of 15 millisecond if __name__ == '__main__': set_max_runtime(15) while True: pass |
SIGXCPU signal is generated when the time expires on running this code and the program can clean up and exit.
Code #2 : In order to restrict memory use, the code puts a limit on the total address space
# using resource import resource def limit_memory(maxsize): soft, hard = resource.getrlimit(resource.RLIMIT_AS) resource.setrlimit(resource.RLIMIT_AS, (maxsize, hard)) |
When no more memory is available then the program will start generating MemoryError exceptions with a memory limit.
How it works ?
- To set a soft and hard limit on a particular resource, setrlimit() function is used.
- The soft limit is a value upon which the operating system will notify the process via a signal or typically restrict it.
- An upper bound on the values is defined by the hard limit and it may be used for the soft limit.
- Although the hard limit can be lowered, it can never be raised by user processes and is controlled by a system-wide parameter set by the system administrator. (even if the process lowered itself).
- The setrlimit() function can additionally be used to set limits on things such as the number of child processes, number of open files, and similar system resources.
The code in this article only works on Unix systems, and that it might not work on all of them.
Recommended Posts:
- Python | sympy.integrate() using limits
- Python | Size of string in memory
- Long Short Term Memory Networks Explanation
- Text Generation using Recurrent Long Short Term Memory Network
- Python | Index of Non-Zero elements in Python list
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Important differences between Python 2.x and Python 3.x with examples
- Python | Convert list to Python array
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to Python Libraries
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to a Python Script
- Python | Visualizing O(n) using Python
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

