Why is Linux’s buff/cache so large and how to clear it automatically
When running `free -h` on a Linux system, you may notice the buff/cache entry consuming over a gigabyte, leaving little memory for applications; this article explains that the cache is built from file I/O, shows how to manually drop it via `/proc/sys/vm/drop_caches`, and provides a cron‑based script to automate the cleanup.
Running free -h on a Linux host can reveal that the buff/cache line occupies more than 1 GB, which reduces the memory available for other processes.
The buff/cache value represents file data cached by the kernel as a result of normal read/write operations; it is not released automatically when memory becomes scarce.
A direct way to free this memory is to write specific values to /proc/sys/vm/drop_caches:
# Free pagecache only
echo 1 > /proc/sys/vm/drop_caches
# Free dentries and inodes
echo 2 > /proc/sys/vm/drop_caches
# Free pagecache, dentries and inodes together
echo 3 > /proc/sys/vm/drop_cachesExecuting the commands clears the cache, but the process must be repeated manually each time the issue reappears.
To automate the cleanup, create a shell script (e.g., cleanBuffCache.sh) and schedule it with cron:
#!/bin/bash
echo "Start cleaning cache"
# Ensure all pending writes are flushed
sync; sync; sync
# Wait a short period
sleep 10
# Drop caches
echo 1 > /proc/sys/vm/drop_caches
echo 2 > /proc/sys/vm/drop_caches
echo 3 > /proc/sys/vm/drop_caches
echo "Cache cleaning finished"Make the script executable: chmod 777 cleanBuffCache.sh Test it with: ./cleanBuffCache.sh Add a cron job to run the script hourly (or at any desired interval):
crontab -e
* 0 * * * /path/to/cleanBuffCache.shSave and exit the editor, then verify the entry with crontab -l. The scheduled task will periodically clear the kernel’s file cache, preventing buff/cache from growing excessively.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Coder Trainee
Experienced in Java and Python, we share and learn together. For submissions or collaborations, DM us.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
