Preface Swap is a special file (or partition) located on the disk that is part of "virtual memory". In simple terms, it is a spare tire of memory. When there is sufficient memory, there is basically no need for swap (it depends on the settings); when the memory is insufficient, the system will move part of the data in the memory to swap to free up memory for running programs. Using swap allows the system to run more or larger programs, but because the read and write speed of swap is much slower than that of memory, frequent use of swap may cause delays and lags in the system and programs. Is swap necessary for computers with large memory? For computers with small memory, swap is very necessary. The existence of swap makes it possible to execute memory-intensive programs. Slow execution is better than not being able to run or crashing. However, modern computers often have more than 8G of memory, which is enough for daily use unless running large or professional software. In this case, is swap necessary? The answer depends on the usage scenario. If you run the desktop version of Linux on your personal computer and turn on the hibernation function, you should create a swap partition that is no smaller than the memory. If it is a server with enough memory and is not shut down all year round, there is no problem without swap. Should I set up swap for an SSD hard drive? Previous SSD hard drives had problems with read and write lifespan, so it is not recommended to place swap files or partitions on SSD hard drives. The current SSD hard drive technology has been upgraded and become more intelligent. If there is a swap requirement, there is no problem in putting it on an SSD hard drive. How big should the swap setting be? If the hibernation function is enabled, a swap partition should be created and the partition size cannot be smaller than the memory. The recommended value is "memory size + 2G". For servers that do not use the hibernation function, swap can be a file. A practical method to determine the swap size is: for 1G memory or less, the swap size is twice the memory; for 2-4G memory, the swap size is the same as the memory; for 5-16G memory, the swap can be fixed to 4G; for 17-32G memory, 8G swap; for 33G and above, the swap size is fixed to 16G. The above method is not absolute. If there are special needs, the swap size can be increased or decreased as appropriate. How to increase swap? When installing the operating system, you can specify swap in the form of partitions (separate mount points), or you can dynamically add and delete it after the system is installed. View swap The free command can view the swap information of system activities, for example: $ free -m # Output is as follows # total used free shared buff/cache available # Mem: 7976 4979 328 124 2669 2703 # Swap: 0 0 0 The second line is the swap information, and you can see that there is no active swap. You can also use the swapon command to view active swap, for example: $ swapon -s # No swap information, so no output # Output with swap is # Filename Type Size Used Priority # /swap file 2097148 281052 -2 Add swap Suppose we plan to run a program that takes up a lot of memory, and 8G physical memory is not enough, we can use swap to increase the virtual memory available to the system. Here’s how to do it: 1. Select or create a file as swap (partition can also be used). Common commands for creating files of specified size are fallocate and dd. For example, to create a file of 8G: # fallocate sudo fallocate -l 8G /swap #dd sudo dd bs=1GB count=8 if=/dev/zero of=/swap Since dd has a slow copying process, it is recommended to use fallocate in practice; 2. The swap file will store the data in the memory. For security reasons, the access of other users should be restricted: 3. Format the file as a swap file: 4. Enable the swap file: 5. If you want the swap file to be automatically loaded after the system starts, add a line to the /etc/fstab file: swap related settings The setting parameter that is most directly related to swap and affects system performance is vm.swappiness. Its value is an integer between 0 and 100, indicating the percentage of memory used up when swap is started. 100 means using swap as much as possible, and 0 means using swap as a backup only when physical memory is insufficient. For servers, between 10-30 is recommended. The command to set and change swappiness is: refer to https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-centos-7 Summarize The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. You may also be interested in:
|
<<: Install mysql5.7 on Ubuntu 18.04
>>: Detailed explanation of JavaScript object-oriented practice: encapsulation and dragging objects
Table of contents Overview What is lazy loading? ...
1. HTML Overview 1.HTML: Hypertext Markup Languag...
Index extension: InnoDB automatically extends eac...
123WORDPRESS.COM--HTML超文本标记语言速查手册<!-- --> !D...
Multiple values combined display Now we have th...
This article introduces 5 ways to solve the 1px b...
Table of contents 1.1. Enable MySQL binlog 1.2. C...
1. Download Download mysql-5.7.19-linux-glibc2.12...
Table of contents 1. Quickly recognize the concep...
Table of contents DOMContentLoaded and load What ...
1. To optimize the query, try to avoid full table...
What to do if you forget Windows Server 2008R2 So...
Jenkins configuration of user role permissions re...
Pre-installation work: Make sure vmware workstati...
Public name of the page: #wrapper - - The outer e...