Example of how to increase swap in CentOS7 system

Example of how to increase swap in CentOS7 system

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: sudo chmod 0600 /swap;

3. Format the file as a swap file: sudo mkswap /swap;

4. Enable the swap file: sudo swapon /swap; if you do not want to use swap, you can uninstall it with the swapoff command: sudo swapoff /swap;

5. If you want the swap file to be automatically loaded after the system starts, add a line to the /etc/fstab file: /swap swap swap sw 0 0 .
After adding swap, you can use the swapon -s or free -m command to view swap information.

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: sudo sysctl vm.swappiness=10 . This command is only valid for the current system and will be restored to the default value after the system is restarted. To make the setting permanent, add or change the corresponding setting line in /etc/sysctl.conf: vm.swappiness = 10 .

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:
  • How to add and delete swap partition in Centos7
  • Detailed explanation of adding swap partition in CentOS7 in Linux

<<:  Install mysql5.7 on Ubuntu 18.04

>>:  Detailed explanation of JavaScript object-oriented practice: encapsulation and dragging objects

Recommend

An article to help you understand jQuery animation

Table of contents 1. Control the display and hidi...

How to use TypeScript in Vue

introduction In recent years, the call for TypeSc...

MySQL 5.7.23 version installation tutorial and configuration method

It took me three hours to install MySQL myself. E...

Summary of relevant knowledge points of ajax in jQuery

Preface Students who learn JavaScript know that A...

A brief analysis of the function calling process under the ARM architecture

Table of contents 1. Background knowledge 1. Intr...

Five ways to implement inheritance in js

Borrowing Constructors The basic idea of ​​this t...

How to build php7 with docker custom image

First, perform a simple Docker installation. To c...

An article to deal with Mysql date and time functions

Table of contents Preface 1. Get the current time...

Solution to the problem of passing values ​​between html pages

The first time I used the essay, I felt quite awkw...

The whole process of developing a Google plug-in with vue+element

Simple function: Click the plug-in icon in the up...

Detailed tutorial for installing mysql 8.0.12 under Windows

This article shares with you a detailed tutorial ...

mysql installer community 8.0.12.0 installation graphic tutorial

This tutorial shares the installation of mysql in...