### 10.5 Preventing excessive memory usage ¶

Sometimes a programming error will cause a process to allocate huge amounts of memory, consuming all the RAM on a system. To prevent this, the GNU Bash command `ulimit -v limit` can be used to restrict the amount of virtual memory available to each process. The limit is measured in kilobytes and applies to new processes started in the current shell. For example,

    $ ulimit -v 4096

will limit subsequent processes to 4 megabytes of virtual memory (4096k). By default the limit cannot be increased in the same session once it has been applied, so it is best to start a separate shell for reduced `ulimit` operations. Alternatively, you can set a soft limit (which can be undone) with the options `-S -v`.

In addition to preventing run-away processes, limiting the amount of memory a program is allowed to allocate also provides a way to test how robustly out of memory conditions are handled. An artificially low limit can be used to simulate running out of memory—a well-written program should not crash in this case.

The `ulimit` command supports other options including `-p`, which restricts the number of child processes that can be created, and `-t`, which places a limit on the number of CPU seconds that a process can run for. The complete list of settings can be shown with the command `ulimit -a`. To display more information about the `ulimit` command, type `help ulimit` at the Bash prompt.