How to Check Memory Usage in Linux: free, top and ps Explained
To check memory usage in Linux, run free -h and read the available column. That is the memory programs can still use. Low "free" is normal, because Linux uses spare RAM as disk cache (buff/cache) and gives it back when needed. To find which program uses the most RAM, run ps aux --sort=-%mem | head or press M in top. If programs are being killed, check dmesg -T | grep -i oom.
Quick answer
free -h # read "available"
ps aux --sort=-%mem | head -6 # biggest RAM users (RSS column)
swapon --show # is there swap?
sudo dmesg -T | grep -i "out of memory" # OOM killer events
vmstat 1 5 # si/so > 0 = swapping now
A live example first
A student's WordPress site on a t3.micro (1 GB RAM) keeps showing "Error establishing a database connection". Let us look:
free -h
total used free shared buff/cache available
Mem: 949Mi 812Mi 41Mi 12Mi 95Mi 72Mi
Swap: 0B 0B 0B
Only 72 MiB available, and no swap. Now the big users:
ps aux --sort=-%mem | head -4
USER PID %MEM RSS COMMAND
mysql 702 41.2 400412 /usr/sbin/mariadbd
apache 1810 9.8 95320 php-fpm: pool www
apache 1811 9.6 93180 php-fpm: pool www
And the kernel log:
sudo dmesg -T | grep -i "killed process"
[Fri Oct 16 21:14:03 2026] Out of memory: Killed process 702 (mariadbd) total-vm:1532120kB, anon-rss:401204kB
So the database was being killed by the OOM killer, and WordPress could not connect. We made three changes:
- Lowered PHP-FPM
pm.max_childrenfrom 10 to 4. - Set MariaDB
innodb_buffer_pool_size = 128M. - Added a 1 GB swap file as a safety net:
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Now available stays above 300 MiB, and the error is gone. Friends, the number to watch was available, and the proof was in dmesg.
How does Linux use memory?
Watch available, not free. buff/cache is reused, and dmesg shows OOM kills.
Column in free -h |
Meaning |
|---|---|
total |
All RAM the system can use |
used |
Memory used by programs (and the kernel) |
free |
Memory nobody is using at all |
buff/cache |
File cache and buffers. Linux reuses this when programs need it |
available |
Estimate of memory new programs can get (free + reclaimable cache) |
Idle RAM is wasted RAM, so Linux fills it with cache to make disk reads faster. That is why free looks small on a healthy server.
How do I check memory step by step?
- Run
free -hand read available. - Run
ps aux --sort=-%mem | headand note the biggest processes. - Read the RSS column (resident memory in KB). VSZ (virtual size) is often huge and not a real problem.
- Run
swapon --showandvmstat 1 5. Non-zerosi/someans active swapping. - Run
sudo dmesg -T | grep -i oomto see if anything was killed. - Tune the biggest user (below), then check again.
Per-process memory in detail
top # press M to sort by memory, e to change units
htop # easier view, F6 to sort
pmap -x <PID> | tail -1 # total for one process
cat /proc/<PID>/status | grep -E 'VmRSS|VmSwap'
To total memory by program name (useful with many PHP or Apache workers):
ps -C php-fpm -o rss= | awk '{s+=$1} END {print s/1024 " MB"}'
Tune memory by stack
| Stack | Setting | Small server example (1–2 GB) |
|---|---|---|
| Java / Tomcat / Spring | -Xmx or -XX:MaxRAMPercentage |
-Xmx512m |
| Node.js | --max-old-space-size |
384 |
| Python Gunicorn | --workers |
2 |
| PHP-FPM | pm.max_children |
4–6 |
| Apache prefork | MaxRequestWorkers |
20 |
| MySQL / MariaDB | innodb_buffer_pool_size |
128M–256M |
| PostgreSQL | shared_buffers |
128MB |
| Redis | maxmemory + policy |
100mb, allkeys-lru |
| MongoDB | wiredTigerCacheSizeGB |
0.25 |
See EC2 memory in CloudWatch
EC2 metrics come from outside the virtual machine, so CPU and network are there, but memory and disk space are not. Install the CloudWatch agent on the instance and choose memory (mem_used_percent) and disk (disk_used_percent). Then you can set an alarm at, for example, 85% memory. Azure needs the Azure Monitor Agent and Google Cloud the Ops Agent for the same reason.
Real example: "Linux ate my RAM"
New Linux users so often panic about low "free" memory that a famous page, linuxatemyram.com, exists only to explain that disk cache is normal and harmless. Since Linux kernel 3.14 (2014), the available column was added to free to make this clearer. When you report memory in an incident, always quote available and any OOM messages, not "free".
Mistakes to avoid
- Dropping caches to "free memory".
echo 3 > /proc/sys/vm/drop_cachesonly makes the next reads slower. - Adding huge swap instead of fixing a leak. The server will crawl.
- Reading VSZ as real usage. Use RSS.
- Forgetting per-worker memory. 10 PHP workers × 90 MB = 900 MB.
Summary table
| Question | Command |
|---|---|
| How much is left? | free -h → available |
| Who uses it? | ps aux --sort=-%mem, top (M) |
| Swapping? | swapon --show, vmstat 1 |
| Anything killed? | dmesg -T \| grep -i oom |
| One process | /proc/PID/status, pmap -x |
| EC2 monitoring | CloudWatch agent mem_used_percent |
Quick quiz: check what you learned
Which free -h column shows memory programs can still use?
- available
- free
- shared
- total
Show answer
A. available includes cache that Linux can give back.
What is buff/cache?
- Spare RAM used as disk cache
- Memory leaked by apps
- Swap space on the disk
- RAM reserved for root
Show answer
A. Linux uses idle RAM to cache files and frees it when needed.
Where do you see that the OOM killer ended a process?
- dmesg or journalctl -k
- The crontab of root
- The /etc/hosts file
- The bash history file
Show answer
A. Kernel messages record OOM kills.
Why is EC2 memory missing from CloudWatch by default?
- AWS cannot see inside the VM
- Memory is always unlimited
- The metric costs nothing
- Linux has no memory data
Show answer
A. Memory is inside the OS; the agent must report it.
Learn it properly
These pages go deeper, with labs:
Frequently asked questions
How do I check memory usage in Linux?
Run free -h. The "available" column shows how much memory programs can still use.
Why is "free" memory so low?
Linux uses spare RAM as disk cache (buff/cache). It gives that memory back when programs need it, so low "free" is normal. Watch "available".
How do I see memory used per process?
Run ps aux --sort=-%mem | head, or open top and press M. The RES column is the real RAM used.
What is the OOM killer?
When Linux runs out of memory and swap, the kernel kills a process to survive. You will see "Out of memory: Killed process" in dmesg.
Why does EC2 not show memory in CloudWatch?
EC2 sees the virtual machine from outside and cannot see inside its memory. Install the CloudWatch agent to send memory metrics.
Should I add swap on a small EC2 instance?
A small swap file (1 to 2 GB) can prevent sudden OOM kills on t3.micro labs, but heavy swapping makes everything slow. Fix memory use first.