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 install the graphical interface in Linux

1. Linux installation (root user operation) 1. In...

Detailed explanation of JSONObject usage

JSONObject is just a data structure, which can be...

Native JS to implement breathing carousel

Today I will share with you a breathing carousel ...

Vue realizes click flip effect

Use vue to simply implement a click flip effect f...

Implementation of Nginx hot deployment

Table of contents Semaphore Nginx hot deployment ...

How to clean up data in MySQL online database

Table of contents 01 Scenario Analysis 02 Operati...

js to realize the rotation of web page pictures

This article shares the specific code of js to re...

Brief Analysis of MySQL B-Tree Index

B-Tree Index Different storage engines may also u...

Solution to the problem of z-index not taking effect in CSS3

I recently wrote a combination of CSS3 and js, an...

Briefly describe mysql monitoring group replication

Original text: https://dev.mysql.com/doc/refman/8...

A complete guide to the Docker command line (18 things you have to know)

Preface A Docker image consists of a Dockerfile a...

Solution to Ubuntu 20.04 Firefox cannot play videos (missing flash plug-in)

1. Flash plug-in package download address: https:...