[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

[Persian cat memorandum] I tried it with AWS free frame! Add SWAP area

Hello!
This is Inoue, a Persian cat from Beyond Shikoku Office.

This time, I created a SWAP area on my AWS instance, so
I will write it down on my blog as a memorandum for Persian cats by Persian cats.
I would be happy if this could be helpful to you! (*'ω' *)

SWAP is a function that moves data to disk and temporarily saves it when memory is insufficient.
Swapout is a mechanism that saves part of memory to disk.
Swapping in means returning data that was swapped out to memory.

Memory is like a computer's work desk.
If your desk is small, your work will be slow.
Because you have to put away things you don't use and take out things you use.
A hard disk is, for example, a drawer on a computer's work desk.
Swap uses your hard disk as memory,
creating the illusion that you have more memory than you actually have.
However, it takes longer to complete tasks using the hard disk than using memory.
Therefore, using swap will slow down your computer.
Why does swap slow down my computer? ?
First of all, the data required to perform calculations on the CPU must be expanded to main storage (memory).
If your desk is small and narrow, and you work at your desk, you have no choice but to temporarily put the data in the drawer, so you
will have to put data in the drawer more often, increasing swap usage.
Also, since I want to put the data I want to use on my desk, the frequency of taking data from the drawer to the desk will increase.
You will be frequently putting data in (swap out) and taking data out (swap in) of the drawer.
This frequent repetition of swap-outs and swap-ins is called thrashing.
Swap takes longer to process than memory,
so increased SWAP usage slows down your computer. That's why.

Possible causes of SWAP are as follows:
① There is not enough memory.
②Memory fragmentation has progressed and it is not possible to secure a large amount of memory.

Let's 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

You can see that by default there is no SWAP space, only memory is used.

Let's create a SWAP area right away!
First, create a SWAP file to use as memory.
I prepare 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 avoid editing the "swap" file by mistake, 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.
Additionally, 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

SWAP area is now available! ! !

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 even after rebooting.

I usually don't 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 is exhausted, the OOM killer will kill the process.
as an emergency measure when you are running out of memory
, you can alleviate the anxiety of running out of memory.
I want to use memory and SWAP wisely to prevent system hangs.
The SWAP that was supposed to have been created disappeared after rebooting! Don't forget to mount it so this doesn't happen.

Growing every day, moving forward every day.
I have to update myself every day! ! !
Thank you for reading to the end.

If you found this article helpful , please give it a like!
0
Loading...
0 votes, average: 0.00 / 10
1,156
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

The person who wrote this article

About the author

Akika Inoue

Belongs to the System Solutions Department.
He joined Beyond as a founding member of the Shikoku office.
I jumped into the IT industry with no experience. As an education team, we create curriculum and conduct training for new graduates, mid-career, and existing members.
The main business is server operation and maintenance.
Either way, we value your content.
Also belongs to the Web Content Division and YouTube Team.