How to Master Linux Resource Limits: Boost Your Server Stability
This article explains how Linux system and user resource limits—such as file descriptors, stack size, and process counts—can cause runtime failures, demonstrates practical experiments to modify these limits via ulimit, /etc/security/limits.conf, and prlimit, and provides guidance for stable server-side programming.
Introduction
When writing programs we often overlook system resource limits, yet these limits can cause serious problems such as forgotten open file descriptors, malloc failures, or stack overflows. This article shows how to identify and prevent these issues.
User‑Level Resource Limits
The ulimit command displays user‑level limits, which are defined in /etc/security/limits.conf (and files under /etc/security/limits.d). These limits affect users logging in via PAM but do not affect system services.
Typical limits of interest include:
Stack size: 8 MB
Maximum processes: over 30 000
File descriptor limit: 1024
Soft limits can be lowered by the user, while hard limits are set by root and can only be increased by root. Use ulimit -a to view all limits, or ulimit -Ha / ulimit -Sa for hard and soft limits respectively.
Experiment 1 – Increase File Descriptor Limit
Add the following snippet to /etc/security/limits.conf (or a file in /etc/security/limits.d) and reboot:
* soft nofile 10240
* hard nofile 10240After reboot, verify the new limit with ulimit -n. A test program that attempts to open 10 240 temporary files succeeds, opening 10 217 files (the remainder are used by the program itself and shared libraries).
Experiment 2 – Increase Stack Size
Add the following lines to /etc/security/limits.conf:
* soft stack 8192000
* hard stack 8192000Compile and run a C program that recursively allocates stack space. The program crashes with a segmentation fault when the stack reaches roughly 8 192 000 B, confirming the new limit.
System‑Level Resource Limits
Key system‑wide limits include:
Maximum open files per process: 1 billion
PID limit: ~4 million file‑max (kernel‑enforced max file descriptors): 6 million
Total threads limit: 60 000
Maximum memory‑mapped areas per process: 60 000
Redis File Descriptor Adjustment
When starting redis‑server, you may see a log line such as:
Increased maximum number of open files to 10032 (it was originally set to 1024).Redis raises the limit by calling setrlimit(RLIMIT_NOFILE, &limit).
prlimit Command
The prlimit command provides functionality similar to ulimit but can operate on any running process.
Conclusion
Linux resource limits are crucial for server‑side developers. Adjusting them via /etc/security/limits.conf (and rebooting) helps fully utilize system capabilities and avoid unexpected failures.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
