laitimes

Configure swap virtual memory in the Linux environment to enhance system performance

author:JasonTang

Abstract:The command line of the Linux system is often contacted, its performance is very powerful, and it is more convenient to use, but in the process of installing the Linux system, you need to have a certain computer knowledge, and the installation process is not as convenient as Window, so there will be certain variables, for example, sometimes it is found that there is no reasonable allocation of virtual memory swap. Swap virtual memory and physical memory are two different concepts of memory management. Virtual memory uses hard disk space to emulate physical memory in order to expand the available memory space. There is a certain relationship between virtual memory and physical memory, which is mainly manifested in address mapping, page swapping, and memory protection. Through reasonable memory management, the performance and stability of the system can be improved, and the user experience can be improved.

This topic describes how to configure swap virtual memory on a Linux system.

1. Access the Linux system

1. Log in to the Linux system

Configure swap virtual memory in the Linux environment to enhance system performance

2. Check the memory usage of the Linux system

Note: On the Linux system, you can use the top command to view the memory occupied by a process: VIRT, RES, and SHR, with VIRT representing virtual memory space, RES referring to the part mapped to physical memory, and SHR representing shared memory.

Configure swap virtual memory in the Linux environment to enhance system performance

2. Introduction to virtual memory

1. What is virtual memory

Description: Virtual memory is a technology that expands part of physical memory into disk space to provide more memory space and more efficient system performance. Virtual memory allows the system to move infrequently used memory pages to disk when physical memory is running low, freeing up physical memory for active processes.

Configure swap virtual memory in the Linux environment to enhance system performance

2.What is Swap

Note: Swap is a virtual memory technology in Linux that is used to assist in the management of physical memory. When physical memory is insufficient, Swap provides a way to swap some of the memory content onto the hard disk to free up physical memory for use by other processes.

When the physical memory is insufficient, inactive processes are temporarily stored in the swap area. This process is reloaded from the swap to memory when it is needed, otherwise it will not be actively swapped into physical memory.

Configure swap virtual memory in the Linux environment to enhance system performance

3. Set up virtual memory

1. Swap configuration command

Execute the # swapon -s command to display summary information of the currently enabled swap partitions and swap files, including paths, sizes, and usage.

Configure swap virtual memory in the Linux environment to enhance system performance

2. Swap configuration swap file

执行指令# dd if=/dev/zero of=/opt/swapdata bs=1024 count=16777216使用dd命令创建一个16GB大小的交换文件。

Configure swap virtual memory in the Linux environment to enhance system performance

Note: The parameters for using the dd tool to create a swap file are described below:

Configure swap virtual memory in the Linux environment to enhance system performance

3. View the swapdata swap file

Run the # ls -al /opt/swapdata command to check the created swap file

Configure swap virtual memory in the Linux environment to enhance system performance

4. Set the permissions of swapdata to swap files

执行指令# chown root:root /opt/swapdata # 交换文件的所有者和所属组

执行指令# chmod 600 /opt/swapdata #只有root用户能读写

Configure swap virtual memory in the Linux environment to enhance system performance

5. Format the swapdata swap file

执行指令# mkswap /opt/swapdata格式化交换文件

Configure swap virtual memory in the Linux environment to enhance system performance

6. Enable swapdata to swap files

执行指令# swapon /opt/swapdata启用交换文件

Configure swap virtual memory in the Linux environment to enhance system performance

7. Verify the swap partition

Run the # swapon -s command to check the swap partition again

Configure swap virtual memory in the Linux environment to enhance system performance

8. Set up automatic mounting of Swap

Run the # vim /etc/fstab command to modify the fstab file and add the following contents:

/opt/swapdata swap swap defaults 0 0

9. Set the usage level of the swap partition

执行指令# cat /proc/sys/vm/swappiness 查看swap使用等级

Configure swap virtual memory in the Linux environment to enhance system performance

Note: This parameter ranges from 0 to 100. 0 is to maximize the use of memory, try not to use swap. 100 is actively using swap. The higher it is, the more the swap partition will be used. A general recommended value of 30 is used.

Configure swap virtual memory in the Linux environment to enhance system performance

10. Turn off the virtual memory (If you don't want to use the virtual memory, you can execute the instruction to turn it off.) )

Configure swap virtual memory in the Linux environment to enhance system performance

Read on