Explore Practical Examples of Using ulimit
In this final step, we will explore some practical examples of using the ulimit
command to manage system resources.
One common use case for ulimit
is to prevent processes from consuming too much memory. Let's create a script that allocates a large amount of memory:
#!/bin/bash
## Allocate a 1 GB array
big_array=( $(seq 1 $(( 1024 * 1024 ))) )
echo "Array size: ${#big_array[@]} elements"
Now, let's run the script and see what happens:
$ ./allocate_memory.sh
Array size: 1048576 elements
Segmentation fault
The script failed with a segmentation fault because it exceeded the default memory limit. Let's use ulimit
to set a memory limit and try again:
$ ulimit -v 1048576 ## Set the maximum virtual memory size to 1 GB
$ ./allocate_memory.sh
Array size: 1048576 elements
This time, the script runs successfully because we've set a 1 GB memory limit using ulimit
.
Another practical example is using ulimit
to limit the number of processes a user can run. This can be useful to prevent a user from launching too many processes and overloading the system. Let's create a script that spawns multiple child processes:
#!/bin/bash
## Spawn 100 child processes
for i in {1..100}; do
./allocate_memory.sh &
done
wait
Now, let's run the script and see what happens:
$ ulimit -u 50 ## Set the maximum number of user processes to 50
$ ./spawn_processes.sh
./spawn_processes.sh: fork: Resource temporarily unavailable
The script failed because we've set the maximum number of user processes to 50, and it tried to spawn 100 processes. Let's increase the limit and try again:
$ ulimit -u 100 ## Set the maximum number of user processes to 100
$ ./spawn_processes.sh
This time, the script runs successfully because we've increased the process limit.
These examples demonstrate how you can use ulimit
to manage system resources and prevent processes from consuming too many resources, which can help maintain the overall system stability and performance.