[Linux] Ubuntu increases the size of swap area

Linux swap area Swap

When the physical memory of the system is not enough, part of the space in the physical memory needs to be released for use by the currently running program. The freed space may come from some programs that have not operated for a long time. The freed space is temporarily saved in the swap space. When the programs are running, the saved data is restored from the swap partition to the memory. In this way, the system always performs swap when the physical memory is insufficient.

problem appear

  • The system is too slow

Increase the swap area size

  • Check the size of the swap area:
    free -m
  • Enter the root user (very important, it will be troublesome not to continue)
    sudo su - root
  • Create a directory:
    mkdir /swap
  • Enter the catalog
    cd /swap
  • Enter the following command (increase swap space):
    dd if=/dev/zero of=swapfile bs=1024 count=10290000
    a. Successful execution, you can see the following:
      Record 1029000+0 read
      Recorded 1029000+0 write out
      1053696000 bytes (1.1 GB) copied, 69.0168 seconds, 15.3 MB/sec
    b. Enter ls -lh to see a file.
      -rw-r–r-- 1 root root 1005M 2010-01-02 14:35 swapfile
  • Configuration
    mkswap swapfile
  • Mount the swap area:
    swapon swapfile
  • Check the size of the swap area
    free -m

There may be a problem

  • Insecure permissions 0644, 0600 is recommended (modify permissions)
    chmod 0600 /swap/swapfile
  • swapon failed: device or resource is busy (swap file activated)
    swapoff /swap/swapfile
    swapon /swap/swapfile
  • The swap area restores to the default after restart
    Command: sudo vim /etc/fstab
    Add: /swap/swapfile /swap swap defaults 0 0
    Save:wq

Guess you like

Origin blog.csdn.net/m0_46537958/article/details/108469587
Recommended