Hapus buff/cache Server Linux + Cronjob

Buffer ? : Buffering is the process of preloading data into a reserved area of memory called buffer memory. Buffer memory is a temporary storage area in the main memory (RAM) that stores data transferring between two or more devices or between an application and a device. Buffering compensates for the difference in transfer speeds between the sender and receiver of the data.

Systems automatically create buffers in the RAM whenever there are varying transmission speeds between applications or devices transferring data. The buffer accumulates the bytes of data received from the sender and serves it to the receiver when ready.

Cache ? : Caching is the process of temporarily storing a copy of a given resource so that subsequent requests to the same resource are processed faster.

Cache memory is a fast, static random access memory (SRAM) that a computer chip can access more efficiently than the standard dynamic random access memory (DRAM). It can exist in either RAM or a hard disk. Caching in RAM is referred to as memory caching, while caching in a hard disk is referred to as disk caching.

Disk caching is advantageous because cached data in the hard disk isn’t lost if the system crashes. However, data access in disk caching is slower in comparison to memory caching.

Whenever a program requests data from the hard disk, the system first checks the cache memory. It only checks the hard disk if the requested data isn’t present in the cache memory. This greatly improves data processing because accessing it directly from the hard disk is slower.

Hapus (Delete) Buffer / Cache

Masalah pada besaranya simpanan data buff, cache pada memory server VPS linux bisa membuat kinerja lambat dan ruang RAM menjadi berkurang, akhirnya solusi terbaik adalah hapus secara berkala dengan bantuan cronjob setiap 1 jam sekali.

Data shared buff/cache available normal:

root@***:~# free -hm
total used free shared buff/cache available
Mem: 1.9Gi 878Mi 941Mi 5.0Mi 151Mi 935Mi
Swap: 5.8Gi 76Mi 5.7Gi

Secara umum kadang penggunaan buff/cache bisa lebih besar hingga 4Gi – 10Gi, sehingga perlu tindakan hapus cache secara berkelanjutan.

READ :  Install VPN Free Cloudflare WARP Linux (Ubuntu)

OK, ini adalah kode hapus, delete semua buff/cache (clearing pagecache, dentries, and inodes)

sudo sh -c "echo 3 > /proc/sys/vm/drop_caches"

Atur Hapus Buffer / Cache Automatis

Selanjutnya seting penghapusan automatis cronjob:

masuk ssh sebagai root (wajib) :

crontab -e

Silahkan dipilih waktu cronjob mana yang cocok, apakah 1 menit, 5 menit atau 1 jam semua tergantung tinggi rendah traffic server  dan kebutuhan spesifikasi sistem.

Seting dan isi kode cronjob hapus berkala setiap 5 menit:

# hapus buffer / cache VPS linux 
*/5 * * * * sudo sh -c "echo 3 > /proc/sys/vm/drop_caches" >/dev/null 2>&1

Seting dan isi kode cronjob hapus berkala setiap 1 menit:

# hapus buffer / cache VPS linux 
* * * * * sudo sh -c "echo 3 > /proc/sys/vm/drop_caches" >/dev/null 2>&1

Seting dan isi kode cronjob hapus berkala setiap 1 jam:

# hapus buffer / cache VPS linux 
0 * * * * sudo sh -c "echo 3 > /proc/sys/vm/drop_caches" >/dev/null 2>&1

cek verifikasi log cronjob berjalan lancar

root@****:~# tail -f /var/log/syslog | grep CRON
Aug 19 23:58:01 ** CRON[2068880]: (root) CMD (sudo sh -c “echo 3 > /proc/sys/vm/drop_caches” >/dev/null 2>&1)
Aug 19 23:59:01 ma** CRON[2068898]: (root) CMD (sudo sh -c “echo 3 > /proc/sys/vm/drop_caches” >/dev/null 2>&1)
Aug 20 00:00:01 m*** CRON[2069063]: (root) CMD (sudo sh -c “echo 3 > /proc/sys/vm/drop_caches” >/dev/null 2>&1)

NOTE : pada ujicoba cronjob waktu di ubah pada setiap 1 menit, rekomendasi terbaik karena sistem buffer cache sangat diandalkan untuk kecepatan akses, load data sistem sehingga prakter terbaik bisa diseting cronjob 7 hari sekali atau 1 bulan sekali.

0 0 * * 0  (1 x dalam 7 hari) cocok untuk server linux

0 0 1 * * (1 x dalam 1 bulan) cocok untuk desktop linux