Thursday, November 10, 2022

.::: How to Flush Clear RAM Memory Cache, Buffer and Swap Space on Linux :::.

1. Clear PageCache only.

# sync; echo 1 > /proc/sys/vm/drop_caches

2. Clear dentries and inodes.

# sync; echo 2 > /proc/sys/vm/drop_caches

3. Clear pagecache, dentries, and inodes.

# sync; echo 3 > /proc/sys/vm/drop_caches

4. if create script

echo "Clear PageCache only "
sync; echo 1 > /proc/sys/vm/drop_caches
free -h

echo "Clear dentries and inodes "
sync; echo 2 > /proc/sys/vm/drop_caches
free -h

echo "Clear PageCache only "
sync;echo 3 > /proc/sys/vm/drop_caches
free -h

#flush swap
cat /proc/sys/vm/swappiness
sysctl vm.swappiness=1
swapoff -av
swapon -av
htop

5. check script
[root@teguhth data]# cat /data/clearswaplinux.sh
echo "Clear PageCache only "
sync; echo 1 > /proc/sys/vm/drop_caches
free -h
# usually using echo1

echo "Clear dentries and inodes "
#sync; echo 2 > /proc/sys/vm/drop_caches
free -h

echo "Clear PageCache only "
#sync;echo 3 > /proc/sys/vm/drop_caches
free -h

#no recomencation echo 2 & 3 in production

#flush swap
cat /proc/sys/vm/swappiness
sysctl vm.swappiness=1
swapoff -av
swapon -av
htop

# tutorial https://www.tecmint.com/clear-ram-memory-cache-buffer-and-swap-space-on-linux/
[root@teguhth data]#


6. Testing cek free space

[root@teguhth data]# free -h
              total        used        free      shared  buff/cache   available
Mem:           972M        260M        130M        7.7M        581M        556M
Swap:          2.0G          8K        2.0G
[root@teguhth data]# df -k
Filesystem              1K-blocks    Used Available Use% Mounted on
devtmpfs                   485824       0    485824   0% /dev
tmpfs                      497836       0    497836   0% /dev/shm
tmpfs                      497836    7928    489908   2% /run
tmpfs                      497836       0    497836   0% /sys/fs/cgroup
/dev/mapper/centos-root  23049220 2113932  20935288  10% /
/dev/sda1                 1038336  140320    898016  14% /boot
tmpfs                       99568       0     99568   0% /run/user/0
[root@teguhth data]# df -l
Filesystem              1K-blocks    Used Available Use% Mounted on
devtmpfs                   485824       0    485824   0% /dev
tmpfs                      497836       0    497836   0% /dev/shm
tmpfs                      497836    7928    489908   2% /run
tmpfs                      497836       0    497836   0% /sys/fs/cgroup
/dev/mapper/centos-root  23049220 2113932  20935288  10% /
/dev/sda1                 1038336  140320    898016  14% /boot
tmpfs                       99568       0     99568   0% /run/user/0
[root@teguhth data]#

 

7. drop example database or other

MariaDB [test]> drop database test;
Query OK, 5 rows affected (0.018 sec)

MariaDB [(none)]> drop database test1;
Query OK, 5 rows affected (0.008 sec)

MariaDB [(none)]> drop database test2;
Query OK, 5 rows affected (0.013 sec)

MariaDB [(none)]> drop database test3;
Query OK, 5 rows affected (0.012 sec)

MariaDB [(none)]>


8. check again
[root@teguhth data]# free -h
              total        used        free      shared  buff/cache   available
Mem:           972M        260M        130M        7.7M        581M        556M
Swap:          2.0G          8K        2.0G
[root@teguhth data]# df -k
Filesystem              1K-blocks    Used Available Use% Mounted on
devtmpfs                   485824       0    485824   0% /dev
tmpfs                      497836       0    497836   0% /dev/shm
tmpfs                      497836    7928    489908   2% /run
tmpfs                      497836       0    497836   0% /sys/fs/cgroup
/dev/mapper/centos-root  23049220 2112316  20936904  10% /
/dev/sda1                 1038336  140320    898016  14% /boot
tmpfs                       99568       0     99568   0% /run/user/0
[root@teguhth data]# df -l
Filesystem              1K-blocks    Used Available Use% Mounted on
devtmpfs                   485824       0    485824   0% /dev
tmpfs                      497836       0    497836   0% /dev/shm
tmpfs                      497836    7928    489908   2% /run
tmpfs                      497836       0    497836   0% /sys/fs/cgroup
/dev/mapper/centos-root  23049220 2112316  20936904  10% /
/dev/sda1                 1038336  140320    898016  14% /boot
tmpfs                       99568       0     99568   0% /run/user/0
[root@teguhth data]#

9. run script

[root@teguhth data]# sh clearswaplinux.sh
Clear PageCache only
              total        used        free      shared  buff/cache   available
Mem:           972M        258M        626M        7.7M         86M        596M
Swap:          2.0G          8K        2.0G
Clear dentries and inodes
              total        used        free      shared  buff/cache   available
Mem:           972M        259M        626M        7.7M         86M        596M
Swap:          2.0G          8K        2.0G
Clear PageCache only
              total        used        free      shared  buff/cache   available
Mem:           972M        259M        626M        7.7M         86M        596M
Swap:          2.0G          8K        2.0G
30
vm.swappiness = 1
swapoff /dev/dm-1
swapon /dev/mapper/centos-swap
swapon: /dev/mapper/centos-swap: found swap signature: version 1, page-size 4, same byte order
swapon: /dev/mapper/centos-swap: pagesize=4096, swapsize=2147483648, devsize=2147483648
[root@teguhth data]#

 


10. check after swap
[root@teguhth data]# free -h
              total        used        free      shared  buff/cache   available
Mem:           972M        256M        621M        7.7M         93M        594M
Swap:          2.0G          0B        2.0G
[root@teguhth data]# df -k
Filesystem              1K-blocks    Used Available Use% Mounted on
devtmpfs                   485824       0    485824   0% /dev
tmpfs                      497836       0    497836   0% /dev/shm
tmpfs                      497836    7928    489908   2% /run
tmpfs                      497836       0    497836   0% /sys/fs/cgroup
/dev/mapper/centos-root  23049220 2112324  20936896  10% /
/dev/sda1                 1038336  140320    898016  14% /boot
tmpfs                       99568       0     99568   0% /run/user/0
[root@teguhth data]# df -l
Filesystem              1K-blocks    Used Available Use% Mounted on
devtmpfs                   485824       0    485824   0% /dev
tmpfs                      497836       0    497836   0% /dev/shm
tmpfs                      497836    7928    489908   2% /run
tmpfs                      497836       0    497836   0% /sys/fs/cgroup
/dev/mapper/centos-root  23049220 2112324  20936896  10% /
/dev/sda1                 1038336  140320    898016  14% /boot
tmpfs                       99568       0     99568   0% /run/user/0
[root@teguhth data]#

 

No comments:

Post a Comment

Popular Posts