Urgent.News

What's breaking now, across thousands of outlets.

Tech

How to Fix High Memory Usage on a Linux Server

Linux server running out of memory? Learn how to diagnose and fix high memory usage with real commands — before it takes down your app. Your app starts slowing down, the OOM killer fires, or your monitoring page turns red — and the culprit is memory. High memory usage on a Linux server is one of the most common production crises for small teams, and it's easy to misread. Linux intentionally uses…

Dealing with a Linux server running out of memory can be a common and stressful issue for small teams. It's crucial to understand the difference between high memory usage due to caching and actual memory exhaustion. Here's how to diagnose and resolve high memory usage on a Linux server:

1. Get a clear picture of the memory usage. Run the command `free -h` to see total, used, free, and available memory. Pay attention to the available memory column, which accounts for reclaimable cache and is more useful than the free column. Another command is `vmstat 1 5`, which provides a series of five one-second snapshots, showing swap-in and swap-out activity. Run `cat /proc/meminfo` for a full breakdown of memory usage, including Slab, PageTables, and AnonPages.

2. If swap is being actively used (si/so values above zero consistently), then your server is genuinely memory-constrained, unlike when swap space exists but remains idle. Now, find the processes consuming your RAM by running `ps aux --sort=-%mem | head -20`. This command lists the top 20 processes by memory percentage.

3. Diagnose the root cause of the memory issue. Common causes include memory leaks, misconfigured heap limits, running too many processes, kernel slab cache issues, and large log or data buffers. To check for a memory leak, monitor a specific process's RSS (resident set size) using `watch -n 5 ps -p -o rss=`. For misconfigured heap limits, check the startup flags for Java apps (`-Xmx` for maximum heap size) or Node.js (`--max-old-space-size` for maximum heap size). Use `slabtop` to see if kernel object caches are unusually large.

4. Free memory and reduce pressure if needed, following these steps in order of preference: restart the leaking or bloated process, drop the page cache with `echo 1 /proc/sys/vm/drop_caches`, use `2` to free dentries/inodes, `3` for both. You can also reduce worker counts, such as PHP-FPM `pm.max_children` or Unicorn workers, to reflect your actual RAM budget.

Alternatively, increase swap temporarily using a `swapfile` with commands like `fallocate`, `chmod`, `mkswap`, and `swapon`. Adjust `vm.swappiness` to 10 to make the kernel prefer keeping processes in RAM over swapping.

5. Finally, prevent future memory spikes by setting per-process memory limits using `systemd`'s `MemoryMax=` in service unit files or `cgroups`. Add alerts for available memory instead of just used memory, and schedule regular restarts for known-leaky long-running processes. Profile your app under realistic load with tools like Valgrind (C/C++), `memory_profiler` (Python), or `clinic.js` (Node.js) to catch leaks before production.

For continuous tracking of per-process memory trends, consider using Opservo, which provides per-process memory tracking, anomalies detection, and alerts.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Thursday 27 August →