-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclear-cache.sh
executable file
·38 lines (32 loc) · 1.05 KB
/
clear-cache.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env bash
# Retrieve memory information using awk
MEM_FREE=$(awk '/^MemFree/ {print int($2/1024)}' /proc/meminfo)
MEM_TOTAL=$(awk '/^MemTotal/ {print int($2/1024)}' /proc/meminfo)
MEM_THRESHOLD=1024
# Check for root permissions or sudo availability
if ! [ "$(id -u)" -eq 0 ] && ! command -v sudo &>/dev/null; then
echo "Superuser privileges are required."
exit 1
fi
if [ "$MEM_FREE" -le "$MEM_THRESHOLD" ]; then
echo "Clearing PageCache, dentries, and inodes..."
if [ "$(id -u)" -eq 0 ]; then
sync
echo 3 >/proc/sys/vm/drop_caches
else
sudo sh -c 'sync; echo 3 > /proc/sys/vm/drop_caches'
fi
echo "Memory caches cleared."
echo "Deflating memory..."
echo 1 >/proc/sys/vm/compact_memory
echo "Memory deflated."
MEM_AFTER=$(awk '/^MemFree/ {print int($2/1024)}' /proc/meminfo)
echo "Total Memory: $MEM_TOTAL MB"
echo "Free Memory before: $MEM_FREE MB"
echo "Free Memory after: $MEM_AFTER MB"
echo
echo "Memory caches cleared and memory deflated successfully."
else
echo "No need to clear memory cache."
fi
exit 0