Table of Contents
High memory usage can cause slow performance, service crashes, and unexpected out-of-memory errors on a Linux VPS.
Step 1: Check Current Memory Usage
Start by viewing overall RAM usage.
free -h
Focus on used, available, and swap memory.
Step 2: Monitor Memory Usage in Real Time
Use top to identify processes consuming the most memory.
top
Press M to sort by memory usage.
Step 3: Use htop for Better Visibility
htop makes memory analysis easier.
sudo apt update
sudo apt install htop -y
htop
Memory usage is shown per process in real time.
Step 4: Identify Memory-Hungry Processes
Once you identify the PID, inspect it further.
ps -fp PID
Replace PID with the process ID.
Step 5: Restart or Stop the Problematic Service
Restarting often frees leaked memory.
sudo systemctl restart service_name
Or stop the process if it is unnecessary.
sudo kill PID
Step 6: Clear Page Cache and Buffers
Linux uses free RAM for caching. Clearing cache may temporarily free memory.
sudo sync
sudo echo 3 > /proc/sys/vm/drop_caches
Use this only for troubleshooting.
Step 7: Check Swap Usage
Heavy swap usage indicates RAM pressure.
swapon --show
If swap is constantly used, consider increasing RAM or optimizing services.
Step 8: Limit Memory Usage for Processes
Prevent a single service from consuming all memory.
sudo systemctl set-property service_name MemoryMax=512M
This limits the service to 512MB RAM.
You may also want to review this related article: Fix High CPU Usage on a Linux VPS
Optional Step: Enable OOM Protection
Ensure the system kills misbehaving processes before crashing.
sudo sysctl vm.overcommit_memory=2