Table of Contents
- Why Performance Optimization Matters
- Step 1: Monitor Before Optimizing
- Optimizing CPU Performance
- Optimizing Memory (RAM) Usage
- Optimizing Disk I/O Performance
- Optimizing Network Performance
- Startup & Service Optimization
- Kernel & System Tweaks
- Best Practices for Long-Term Optimization
- Conclusion
- References
Why Performance Optimization Matters
Poor Linux performance can manifest as:
- Slow application launches
- Unresponsive desktop environments
- High server latency
- Frequent crashes or freezes
Optimization isn’t just about speed—it also improves reliability, reduces power consumption (critical for laptops/embedded devices), and extends hardware lifespan. Even small tweaks can make a big difference!
Step 1: Monitor Before Optimizing
Rule #1: Always measure before making changes. Blindly tweaking settings can worsen performance or break things. Use these tools to identify bottlenecks.
Key Monitoring Tools
| Tool | Purpose | Example Command | What to Look For |
|---|---|---|---|
top/htop | Real-time CPU/memory/process monitor | htop | High CPU/memory usage, processes with %CPU > 80%, load average (1-min > cores) |
vmstat | Virtual memory statistics | vmstat 5 (refresh every 5s) | si/so (swap in/out) > 0, wa (I/O wait) > 20% |
iostat | Disk I/O statistics | iostat -x 5 | %util (disk utilization) > 80%, await (avg I/O wait) > 20ms |
iftop | Network bandwidth usage | sudo iftop -i eth0 | High bandwidth usage on unnecessary ports/protocols |
free | Memory usage summary | free -h | Low available RAM, high used swap |
systemd-analyze | Boot time analysis (systemd systems) | systemd-analyze blame | Services taking >10s to start |
What to Look For
Focus on metrics that deviate from “normal”:
- CPU: Load average > number of CPU cores (e.g., 4-core CPU with load 5.0 = bottleneck).
- Memory:
availableRAM < 10% of total, frequent swapping (si/so> 0). - Disk:
%util> 80% (disk is saturated), highawait(slow I/O). - Network: Sustained bandwidth > 90% of your plan, high latency (
ping> 100ms).
Optimizing CPU Performance
Identifying CPU Bottlenecks
Common causes:
- A single process hogging CPU (e.g., a misbehaving app, background task).
- Too many processes competing for cores.
- High I/O wait (
wainvmstat), where CPU is idle waiting for disk/network.
Fixes: Process Management & Resource Limits
1. Kill or Restart Rogue Processes
Use htop to find processes with high %CPU. Press F9 to send a signal (e.g., SIGTERM to terminate gracefully).
Example:
# Find PID of a process (e.g., "chrome")
pgrep chrome # Output: 1234
# Kill it (gracefully)
kill 1234
# If unresponsive, force kill
kill -9 1234
2. Adjust Process Priority with nice/renice
Lower-priority processes (higher nice value) get less CPU time.
nice: Start a process with low priority (range: -20 to 19; default 0).
Example:nice -n 10 ./heavy_script.sh(runs script with lower priority).renice: Adjust priority of a running process.
Example:sudo renice -n 15 -p 1234(lower priority of PID 1234).
3. Limit CPU Usage with cpulimit
Prevent a process from exceeding a CPU percentage:
sudo apt install cpulimit
sudo cpulimit -p 1234 -l 50 # Limit PID 1234 to 50% CPU
Optimizing Memory (RAM) Usage
Understanding Memory Metrics
Use free -h to check memory:
total used free shared buff/cache available
Mem: 15Gi 8.2Gi 1.3Gi 1.1Gi 6.0Gi 6.2Gi
Swap: 2.0Gi 500Mi 1.5Gi
available: RAM available for new apps (most important).buff/cache: Linux caches files to speed up access—this is normal and freed when needed.swap: Disk space used as “overflow” when RAM is full (slow, avoid if possible).
Fixes: Reducing Swap & Cleaning Cache
1. Adjust swappiness
swappiness (0-100) controls how aggressively Linux swaps. Lower = less swapping.
- For desktops/laptops: Set to 10-20 (swap only when RAM is nearly full).
- For servers: Set to 0-5 (avoid swap if possible).
# Check current value
cat /proc/sys/vm/swappiness # Default: 60
# Temporary change (reset on reboot)
sudo sysctl vm.swappiness=10
# Permanent change (persists after reboot)
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p # Apply changes
2. Clear Cache (Temporarily)
Linux clears cache automatically, but you can force it (use sparingly!):
# Clear pagecache, dentries, and inodes
sudo sysctl -w vm.drop_caches=3
3. Optimize Applications
- Close unused apps/tabs (e.g., 50 Chrome tabs = ~5GB RAM!).
- Use lightweight alternatives:
libreoffice --writer→abiword,firefox→midori.
Optimizing Disk I/O Performance
Monitoring Disk Activity
Use iostat -x 5 to check disk health:
Device r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 5.00 3.00 200.00 120.00 80.00 0.50 62.50 50.00 80.00 5.00 40.00
%util: % of time disk is busy (target < 80%).await: Avg time (ms) for I/O requests (target < 20ms).
Fixes: Filesystems, SSDs, and Cleanup
1. Use Fast Filesystems
For SSDs/HDDs:
- ext4: Default, reliable, and fast for most use cases.
- XFS: Better for large files (e.g., media servers).
Avoidbtrfsfor beginners (advanced features but higher overhead).
2. Optimize SSDs
- Enable TRIM (reclaims unused space):
sudo fstrim -av # Run once; enable weekly via cron: `sudo crontab -e` → `@weekly /sbin/fstrim -av` - Disable unnecessary writes (e.g., log rotation, tmpfs for
/tmp):
Add to/etc/fstab:tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0
3. Clean Up Disk Space
- Delete large files:
du -sh /*(find big directories),rm -rf /path/to/large/files. - Clear old logs:
sudo journalctl --vacuum-size=100M(systemd logs). - Uninstall unused apps:
sudo apt autoremove(Debian/Ubuntu),sudo dnf autoremove(Fedora).
Optimizing Network Performance
Diagnosing Network Issues
Use iftop (bandwidth) or ss (socket stats):
sudo ss -tuln # List listening TCP/UDP ports
Look for:
- Unnecessary services listening on public IPs (e.g.,
sshdon0.0.0.0if not needed). - High bandwidth usage on non-essential ports (e.g., P2P apps).
Fixes: Bandwidth Control & TCP Tuning
1. Limit Bandwidth with wondershaper
Cap bandwidth for a specific interface:
sudo apt install wondershaper
sudo wondershaper eth0 1000 500 # Limit eth0 to 1000Kbps down, 500Kbps up
2. Tune TCP Settings (Safe for Beginners)
Edit /etc/sysctl.conf to optimize TCP:
# Enable TCP window scaling (better for high-latency networks)
net.ipv4.tcp_window_scaling = 1
# Reduce TIME_WAIT sockets (frees resources)
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
Apply changes: sudo sysctl -p.
Startup & Service Optimization
Reducing Boot Time
Use systemd-analyze to see what’s slowing boot:
systemd-analyze blame # List services by startup time
# Example output: 12.345s NetworkManager.service
Disabling Unnecessary Services
Stop and disable services you don’t need (e.g., bluetooth, cups if no printer):
# Check status
systemctl status bluetooth
# Stop temporarily
sudo systemctl stop bluetooth
# Disable permanently (won’t start on boot)
sudo systemctl disable bluetooth
# Mask (prevent accidental start)
sudo systemctl mask bluetooth
Critical services to keep: systemd-journald, networking, sshd (if remote access).
Kernel & System Tweaks
Safe Kernel Parameters for Beginners
Avoid random sysctl tweaks! Stick to tested profiles:
1. Use tuned (Red Hat/Fedora) or powertop
tunedapplies pre-configured optimization profiles:sudo dnf install tuned sudo tuned-adm list # Show profiles (e.g., "desktop", "server", "powersave") sudo tuned-adm profile desktop # Apply desktop profilepowertop(for laptops): Optimizes power usage (runsudo powertop --auto-tune).
Kernel Updates
New kernels often include performance fixes. Update safely:
sudo apt update && sudo apt upgrade # Debian/Ubuntu
sudo dnf update # Fedora
Best Practices for Long-Term Optimization
- Monitor regularly: Use
htop,vmstat, andiostatto catch issues early. - Update software: New versions fix bugs and improve efficiency.
- Backup before changes: Use
Timeshift(desktop) orrsync(server) to revert if needed. - Test incrementally: Change one setting at a time and measure impact.
Conclusion
Linux performance optimization is a journey, not a one-time fix. Start with monitoring to identify bottlenecks, then apply targeted fixes to CPU, memory, disk, or network. For beginners, focus on process management, startup services, and cleanup before diving into kernel tweaks. With practice, you’ll keep your system running fast and stable!