Experiencing sluggish performance, unresponsive terminals, or unexpected application crashes on your MIG servers infrastructure usually points to memory exhaustion.
This guide provides the exact diagnostic commands and mitigation strategies to stabilize your Linux environment and permanently resolve memory leaks, using production-safe best practices.
1. Evaluate Current Memory Availability
Before taking action, you must understand how your server distributes its resources. Run the following command to get a snapshot of your system's memory:
free -h
Total
Description: Physical RAM installed on your server.
Actionable Insight: Baseline reference for your hardware capacity.
Available
Description: Memory currently free and ready for new processes.
Actionable Insight: If consistently near zero (and swap is growing), your server is struggling.
Buff/Cache
Description: RAM used by the Linux kernel to cache files.
Actionable Insight: High numbers are healthy; Linux frees this automatically when apps need RAM.
Swap
Description: Disk space used as emergency overflow RAM.
Actionable Insight: Sustained, active swap usage severely degrades performance.
2. Identify Resource Hogs
Locate the specific applications draining your RAM using built-in Linux utilities.
Interactive Monitoring: Run
topand pressShift + Mto sort active tasks by memory usage. For a more readable interface, install and runhtop(pressF6to sort by memory).Targeted Process Snapshot: To immediately print the top 10 memory-consuming processes directly to your terminal:
ps aux --sort=-%mem | head -10
- Track Memory Leaks: If you suspect an application's Resident Set Size (RSS) is growing endlessly without releasing memory, monitor its Process ID (PID) over a 10-minute window:
for i in $(seq 1 10); do ps -o pid,rss,vsz -p <PID>; sleep 60; done
3. Immediate Remediation Actions
If your server is actively freezing, apply these commands to safely recover system resources.
- Gracefully Terminate Rogue Processes: Send a standard termination signal to allow the application to clean up and shut down properly.
kill <PID>
Crucial Warning: Only use kill -9 as an absolute last resort if the process is completely frozen. It forces an immediate shutdown without cleanup, which can cause data corruption in databases or file writes.>
- Restart Application Services: Restarting daemons (like Nginx, MySQL, or Node.js) flushes their allocated memory pools.
sudo systemctl restart <service_name>
- Diagnostic Cache Clearing (Use Sparingly): Linux intentionally uses free RAM to cache files for better performance. Dropping the cache can prove whether RAM is truly locked up by applications, but doing this routinely will actually hurt server performance as the kernel is forced to re-read files from the disk.
sync && echo 1 | sudo tee /proc/sys/vm/drop_caches
4. Long-Term Server Optimization
Prevent future bottlenecks by tuning kernel parameters and configuring workload-specific limits.
Lower Swappiness: Depending on your workload, you can encourage the kernel to favor physical RAM before relying on slow disk swap. Open /etc/sysctl.conf, append vm.swappiness=10, and apply with sudo sysctl -p.
Deploy Emergency Swap Space: Swap prevents fatal Out-Of-Memory (OOM) crashes by providing overflow space, though it is not a replacement for sufficient physical RAM. Size this appropriately for your server (e.g., 2GB to 4GB):
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
-
Enforce Workload-Based Memory Limits: Prevent a single memory leak from taking down the entire server by setting Systemd limits. Run
sudo systemctl edit <service_name>and configure limits based on your app's actual needs:
[Service]
MemoryMax=4G
MemorySwapMax=0
(Note: Set MemoryMax based on actual profiling; if set too low, Systemd will routinely kill your service. Setting MemorySwapMax=0 strictly prevents the service from using swap, which guarantees an OOM kill if physical RAM limits are exceeded).
- Audit and Disable Unused Services: Background services consume baseline RAM. Identify unnecessary software and disable it from booting:
sudo systemctl disable <service_name>
Discussion:
What is your go-to terminal command when a server starts lagging? Drop your tips in the comments below! 👇
Top comments (0)