Tutorial on setting up Linux Swap partition: close partition, create partition file, set up boot, etc.

888u

Last update at :2024-03-19,Edit by888u

Recently, this site has created some Swap partitions for several Linux VPSs. The effect is pretty good and the memory usage has been significantly reduced. So today I will record the process of creating Swap partitions in Linux, including closing the Swap partition and creating the Linux Swap file. , create Swap partitions and other common Linux Swap settings.

1. Introduction to Swap partition

What is Swap partition? Swap (i.e., swap partition) in Linux is similar to the virtual memory of Windows. When the memory is insufficient, a part of the hard disk virtual host is virtualized into memory to solve the problem of insufficient memory capacity.

What is the role of Swap partition? Its function is that when the memory is insufficient, the operating system first saves temporarily unused data in the memory to the swap virtual host on the hard disk, freeing up memory for other programs to run

2. Steps to create Swap partition

We can use the following command to view

free -m

As shown in the figure below, Mem represents the memory size (ram), and Swap represents the swap partition size:

If our Swap partition is set unreasonably (too small) and the physical memory of the VPS itself is not enough, then the Linux Swap usage will be very high, or even the entire Swap partition will be full. Generally speaking, according to The size of Linux memory is different. We can set the Swap partition size according to the following correspondence:

  • Memory <= 4g: Swap at least 4G
  • Memory 4~16G: Swap at least 8G
  • Memory 16G~64G: Swap at least 16G
  • Memory 64G~256G: Swap at least 32G
  • The following is an introduction to the steps for creating/adding Swap partitions for Linux VPS.

    1. Close the Swap partition

    First close the Swap partition and clear the default Swap partition:

    swapoff -a

    If you just plan to close the Swap partition, then jump directly to step 6 to modify the startup items.

    2. Create the Swap partition file

    Where/var/swapfile is the file location, bs*count is the file size. For example, the following command will create a 4G file:

    dd if=/dev/zero of=/var/swapfile bs=1M count=4096

    3. Format as Swap partition file

    mkswap /var/swapfile

    4. Enable Swap partition

    swapon /var/swapfile

    5. View new partitions

    free -m

    As shown in the figure below, the 4G Swap partition is successfully created:

    6. Set up startup

    The last step is to add startup. You only need to add this line of code to the file /etc/fstab (if you don’t know how to edit files on Linux, you can refer to: "Using vim to edit and create new files under Linux" ):

    /var/swapfile swap swap defaults 0 0

    As shown below:

    3. The role of Swap partition

    Here I will show you the role of Swap partition. At first, this VPS defaulted to 2GB memory + 2GB Swap. As a result, the memory usage was very high, and Swap was full. After changing it to 4GB Swap, I ran the same process, the memory usage is lower, and the Swap partition is not used at all:

    Recommended site search: free cloud server, Hong Kong server, special price space, foreign PHP host, domain name expiration query, rent server, webmaster IP, Korean independent server, free space in the United States, and registration-free CDN in mainland China

    Tutorial on setting up Linux Swap partition: close partition, create partition file, set up boot, etc.

    All copyrights belong to 888u unless special state
    取消
    微信二维码
    微信二维码
    支付宝二维码