Server Administration
How to Reduce Server Resource Usage: Practical Tips for CPU, RAM and Disk
By NileVPS Team · Published · 4 min read
اقرأ المقال بالعربيA server that wastes resources costs more than it should and has less headroom for real traffic. Many VPS installations run services nobody uses, keep too many worker processes alive, and recompute the same pages thousands of times a day. The good news is that most savings come from a few simple changes. This guide walks through practical steps to reduce CPU, memory, disk and bandwidth usage on a Linux server, in the order that usually gives the best results.
Step 1: measure before you change anything
Optimization without measurement is guessing. Record a baseline first:
uptime
free -h
df -h
ps -eo pid,pcpu,pmem,rss,cmd --sort=-rss | head -15
systemd-cgtop # resource usage grouped by serviceNote peak and idle values, ideally over a full day with a monitoring tool. Then change one thing at a time and compare. If you are not sure which resource is the limit, start with our guide to finding server bottlenecks.
Step 2: remove what you do not need
Every running service uses memory, and many wake up regularly to use CPU.
systemctl list-units --type=service --state=running
systemctl disable --now <service-name>Common candidates on a web server: a desktop environment installed by mistake, mail servers you do not use, print services, unused database engines, old application versions, and leftover test containers (docker ps -a, docker system df). Remove packages you no longer need with apt autoremove or dnf autoremove, after checking the list it proposes.
Step 3: right-size worker processes
Application servers usually start a pool of workers. Too many workers waste RAM and can push the server into swap; too few cause queuing.
For PHP-FPM, a reasonable approach is:
- Measure the average memory of one worker:
ps -C php-fpm8.2 -o rss=(adjust the name to your version). - Decide how much RAM you can give PHP after the database and OS.
- Set
pm.max_childrento roughly that budget divided by the per-worker size. - On low-traffic sites,
pm = ondemandwithpm.process_idle_timeout = 10sfrees memory when idle.
The same idea applies to Gunicorn, Puma, Node.js clusters and others: tie the worker count to the available cores and memory, not to a number copied from a tutorial.
Step 4: cache aggressively
Caching avoids doing the same work twice and is often the single biggest saving.
| Layer | What it saves | Examples |
|---|---|---|
| Opcode cache | Recompiling PHP on each request | OPcache |
| Object cache | Repeated database queries | Redis, Memcached |
| Page cache | Running the app for anonymous visitors | Nginx fastcgi_cache, Varnish |
| Browser and CDN cache | Serving static files repeatedly | Cache-Control headers, a CDN |
Make sure OPcache is enabled and has enough memory (opcache.memory_consumption), and set long cache lifetimes for versioned static assets.
Step 5: tune the database
Databases are frequently the heaviest process on a small server.
- Enable the slow query log and fix the worst queries first, usually by adding an index.
- Size buffers to the server: for MySQL or MariaDB,
innodb_buffer_pool_sizeshould fit your working data but leave room for everything else; for PostgreSQL, tuneshared_buffersandwork_memwith care. - Limit
max_connectionsand use connection pooling instead of opening a connection per request. - Clean up old rows such as expired sessions, logs stored in tables, and revisions.
Step 6: reduce disk usage and writes
du -xh / --max-depth=2 2>/dev/null | sort -h | tail -20
journalctl --disk-usage
sudo journalctl --vacuum-size=200MSet limits for the journal (SystemMaxUse= in /etc/systemd/journald.conf), make sure logrotate compresses and rotates application logs, prune old Docker images, and move old backups off the server. Reducing debug logging also lowers disk writes, which helps I/O-sensitive workloads.
Step 7: save bandwidth
- Enable gzip or Brotli compression for text responses.
- Serve images in modern formats and at the size they are displayed.
- Use a CDN for static files.
- Block hotlinking and abusive bots that crawl aggressively.
Step 8: schedule heavy jobs wisely
Backups, reports and imports should not run at peak time. Schedule them with cron during quiet hours and lower their priority:
nice -n 10 ionice -c2 -n7 /usr/local/bin/backup.shKnow when to stop optimizing
Once the server is lean, further tweaks bring small gains for real effort. If usage is still near the limit after these steps, upgrading is the sensible choice, and snapshots make it safe to test. You can compare sizes on the pricing page.
FAQ
Is high RAM usage always a problem on Linux?
No. Linux uses free memory for the page cache, which speeds up disk access and is released when applications need it. Watch the "available" value in free -h and swap activity rather than "used".
Will disabling swap save resources?
Usually not. A small amount of swap gives the kernel room to move rarely used pages out of RAM. The problem is constant swapping, which means the server needs less memory usage or more RAM.
How much can caching reduce CPU usage?
It depends on the application and how many requests can be served from cache. For content sites with many anonymous visitors, a page cache can remove most of the application work, but measure your own before and after numbers rather than relying on general claims.