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

table of contents
Hello!
I'm Inoue, the Persian cat at Beyond Shikoku office.
This time, I created a SWAP area on my own AWS instance, so
I'm writing this blog post as a memo by a Persian cat for Persian cats.
I hope it will be helpful to you all! (*'ω' *)
SWAP is a function that moves data to disk for temporary storage when memory is insufficient.
Swap-out is a mechanism that saves a portion of memory to disk.
Swap-in is the process of returning swapped-out data to memory.
Memory can be likened to a computer's workspace.
If the workspace is small, work will be slow,
because you have to put away things you're not using and take out things you do need.
The hard disk can be likened to a drawer on a computer's workspace.
Swapping uses the hard disk like memory,
creating the illusion that there is more memory than there actually is.
However, it takes longer to perform tasks using the hard disk than using memory.
Therefore, using swap slows down the computer's operation.
Why does using swap slow down a computer's operation?
First of all, the data necessary for calculations performed by the CPU must always be loaded into main memory.
When working on a small, cramped workspace, you have no choice but to temporarily put data into drawers, so you end up
putting data into drawers more frequently, increasing swap usage.
Also, you want to take the data you want to use out of the drawers and onto the desk, so you end up taking data out of the drawers more frequently.
you're frequently putting data into drawers (swapping out) and taking it out (swapping in).
This frequent swapping out and swapping in is called thrashing.
Swapping takes longer to process than memory, so
an increase in SWAP usage slows down the computer's performance.
The following are possible causes of SWAP:
① Insufficient memory.
② Memory fragmentation has progressed to the point where a large block of memory cannot be allocated.
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 get started and create the SWAP area!
First, we'll create a SWAP file to use as memory.
I'll create a directory called "swapfile" under /var and create a file called "swap" in it.
[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,600set the permissions to
*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 use as a SWAP area.
Then, enable 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.
(Note: If you do use SWAP, you don't 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 forcibly terminate processes.
as a temporary measure when memory is low
Using the temporary storage area SWAP
It's important to use memory and SWAP effectively to prevent system hangs.
Don't forget to mount the SWAP area to avoid situations where it disappears after a reboot!
Growing every day, moving forward every day.
I must update myself every single day!!!
Thank you for reading to the end.
0
