Ubuntu
When systems start hanging, slowing down, or behaving unpredictably, the root cause is usually one of these:
- Memory exhaustion
- Disk exhaustion
- Runaway containers or services
- Cache buildup
This guide helps you systematically diagnose and resolve such issues.
1. Check System Memory
Start by verifying memory usage:
free -h
Look at:
- available memory
- swap usage
If swap is heavily used, the system is already under pressure.
To identify memory-heavy processes:
top
Or for a better view:
htop
Sort by memory usage and identify suspicious processes.
2. Check Disk Usage
Low disk space often causes freezing or application failures.
df -h
Focus on:
- /
- /var
- /home
If usage is above 85%, cleanup is recommended.
3. Clear System Cache
Linux aggressively caches memory. This is good — until it isn’t.
To clear cache safely:
sudo sync
sudo sysctl -w vm.drop_caches=3
This clears:
- Page cache
- Dentries
- Inodes
4. Check Docker Usage
Docker is often the biggest silent disk consumer.
Check Docker space usage:
docker system df
This shows:
- Images
- Containers
- Volumes
- Build cache
5. Remove Unused Docker Data
Remove stopped containers
docker container prune
Remove unused images
docker image prune -a
Remove unused volumes
docker volume prune
Remove build cache
docker builder prune
Full cleanup (safe but aggressive)
docker system prune -a --volumes
6. Check Large Directories
Find what’s consuming disk space:
sudo du -h --max-depth=1 /
Drill into heavy directories like:
- /var/lib/docker
- /var/log
- /home
7. Clean Logs
Logs silently fill disks.
sudo journalctl --vacuum-time=7d
Remove old logs:
sudo rm -rf /var/log/*.gz
sudo rm -rf /var/log/*.[0-9]
8. Restart Memory-Heavy Services
Sometimes services leak memory.
Examples:
sudo systemctl restart docker
sudo systemctl restart postgresql
sudo systemctl restart nginx
9. Check OOM Events
System may be killing processes silently.
dmesg | grep -i oom
If present, system ran out of memory.
10. Optional: Add Temporary Swap (Emergency Only)
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
Verify:
free -h
This completes the Ubuntu troubleshooting baseline.