Linux ulimit Memory Limit: 3 Commands to Control RAM Usage

Quick Linux Tip #115:

Question: A buggy script is eating all my RAM. How do I cap its memory usage?

Try: ulimit -v 524288

Info: Restricts virtual memory for the current shell session and its child processes.

Examples:

  • $ ulimit -a  # Display all current resource limits for the session
  • $ ulimit -t 60  # Set a maximum CPU time limit in seconds
  • $ ulimit -n 65536  # Limit the maximum number of open file descriptors

Note: Use /etc/security/limits.conf for persistent per-user limits, or systemd service settings (MemoryMax) for production services. ulimit can only restrict limits downwards.




LinuxTeck.com
linuxteck:~$ ulimit -v 524288

linuxteck:~$ ulimit -v
524288

linuxteck:~$ python3 -c 'x = [0] * 10000000; print(len(x))'
10000000

linuxteck:~$ python3 -c 'x = [0] * 100000000; print(len(x))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
MemoryError
linuxteck:~$

PREVIOUS ARTICLE Quick Linux Tip #114: SSL Certificate Expiry: 3 Linux Commands to Check Dates
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.