SWAP partition introduction and extension

When running the program on the Linux server, when the code is correct and the previous tasks are running normally, the task suddenly terminates. First, I checked the load, business process and related logs, and found no error log records. After checking the memory usage (free -m), I found that Swap free is 0, which means that the program terminated due to insufficient memory. This blog post will introduce what SWAP is and how to extend it.

1.SWAP Overview

In fact, in the early days, the memory was generally relatively small, and it was easy to encounter the problem of insufficient memory, so the concept of a swap partition was proposed very early.

The swap partition uses the disk as memory, so that the range size of the virtual address space can exceed the actual size of the physical memory. When the physical memory space is insufficient, some unimportant data in the physical memory can be copied to the swap partition of the disk. Thereby freeing up memory space, and when the data that has been copied out is needed, it is copied back to the memory from the swap partition. In other words, SWAP will be used only when the physical memory and buffer memory are not enough.

Only when the physical memory is exhausted or about to be exhausted, if the process continues to request memory allocation, an out-of-memory (OOM) error will be reported to indicate insufficient memory, and when OOM occurs, the operating system will trigger the OOM Killer program from Filter out a memory-intensive process from the process list and kill it, thereby freeing up a large amount of memory.

2. What is the appropriate size for SWAP partition setting?

Generally speaking, you can set the swap size according to the following rules (subject to the actual situation, this is just a suggestion)

For physical memory within 4G, SWAP is set to 2 times the memory.

4-8G physical memory, SWAP is equal to the memory size.

8-64G physical memory, SWAP is set to 8G.

64-256G physical memory, SWAP is set to 16G.

3. How much physical memory is used is not enough?

In fact, it is not necessary to wait until all the physical memory is consumed before using the swap space. Instead, the swappiness parameter value controls whether the physical memory is enough.

cat /proc/sys/vm/swappiness

The default value of the server I use is 60

When swappiness=0, it means that physical memory is used to the maximum extent, and then swap space is used.

When swappiness=100, it means actively using the swap partition and moving the data in the memory to the swap space in a timely manner.

4. Extend SWAP

View the top 10 process pids occupying SWAP

for i in $(cd /proc;ls | grep "^[0-9]"|awk '$0 >100');do awk '/Swap:/{a=a+$2}END{print "'$i'", a/1024"M"}' /proc/$i/smaps 2>/dev/null;done | sort -k2nr | head -10
4.1 View system block device information
lsblk

4.2 Check the swap partition size
free -h

You can see that the current server Swap partition size is 2G, 1.2G has been used, and the remaining amount is 730M.

4.3 Add 1G Swap partition
dd if=/dev/zero of=/swap2 bs=1M count=10240

Non-root users need sudo to enable temporary root permissions

A new directory (/swap2) will be created here, which is the new swap partition.

The meaning of each parameter:

dd: Copies the file in blocks of the specified size and performs the specified conversion while copying

if=: (input file) source of data (source)

of=: (outpu file) output file (target)

bs=: (block size) How big is the size of each copy (bytes)

count=:Number of times

/dev/zero white hole continuous output

/dev/null black hole absorption

/dev/urandom random characters

4.4 Update the file to swap type

Check the file types of the newly added partitions

file /swap2

Use the mkswap command to set the swap file, which is equivalent to formatting the newly created swap partition.

mkswap /swap2

4.5 Activate swap partition

To enable the swap file immediately rather than automatically at boot time, use the swapon command.

swapon /swap2

If you want to stop using the newly created swap partition, use the swapoff command.

Check the added swap situation. The added swap partition is 12238M.

free -m

4.6 Change the permissions of the new swap partition
chmod 600 /swap2
4.7 Check the composition of the swap partition
swapon -s

Reference link

Understanding swap caused by an alarm

How to extend swap partition under linux

http://t.csdn.cn/uEiGO

Guess you like

Origin blog.csdn.net/qq_42458954/article/details/132900939