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

The images in HTML are directly replaced by base64 encoded strings

Recently, I came across a webpage that had images ...

How to implement Nginx reverse proxy for multiple servers

Nginx reverse proxy multiple servers, which means...

MySQL query example explanation through instantiated object parameters

This article will introduce how to query data in ...

XHTML 2.0 New Features Preview

<br />Before browsers can handle the next ge...

MySQL account password modification method (summary)

Preface: In the daily use of the database, it is ...

Completely uninstall MySQL database in Windows system to reinstall MySQL

1. In the control panel, uninstall all components...

Detailed explanation of the use of MySQL sql_mode

Table of contents Preface sql_mode explained The ...

How to quickly delete all tables in MySQL without deleting the database

This article uses an example to describe how to q...

WeChat applet realizes the effect of swiping left to delete list items

This article shares the specific code for WeChat ...

CSS multi-level menu implementation code

This is a pretty cool feature that makes web page...

Setting up shadowsocks+polipo global proxy in Linux environment

1. Install shadowsocks sudo apt-get install pytho...

In-depth understanding of Vue's data responsiveness

Table of contents 1. ES syntax getter and setter ...

js implements the classic minesweeper game

This article example shares the specific code of ...