Lewati ke isi

intro SWAP

Swap (memory swap) adalah area di luar memori utama (mis: di harddisk atau di media seperti USB flash disk, dll) yang digunakan oleh OS untuk memperbesar kapasitas memori virtual (memori total), sehingga kita dapat menjalankan lebih banyak program/proses dalam waktu bersamaan, dan [sebagian] memori yang dipakai program

script auto swap download di sini

Swap Simple swap setup script for Linux

Swap is an area on a hard drive that has been designated as a place where the operating system can temporarily store data that it can no longer hold in RAM.

Disclamer: This script may not work on every GNU/Linux distro. Sorry.

Usage First of all, download the main script:

sh swap <size>

Example (with 4G):

sh swap 4G The default path for the swap file is /swapfile. If you wish to change this, simple the file location (file must not exist) add it to the command:

sh swap 4G /swap
  • manual mode ↓ ↓

Checking the System for Swap Information

sudo swapon --show
free -h

Checking Available Space on the Hard Drive Partition

df -h

Creating a Swap File

sudo fallocate -l 1G /swapfile

We can verify that the correct amount of space was reserved by typing:

ls -lh /swapfile
-rw-r--r-- 1 root root 1.0G Apr 25 11:14 /swapfile

Enabling the Swap File

sudo chmod 600 /swapfile
ls -lh /swapfile
Output
-rw------- 1 root root 1.0G Apr 25 11:14 /swapfile

We can now mark the file as swap space by typing:

sudo mkswap /swapfile
Output
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=6e965805-2ab9-450f-aed6-577e74089dbf

After marking the file, we can enable the swap file, allowing our system to start using it:

sudo swapon /swapfile

Verify that the swap is available by typing:

sudo swapon --show
Output
NAME      TYPE  SIZE USED PRIO
/swapfile file 1024M   0B   -2

We can check the output of the free utility again to corroborate our findings:

free -h
Output
              total        used        free      shared  buff/cache   available
Mem:          981Mi       123Mi       644Mi       0.0Ki       213Mi       714Mi
Swap:         1.0Gi          0B       1.0Gi

Making the Swap File Permanent

Back up the /etc/fstab file in case anything goes wrong:

sudo cp /etc/fstab /etc/fstab.bak

Add the swap file information to the end of your /etc/fstab file by typing:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Tuning your Swap Settings

  • Adjusting the Swappiness Property

  • apa perbedaan swappiness = 0 dengan swappiness =60 lalu ap dampak ny

Swappiness adalah suatu parameter pada sistem operasi Linux yang menentukan seberapa sering kernel akan memindahkan data dari RAM ke swap space (partisi swap pada hard disk) untuk mengelola penggunaan memori.

Swappiness = 0 berarti kernel hanya akan memindahkan data dari RAM ke swap space ketika tidak ada pilihan lain untuk mempertahankan penggunaan memori yang efektif. Ini berarti bahwa lebih banyak data akan tetap berada di RAM, sehingga membuat RAM lebih efisien dan memungkinkan aplikasi untuk berjalan lebih cepat karena akses ke data yang lebih cepat. Namun, jika jumlah memori fisik yang tersedia terbatas, dapat menyebabkan aplikasi crash atau sistem menjadi tidak responsif.

Swappiness = 60 adalah nilai default pada kebanyakan distribusi Linux, yang berarti bahwa kernel akan mulai memindahkan data dari RAM ke swap space ketika penggunaan RAM mencapai 40%. Ini memberikan keseimbangan antara penggunaan RAM dan swap space, yang membuat sistem operasi Linux lebih stabil dalam situasi ketika terjadi kekurangan memori.

Dampak dari pengaturan nilai swappiness ini bergantung pada kondisi dan penggunaan sistem operasi Linux. Jika sistem memiliki banyak RAM, menetapkan nilai swappiness ke 0 mungkin akan meningkatkan performa aplikasi karena data akan tetap berada di RAM. Namun, jika sistem memiliki sedikit RAM, menetapkan nilai swappiness ke 0 dapat menyebabkan sistem menjadi tidak responsif atau crash ketika RAM habis. Sebaliknya, jika sistem sedang digunakan secara intensif, menetapkan nilai swappiness ke 60 mungkin akan mengurangi kinerja karena kernel lebih sering memindahkan data ke swap space. Oleh karena itu, sangat disarankan untuk menyesuaikan nilai swappiness berdasarkan kebutuhan dan karakteristik sistem yang digunakan.

We can see the current swappiness value by typing:

cat /proc/sys/vm/swappiness
Output
60

For a Desktop, a swappiness setting of 60 is not a bad value. For a server, you might want to move it closer to 0.

We can set the swappiness to a different value by using the sysctl command.

For instance, to set the swappiness to 10, we could type:

sudo sysctl vm.swappiness=10
Output
vm.swappiness = 10

This setting will persist until the next reboot. We can set this value automatically at restart by adding the line to our /etc/sysctl.conf file:

sudo nano /etc/sysctl.conf

At the bottom, you can add:

vm.swappiness=10

Save and close the file when you are finished.

Adjusting the Cache Pressure Setting

  • apa itu Cache Pressure Setting

Cache pressure setting adalah parameter pada sistem operasi Linux yang menentukan seberapa cepat kernel akan membebaskan cache page (halaman cache) dari memori ketika RAM mulai penuh. Cache page adalah bagian dari cache kernel yang digunakan untuk menyimpan data yang sering digunakan oleh aplikasi, sehingga dapat dengan cepat diakses jika dibutuhkan.

Nilai cache pressure setting dapat disesuaikan dengan mengubah nilai parameter vm.vfs_cache_pressure pada file konfigurasi /etc/sysctl.conf. Nilai defaultnya adalah 100, yang berarti kernel akan lebih cenderung mempertahankan cache page di RAM dan membebaskan cache page hanya ketika RAM benar-benar penuh.

Jika cache pressure setting dinaikkan, kernel akan lebih agresif dalam membebaskan cache page dari RAM ketika RAM mulai penuh, sehingga dapat memberikan lebih banyak ruang untuk aplikasi. Namun, hal ini dapat mempengaruhi kinerja aplikasi yang sering mengakses cache page, karena cache page harus di-load kembali ke RAM setiap kali aplikasi membutuhkannya.

Di sisi lain, jika cache pressure setting diturunkan, kernel akan lebih lambat dalam membebaskan cache page dari RAM dan akan lebih cenderung mempertahankan cache page di RAM. Ini dapat mempercepat kinerja aplikasi yang sering mengakses cache page, tetapi pada saat yang sama juga membatasi jumlah RAM yang tersedia untuk aplikasi.

Sebaiknya, nilai cache pressure setting disesuaikan dengan kebutuhan sistem dan jenis aplikasi yang digunakan. Nilai yang terlalu tinggi atau terlalu rendah dapat mempengaruhi kinerja aplikasi dan penggunaan memori pada sistem.

Another related value that you might want to modify is the vfs_cache_pressure. This setting configures how much the system will choose to cache inode and dentry information over other data.

Basically, this is access data about the filesystem. This is generally very costly to look up and very frequently requested, so it’s an excellent thing for your system to cache. You can see the current value by querying the proc filesystem again:

cat /proc/sys/vm/vfs_cache_pressure
Output
100

As it is currently configured, our system removes inode information from the cache too quickly. We can set this to a more conservative setting like 50 by typing:

sudo sysctl vm.vfs_cache_pressure=50
Output
vm.vfs_cache_pressure = 50

Again, this is only valid for our current session. We can change that by adding it to our configuration file like we did with our swappiness setting:

sudo nano /etc/sysctl.conf

At the bottom, add the line that specifies your new value:

vm.vfs_cache_pressure=50

Save and close the file when you are finished.

Conclusion

Following the steps in this guide will give you some breathing room in cases that would otherwise lead to out-of-memory exceptions. Swap space can be incredibly useful in avoiding some of these common problems.

If you are running into OOM (out of memory) errors, or if you find that your system is unable to use the applications you need, the best solution is to optimize your application configurations or upgrade your server.