Ubuntu swap area extension

background

I installed Ubuntu 20.04 on a desktop PC (i5 (2400) / memory 8GB) that I bought at Dosupara when I was a college student and used it as my main PC for home use, but since I have been using it for over 8 years, I have been using it recently. The memory is dead. However, strictly speaking, the memory slot part of the motherboard died, not the memory. So, it's not a level that can be solved by replacing the memory, but a level that replaces the motherboard (it is better to buy a new PC than replacing the motherboard) Fortunately, 2 of the 4 memory slots are still alive and can be booted with 4GB of 2GB * 2 cards, so I decided to buy a new PC with a winter bonus and decided to endure it as it is until then. As expected, if it is 4GB, if you start the Docker container while starting Chrome, it will eat up to the Swap area and freeze, so I decided to expand the swap area for the time being.

That's why I decided to write this article about how to expand the Swap area after installing the OS, which also serves as a memorandum.

Create a special file for the Swap area

As with Windows, it seems that the Swap area can be realized with a dedicated system file. If it is Ubuntu 20.04, you can check it with the swapon command. In my environment, / swapfile already existed and it seemed to be recognized as a Swap area.

$ swapon -s
Filename Type Size Used Priority
/swapfile	file	2097148	1851152	-2

Since / swapfile already exists, create / swapfile2 and make it recognized as a Swap file.

$ sudo fallocate -l 2G /swapfile2   #Allocate a 2GB file
$ sudo chmod 600 /swapfile2         #Set to read / write only root
$ sudo mkswap /swapfile2            #Set as swap area
$ sudo swapon /swapfile2            #Mount as swap area
$ swapon -s
$ sudo vim /etc/fstab
$ grep swapfile /etc/fstab 
/swapfile                                none            swap    sw              0       0
/swapfile2                                none            swap    sw              0       0
$ reboot
$ swapon --show

The Swap area was successfully expanded. Now I can continue to use it without freezing.

Serpentine

I remembered that it was said in the past that the size of the Swap area should be twice the size of the real memory, but now the memory can be prepared at a fairly low price, and the server can be easily stored by simply adjusting the settings in the cloud. Since I was able to add more, I was completely unaware of the size of the Swap area, but I thought that it might be better to customize it instead of keeping the default value. Even so, modern browsers are too memory idiot. With 4GB of memory + 2GB of Swap, there is a danger of freezing just by using the browser. In Windows XP, I thought that 2GB of memory was enough luxury, but it was too inflationary.

Recommended Posts

Ubuntu swap area extension
Extend swap area on Ubuntu 18.04