# How to Fix High RAM Usage on a Linux Dedicated Server

Experiencing sluggish performance, unresponsive terminals, or unexpected application crashes on your [MIG servers](https://www.migservers.com/) 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:

```shell
free -h
```

<table style="min-width: 75px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p><strong>Metric</strong></p></td><td colspan="1" rowspan="1"><p><strong>Description</strong></p></td><td colspan="1" rowspan="1"><p><strong>Actionable Insight</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Total</strong></p></td><td colspan="1" rowspan="1"><p>Physical RAM installed on your MIG Server.</p></td><td colspan="1" rowspan="1"><p>Baseline reference for your hardware capacity.</p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Available</strong></p></td><td colspan="1" rowspan="1"><p>Memory currently free and ready for new processes.</p></td><td colspan="1" rowspan="1"><p>If this is consistently near zero and swap is growing, your server is actively struggling.</p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Buff/Cache</strong></p></td><td colspan="1" rowspan="1"><p>RAM used by the Linux kernel to cache files.</p></td><td colspan="1" rowspan="1"><p>High numbers here are healthy and expected; Linux frees this automatically when apps need RAM.</p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Swap</strong></p></td><td colspan="1" rowspan="1"><p>Disk space used as emergency overflow RAM.</p></td><td colspan="1" rowspan="1"><p>Sustained, active swap usage severely degrades performance.</p></td></tr></tbody></table>

## 2\. Identify Resource Hogs

Locate the specific applications draining your RAM using built-in Linux utilities.

*   **Interactive Monitoring:** Run `top` and press `Shift + M` to sort active tasks by memory usage. For a more readable interface, install and run `htop` (press `F6` to sort by memory).
    
*   **Targeted Process Snapshot:** To immediately print the top 10 memory-consuming processes directly to your terminal:
    

```shell
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:
    

```shell
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.

```shell
kill <PID>
```

> **Crucial Warning:** Only use `kill -9 <PID>` 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.

```shell
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.

```shell
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):

```shell
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:

```plaintext
[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 is great for performance-critical apps, but 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:

```shell
sudo systemctl disable <service_name>
```

### **Conclusion**

Proper memory management is crucial for maintaining a healthy Linux server. By actively monitoring resources and implementing systemd limits, you can prevent unexpected downtimes and keep your production environment stable.

*What are your favorite Linux commands for debugging server performance? Let us know in the comments!* 👇
