What is Linux cache?
Linux uses spare memory (RAM) to keep recently used files. This is the page cache. It makes things faster. It is normal for RAM to look full. Linux gives that memory back the moment a program needs it. So you rarely need to clear it. Do it for testing, or if you have a clear reason.
Root is the all-powerful admin account. Sudo means "run this command as the admin".
Check memory first
free -h
This shows memory in easy units. Look at the available column. That is what programs can really use.
Clear the RAM cache
- Write any unsaved data to disk.
sync - Drop the page cache.
echo 1 | sudo tee /proc/sys/vm/drop_caches - To drop dentries and inodes (file name info), use 2. To drop all three, use 3.
echo 3 | sudo tee /proc/sys/vm/drop_caches
This is safe for data, but your server may run slower for a short time while the cache refills.
Clear swap
Swap is disk space used like extra memory when RAM is full. Move it back to RAM like this.
- Check that you have enough free RAM first with
free -h. - Turn swap off, then on again.
sudo swapoff -a && sudo swapon -a
swapoff can freeze the server or kill programs. Do not run it in that case.Clear temp files and package cache
- List the size of the temp folder.
du -sh /tmp - Delete old files in
/tmpthat are over 7 days old. Take a backup first if unsure.sudo find /tmp -type f -atime +7 -delete - On Ubuntu or Debian, clear downloaded package files.
sudo apt clean - On CentOS or AlmaLinux, use
sudo yum clean allorsudo dnf clean all.
Quick recap
- A full-looking RAM is normal in Linux.
- Run
sync, then write todrop_cachesto clear page cache. - Use
swapoff -aandswapon -aonly if RAM is free. - Clean
/tmpand package caches to free disk space.