[Persian Cat Memo] I tried using the AWS free tier! Adding swap space

table of contents
Hello!
I'm Inoue, a Persian cat from Beyond Co., Ltd.'s Shikoku office.
This time, I created a swap area on my AWS instance, so
I'm writing about it on my blog as a memo by a Persian cat for Persian cats.
I hope it will be helpful to you! (*'ω' *)
SWAP is a function that moves and temporarily stores data on disk when there is not enough memory.
Swap-out is a mechanism that saves part of memory to disk.
Swap-in is the process of returning swapped-out data to memory.
Memory can be compared to a computer desk.
If the desk is small, work slows down
because you have to clear away unused items and take out necessary ones.
A hard disk can be compared to a drawer on a computer desk.
Swap uses the hard disk like memory,
creating the illusion of having more memory than actual memory.
However, it takes longer to complete tasks using the hard disk than using memory.
Therefore, using swap slows down a computer.
Why does using swap slow down a computer?
First of all, data required for calculations by the CPU must be stored in the main memory (memory).
If your desk is small and cramped, you have no choice but to temporarily store data in drawers when working at the desk, so you
will frequently put data into drawers, increasing swap usage.
Also, you will want to have the data you want on the desk, so you will frequently take data out of drawers and onto the desk.
data is frequently being put into drawers (swapped out) and taken out (swapped in).
This frequent swapping and swapping is called thrashing.
Since swapping takes longer to process than memory,
the more swap space you use, the slower your computer will run.
The following are possible causes of SWAP:
1. Insufficient memory.
2. Memory fragmentation has progressed to the point where it is not possible to secure a large amount of memory.
Create a swap area
[root@test-aws-harukainoue var]# free -m total used free shared buff/cache available Mem: 983 226 75 0 681 596 Swap: 0 0 0
By default, you can see that there is no SWAP space and only memory is used
Let's create a swap area right away!
First, create a swap file to use as memory.
I create a directory called "swapfile" under /var and create a file called "swap".
[root@test-aws-harukainoue var]# mkdir swapfile [root@test-aws-harukainoue var]# ls -altr drwxr-xr-x 2 root root 6 Feb 2 04:52 swapfile [root@test-aws-harukainoue var]# cd swapfile [root@test-aws-harukainoue swapfile]# pwd /var/swapfile [root@test-aws-harukainoue swapfile]# dd if=/dev/zero of=/var/swapfile/swap bs=1M count=1024 1024+0 records in 1024+0 records out 1073741824 bytes (1.1 GB) copied, 13.6114 s, 78.9 MB/s [root@test-aws-harukainoue swapfile]# ls -altr -rw-r--r-- 1 root root 1073741824 Feb 11 08:40 swap [root@test-aws-harukainoue swapfile]# chmod 600 swap [root@test-aws-harukainoue swapfile]# ls -altr -rw------- 1 root root 1073741824 Feb 11 08:40 swap
To prevent accidentally editing the "swap" file, set the permissions to
600 *If the permissions are not set to "600", the following warning will be displayed.
mkswap: /swap: insecure permissions 0644, 0600 suggested.
Format the swap file for SWAP space
and activate the swap file.
[root@test-aws-harukainoue swapfile]# mkswap ./swap Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes) no label, UUID=8227fefa-a6da-4c63-a9c3-ae21c06dad30 [root@test-aws-harukainoue swapfile]# swapon ./swap [root@test-aws-harukainoue swapfile]# free -m total used free shared buff/cache available Mem: 983 227 73 0 682 594 Swap: 1023 0 1023 [root@test-aws-harukainoue swapfile]# swapon -s Filename Type Size Used Priority /var/swapfile/swap file 1048572 0 -2
The swap area has been created!!!
Set the SWAP area so that it does not disappear even after rebooting
Mount SWAP so that it can be used even after rebooting
[root@test-aws-harukainoue swapfile]# vi /etc/fstab
Write the following and overwrite it:
/var/swapfile/swap swap swap defaults 0 0 :wq
Reboot・・・・・・・・・
[root@test-aws-harukainoue ~]# free -m total used free shared buff/cache available Mem: 983 218 78 0 686 604 Swap: 1023 0 1023
You can see that SWAP is still being used after rebooting
I don't normally use SWAP in my AWS instance environment, so I turn it off.
*If you use SWAP, there is no need to turn it off.
[root@test-aws-harukainoue swapfile]# swapoff ./swap [root@test-aws-harukainoue swapfile]# free -m total used free shared buff/cache available Mem: 983 226 73 0 682 594 Swap: 0 0 0
summary
When memory runs out, the OOM killer will kill processes.
When memory is low,
using the temporary storage area SWAP as a temporary solution can alleviate some of the anxiety of running out of memory.
By using memory and SWAP wisely, you can prevent system hangs.
Don't forget to mount the SWAP you created, so you don't end up discovering that it has disappeared after rebooting!
Growing every day, progressing every day.
I have to update myself every day!!!
Thank you for reading to the end.
0