What should I do if the physical memory is not enough? ? ? How to set up a large swap space under centos9

When doing data analysis, in addition to insufficient CPU speed, the memory often explodes. Here is an introduction to how to use hard disk space to expand physical memory. Of course, the speed of the hard disk is much slower. If you want to use the hard disk to expand the capacity, it is recommended to use a higher-performance SSD disk.

To set or increase a large swap space under CentOS 9 system, you can follow the following steps:

Method 1: Use partition to create swap

  1. Create a new disk partition :

    • If you have extra hard disk space, you can create a new Linux swap type partition through tools such as fdisk.parted
  2. Format to swap type :

     

    bash

    mkswap /dev/your_new_partition

    This /dev/your_new_partitionshould be replaced with the actual path of your new partition, eg /dev/sda3.

  3. Activate swap partition :

    swapon /dev/your_new_partition

    This means that the entire disk is used as a swap partition. Of course, you can divide the disk into an independent partition yourself.

  4. Enable swap permanently :

    • Edit  /etc/fstab the file and add a line to the file to ensure that the swap partition is automatically mounted when the system starts:
       
      echo '/dev/your_new_partition none swap defaults 0 0' >> /etc/fstab

Method 2: Use files to create swap space

If you don't have an extra disk partition available, you can also create a large file and use it as swap space:

  1. Create swap file :

    fallocate -l SIZE_IN_BYTES /path/to/swapfile
    # 或者使用dd命令创建
    dd if=/dev/zero of=/path/to/swapfile bs=1M count=SIZE_IN_MB

    Replace SIZE_IN_BYTESor SIZE_IN_MBwith your desired swap size.

  2. Set swap file permissions :

    chmod 600 /path/to/swapfile
    
    #防止随意删除

  3. Format the file as swap type :

    mkswap /path/to/swapfile
  4. Activate swap file :

    swapon /path/to/swapfile

  5. Enable swap files permanently :

    • Edit the file similarly  /etc/fstab and add the following lines:
      /path/to/swapfile none swap defaults 0 0

Precautions:

  • Determine the size of the swap space based on your actual needs and system memory size. It is generally recommended that the swap space be at least twice the physical RAM, but more than 8 times the physical RAM is not recommended, especially for modern servers with large amounts of memory.
  • Before proceeding, please ensure there is sufficient disk space and back up important data before executing the above command.
  • For production environments, please adjust the swap size according to official documentation and best practices.

Guess you like

Origin blog.csdn.net/zrc_xiaoguo/article/details/135323088