Operations 6 min read

How to Safely Clear Linux RAM Cache and Swap Space (Step‑by‑Step Guide)

This article explains why and how to clear Linux RAM cache and swap space using built‑in kernel interfaces, provides three safe echo commands, shows how to automate the process with a cron‑scheduled shell script, and warns against unsafe production usage.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Safely Clear Linux RAM Cache and Swap Space (Step‑by‑Step Guide)

How to clear cache in Linux?

Linux provides three options to drop caches without stopping any processes or services. sync; echo 1 > /proc/sys/vm/drop_caches 1. Drop only page cache. sync; echo 2 > /proc/sys/vm/drop_caches 2. Drop dentries and inodes. sync; echo 3 > /proc/sys/vm/drop_caches 3. Drop page cache, dentries, and inodes.

The sync command flushes filesystem buffers; the echo writes to the kernel drop‑caches interface. Using option 1 is safest in production because it only clears page cache. Option 3 clears everything and can degrade performance if used indiscriminately.

Automating cache clearing with cron

Create a shell script clearcache.sh containing the desired command, for example:

#!/bin/bash
# Recommended: use echo 1 to clear only page cache
sync; echo 1 > /proc/sys/vm/drop_caches

Make the script executable: chmod 755 clearcache.sh Add a cron job to run it daily at 2 PM:

# crontab -e
0 14 * * * /path/to/clearcache.sh

Automatic cache clearing should be used only when you fully understand the workload; doing it blindly can cause a production server to reload data from disk and potentially crash under load.

How to clear Linux swap space?

To drop swap, run: swapoff -a && swapon -a You can combine cache and swap clearing in one script:

# Clear caches and swap safely
sync; echo 1 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf '
%s
' 'Ram-cache and Swap Cleared'

Before and after running the script, you can verify memory usage with free -m.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CacheSystem AdministrationSwap
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.