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
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:
|
<<: Detailed example of database operation object model in Spring jdbc
>>: Implementing parameter jump function in Vue project
Elastic stack, commonly known as ELK stack, is a ...
What is Let’s first look at the concept of Docker...
List style properties There are 2 types of lists ...
Introduction During the work process, slow querie...
<br />Hello everyone! It’s my honor to chat ...
In fact, it is very simple to encapsulate axios i...
The steps are as follows 1. Create a docker group...
Preface NFS (Network File System) means network f...
tomcat server configuration When everyone is lear...
This article shares the specific code of Vue to i...
There are two common ways to make div background ...
The following two errors were encountered when co...
YSlow is a page scoring plug-in developed by Yaho...
This article shares the specific code of JavaScri...
Table of contents Preface 1. Get the length of a ...