Recently, after adjusting my hard disk partitions, I found that my Ubuntu often froze, with the mouse pointer not responding promptly. After using free, I discovered that there was no SWAP information. It turned out that the partition modification caused the SWAP partition to become ineffective (my 2GB DDR struggled while downloading the Android source code, especially when using Google Chrome, a memory-hungry web browser). Ultimately, I found that the UUID did not match. By using blkid to obtain the UUID of the corresponding partition and then updating the relevant content in /etc/fstab, I resolved the issue. Since the SWAP partition was the cause, we should further understand it to avoid making the same mistake again. So, what is SWAP? What is its function? How do we operate it in Linux? … A series of questions kept popping up, so let’s explore this “troublemaker” that caused my machine to freeze.
In modern operating systems, there is a technology called virtual memory, which allows programs to exceed the limitations of physical memory and operate in a space much larger than the physical memory size. In Linux systems, there is a SWAP partition based on this concept. When physical memory is insufficient, it will use a certain algorithm to decide which less-used spaces to swap to the hard disk’s SWAP partition. When these spaces are needed again, they are swapped back into memory for access. Of course, the swapped spaces can still be accessed later; if they are not accessible, they will be synchronized to the corresponding hard disk space when not in use. There are two concepts here: SWAP IN, which transfers the page content from the SWAP partition to physical memory, and SWAP OUT, which is the opposite concept (viewed from the perspective of memory).
So, is a SWAP partition necessary? It is not absolute; when your physical memory is large enough (from a non-commercial usage perspective), it is completely unnecessary. However, in general, a SWAP partition can significantly save hardware costs, so from this perspective, it is still needed. If you do not have a SWAP partition, what impact will it have on the system? The most obvious effect is that when memory is nearly exhausted, there is no place to swap memory pages, and the entire memory may become full. At this point, as data continues to be moved into memory, it can cause the system to freeze, data errors, and system deadlocks, ultimately requiring a forced restart. From this perspective, if a SWAP partition is used, it can also become fully utilized. So how should we allocate SWAP partition space to avoid waste while ensuring sufficient usage?
<span>Red Hat has the following recommendations for its product <strong>Red Hat Enterprise Linux</strong> <strong>5</strong>:</span>
<span><strong>Systems</strong> with <strong>4</strong>GB of RAM or less require a minimum of <strong>2</strong>GB of swap space</span><span><strong>Systems</strong> with <strong>4</strong>GB to <strong>16</strong>GB of RAM require a minimum of <strong>4</strong>GB of swap space</span><span><strong>Systems</strong> with <strong>16</strong>GB to <strong>64</strong>GB of RAM require a minimum of <strong>8</strong>GB of swap space</span><span><strong>Systems</strong> with <strong>64</strong>GB to <strong>256</strong>GB of RAM require a minimum of <strong>16</strong>GB of swap space</span>
That is, systems with <=4GB of RAM require at least 2GB of swap space, 4GB-16GB of RAM require at least 4GB of swap space, 16GB-64GB of RAM require at least 8GB of swap space, and 64GB-256GB of RAM require at least 16GB of swap space.
Generally, during the installation process, a partition can be allocated as SWAP. If no SWAP partition was created during installation, can we still allocate one in the system? The answer is yes. A SWAP space can be either a partition, a file, or a combination of both. So how do we confirm, allocate, and load SWAP space in the system?
In Linux, we can use commands like swap -s, cat /proc/swaps, or free to check if the SWAP partition is enabled. If it is, the output will be as follows:
xinu@slam:~$ swapon -sFilename Type Size Used Priority/dev/sdb5 partition 7813488 934508 -1
xinu@slam:~$ cat /proc/swaps Filename Type Size Used Priority/dev/sdb5 partition 7813488 934060 -1
xinu@slam:~$ free total used free shared buffers cachedMem: 2049708 1766888 282820 0 4060 158540-/+ buffers/cache: 1604288 445420Swap: 7813488 933724 6879764
So, if there is no such content, we need to allocate space in the system for SWAP space. How do we do this? (The following operations require root user permissions)
1. When using a disk partition as SWAP space
Use the fdisk command to partition the disk (assuming my entire disk has only one partition for Linux and there is unallocated space). Enter the command interface, type n to create a new partition, then type t to enter the modify ID interface, change it to 82 (Linux swap type), and finally type w to save the changes and update the partition table. After partitioning, the next step is to format it using the mkswap /dev/sda2 command (where sda2 is my second hard disk partition, which should be filled in according to the actual situation). Next, use swapon /dev/sda2 to enable the newly created SWAP partition, but this is only temporary; you will need to run the swapon command every time you start the system. We can modify the /etc/fstab file to add the line /dev/sda2 swap swap defaults 0 0 to enable this swap space automatically at system startup.
2. When using a file as SWAP space
Use the dd command to create a 1GB (please adjust according to actual needs) file to serve as the swap partition. The corresponding command is as follows:
dd if=/dev/zero of=/home/xinu/swapfile bs=1M count=1024
The file size is calculated as bs*count, where the block size bs is 1MB, and the number of blocks is 1024. The swapfile name can be chosen freely.
Once the file for the swap partition is created, it still needs to be formatted. The corresponding command is as follows:
mkswap /home/xinu/swapfile
Then use swapon /home/xinu/swapfile to enable the SWAP partition. Similarly, we need to modify the /etc/fstab file to add the following line:
/home/xinu/swapfile swap swap defaults 0 0
Thus, we have gone through the entire function of the SWAP space and its usage in Linux, understanding the relevant content.