How to change the size of swapfile in Linux



A swap file is a file that contains data retrieved from system memory, or RAM. By transferring data from RAM to a secondary storage device in the form of a swap file, a computer is able to free up memory for other programs [1]. 

Sometimes, a large swap file can help when your RAM size is limited, e.g., training a large Deep Learning model on your laptop (although it is not a good idea...)

In Linux, we can follow the several steps in the following to change the swap size.

1. disable the current swap file

$ sudo swapoff /swapfile

2. increase (or decrease) the size of swap file (e.g., 8G as below)

$ sudo dd if=/dev/zero of=/swapfile bs=1G count=8

3. Setup the file as our swap file

$ sudo mkswap /swapfile

4. enable swaping

$ sudo swapon /swapfile

5. check the current swap size

$ grep SwapTotal /proc/meminfo


References

[1]. https://techterms.com/definition/swap_file

No comments:

Post a Comment