How to create a swap partition file in Linux

How to create a swap partition file in Linux

Introduction to Swap

Swap (i.e. swap partition) in Linux is similar to virtual memory in Windows. When the memory is insufficient, part of the hard disk space is virtualized as memory to solve the problem of insufficient memory capacity.

How to create a Swap file

1. Create a file for exchange

sudo fallocate -l 1G /swapfile

If faillocate is not installed or you receive the error message fallocate failed: Operation not supported then you can create a swap file using the following command:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576

2. Set the correct permissions

Only the root user can write and read the swap file, to set the correct permission type:

sudo chmod 600 /swapfile

3. Set up Linux swap area

Use the mkswap utility to set up a file as a Linux swap area:

sudo mkswap /swapfile

4. Enable swap

Activate the swap file with the following command:

sudo swapon /swapfile

To make the changes permanent open /etc/fstab file and append the following line:

/swapfile swap swap defaults 0 0

5. Verify the exchange status

To verify that swap is active, we can use the swapon or free command as follows:

sudo swapon --show

Output

NAME TYPE SIZE USED PRIO
/swapfile file 1024M 507.4M -1
sudo free -h

Output

       total used free shared buff/cache available
Mem: 488M 158M 83M 2.3M 246M 217M
Swap: 1.0G 506M 517M

How to adjust swappiness

Swappiness is a Linux kernel property that defines how often the system uses swap space. Swappiness can have a value between 0 and 100. A lower value will make the kernel avoid swapping as much as possible, while a higher value will make the kernel use swap space more aggressively.

The default swappiness value is 60. You can check the current swappiness value by typing:

cat /proc/sys/vm/swappiness

Output

60

While a swappiness value of 60 is fine for a desktop, for a production server you may want to set a lower value.

For example, to set the swappiness value to 10, type:

sudo sysctl vm.swappiness=10

To make this parameter persistent across reboots, append the following line to the /etc/sysctl.conf file:

vm.swappiness=10

The optimal swappiness value depends on your system workload and how memory is used. You should adjust this parameter in small increments to find the optimal value.

How to Delete a Swap File

If for any reason you want to deactivate and delete the swap file, follow these steps:

1. First, deactivate swap using the following command:

sudo swapoff -v /swapfile

2. Delete the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.

3. Finally delete the actual swapfile:

sudo rm /swapfile

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Get access/creation/modification time of files on linux using golang
  • Parsing Linux folder file creation and deletion
  • How to create a file system in a Linux partition or logical volume
  • Command to view binlog file creation time in Linux
  • Example code of Linux command to create date folder or file
  • Getting the creation time of a file under Linux and a practical tutorial

<<:  Detailed example of database operation object model in Spring jdbc

>>:  Implementing parameter jump function in Vue project

Recommend

How to build a multi-node Elastic stack cluster on RHEL8 /CentOS8

Elastic stack, commonly known as ELK stack, is a ...

Detailed explanation of Docker container data volumes

What is Let’s first look at the concept of Docker...

How to set list style attributes in CSS (just read this article)

List style properties There are 2 types of lists ...

Detailed explanation of mysql execution plan id is empty (UNION keyword)

Introduction During the work process, slow querie...

How to Apply for Web Design Jobs

<br />Hello everyone! It’s my honor to chat ...

How to encapsulate axios request with vue

In fact, it is very simple to encapsulate axios i...

Solve the problem that Docker must use sudo operations

The steps are as follows 1. Create a docker group...

Complete steps to build NFS file sharing storage service in CentOS 7

Preface NFS (Network File System) means network f...

How to configure tomcat server for eclipse and IDEA

tomcat server configuration When everyone is lear...

Vue implements DingTalk's attendance calendar

This article shares the specific code of Vue to i...

Example of how to set div background transparent

There are two common ways to make div background ...

Solve the problem of IDEA configuring tomcat startup error

The following two errors were encountered when co...

Scoring rules of YSlow, a webpage scoring plugin developed by Yahoo

YSlow is a page scoring plug-in developed by Yaho...

JavaScript to achieve floor effect

This article shares the specific code of JavaScri...

Summary of 28 common JavaScript string methods and usage tips

Table of contents Preface 1. Get the length of a ...