Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of adding Swap Partition on Ubuntu 16.04

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail about the method of adding Swap partition on Ubuntu 16.04, Xiaobian thinks it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.

preface

One of the easiest ways to improve server responsiveness and prevent application out-of-memory errors is to add some swap space. In this guide, we'll cover how to add swap files to an Ubuntu 16.04 server.

However, please note:

Although swap zones are generally recommended for systems that use traditional rotating hard drives, using swap zones with SSDs can cause hardware degradation issues over time. Due to this consideration, we do not recommend enabling swap partitioning on DigitalOcean or any other vendor that uses SSD storage. Doing so may affect the reliability of the underlying hardware for you and your neighbors. This guide is for users who may have used rotating disk systems elsewhere. If you need to improve the performance of DigitalOcean servers, we recommend upgrading your Droplet for a better experience and to reduce the likelihood of hardware issues affecting your service.

1. Let's first understand what a Swap is.

Swap partition (also known as swap partition) is an area on the hard disk designated as a place where the operating system can temporarily store data that can no longer be saved in RAM. Basically, this allows you to increase the amount of information the server retains in working "memory," but with a few caveats, mainly that swap space on the hard drive will be used when there isn't enough space in RAM for the application data in use.

Information written to disk will be much slower than information stored in RAM, but the operating system prefers to keep application data in memory and swap old data with. In general, using swap space as fallback space when a system runs out of RAM can be a good safety net to prevent a non-SSD storage system from running out of memory.

2. Check the exchange information of the system

Before we start, we can check whether the system already has some swap space available, possibly multiple swap files or swap partitions, but usually enough. We can check whether the system has swap partitions by issuing the following command:

$ sudo swapon --show

If there are no results or no displays, the system currently has no swap space available. You can also use the free tool to verify that there are no swap partitions currently available.

$ free -h

Output:

total used free shared buff/cache available

Mem: 488M 36M 104M 652K 348M 426M

Swap: 0B 0B 0B

You can see that the "swap" rows here are all 0, which means that no swap is active on the system.

3. Check free space on hard drive partition

The most common way to allocate space for swap is to use a separate partition dedicated to a specific task, but changing the partitioning scheme is not necessarily feasible, we can simply create swap files that reside on existing partitions.

Before we begin, we should check the current disk usage by entering the following command:

$ df -h

Output:

Filesystem Size Used Avail Use% Mounted on

udev 238M 0 238M 0% /dev

tmpfs 49M 624K 49M 2% /run

/dev/vda1 20G 1.1G 18G 6% /

tmpfs 245M 0 245M 0% /dev/shm

tmpfs 5.0M 0 5.0M 0% /run/lock

tmpfs 245M 0 245M 0% /sys/fs/cgroup

tmpfs 49M 0 49M 0% /run/user/1001

In this case, the device under/dev is our disk. In this example we have plenty of room (only 1.1G used), although your usage may vary.

Although there are many opinions about the appropriate size of exchange space, it depends on your personal preferences and application requirements. In general, twice or twice the amount of system memory is a good starting point. Another good rule of thumb is that if you're only using it as a RAM backup, try not to exceed 4 GB in swap partition size.

4. Create swap file

Now that we know the available hard disk space, we can create a swap file in the file system. We will create a file called swapfile in our root (/) directory. The best way to create swap files is to use the fallocate command, which immediately creates a pre-allocated size file. Since the server RAM size in this example is 512MB, we will create a 1 GB file in this tutorial and adjust it appropriately to meet the needs of your own server:

$ sudo fallocate -l 1G /swapfile

Once created, we can verify that the correct swap space has been reserved with this command:

$ ls -lh /swapfile

Display results:

$ -rw-r--r-- 1 root root 1.0G Apr 25 11:14 /swapfile

This shows that our files have been created with the correct space size.

5. Enable swap files

Now we have a 1 GB file, we need to turn it into swap space

First of all, we need permission to lock the file so that only users with root privileges can read the contents of the file, which prevents ordinary users from accessing the file and creating a major security risk.

Root privileges to lock files:

$ sudo chmod 600 /swapfile

Verification authority:

$ ls -lh /swapfile

Display results:

-rw------- 1 root root 1.0G Apr 25 11:14 /swapfile

As you can see, only root has the read-write flag enabled.

Next, we can mark the file as swap space with the following command

$ sudo mkswap /swapfile

Display results:

Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)

no label, UUID=6e965805-2ab9-450f-aed6-577e74089dbf

After marking the file, we can enable the swap file and let our system start using it:

$ sudo swapon /swapfile

You can verify that swap space is available by issuing the following command:

$ sudo swapon --show

Display results:

NAME TYPE SIZE USED PRIO

/swapfile file 1024M 0B -1

At this point, we can check our settings again via free:

$ free -h

Display results:

total used free shared buff/cache available

Mem: 488M 37M 96M 652K 354M 425M

Swap: 1.0G 0B 1.0G

You can see that the swap partition has been successfully created with a size of 1.0 G, which the operating system will use if necessary.

6. Permanently retain exchange files

Our most recent change enables swap files for the current session, however, if we reboot, the server does not automatically retain swap settings, we can change this by adding swap files to the/etc/fstab file.

Backup the/etc/fstab file in case of errors:

$ sudo cp /etc/fstab /etc/fstab.bak

Add swap file information to the end of the/etc/fstab file:

$ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

This preserves the swap file.

7. Adjust your swap settings

When dealing with swapping, you can configure several options that affect the performance of the system

7.1 Adjust the happiness attribute

The swappiness parameter configures how often your system swaps data from RAM to swap space, and values between 0 and 100 represent percentages. If the happiness value is close to 0, the kernel will not swap data to disk unless absolutely necessary. Keep in mind that interacting with swap files is "expensive" because interacting with swap takes longer than interacting with RAM and results in significant performance degradation. A system that relies less on swap partitioning usually makes your system faster. A value of swappiness close to 100 will try to put more data into the swap to keep more RAM space. Depending on your application's memory profile or the server you use, this may be better in some cases.

To view the current happiness value:

$ cat /proc/sys/vm/swappiness

results showed

60

A happiness setting of 60 is not a bad value for desktop systems, but for servers you might want to set it closer to 0.

We can set happiness to different values using the sysctl command, for example, to set happiness to 10:

$ sudo sysctl vm.swappiness=10

Display results:

vm.swappiness = 10

This setting will persist until the next system reboot, and if you want it to take effect after a reboot, you can do this by adding a line to the/etc/sysctl.conf file:

$ sudo nano /etc/sysctl.conf

Add at the end of the document:

vm.swappiness=10

Save and close the file when finished.

7.2 Adjust cache pressure settings

Another relevant value you might want to modify is vfs_cache_pressure, which configures how much data the system will select for caching inode and dentry information. Basically, this is accessing data about the file system, usually very time-consuming queries and frequent requests, so it's a good thing to have your system cache where you can view current values by querying the proc file system again.

$ cat /proc/sys/vm/vfs_cache_pressure

Output:

100

This configuration may cause our system to delete inode information from cache too quickly. We can set a more conservative value, such as 50.

$ sudo sysctl vm.vfs_cache_pressure=50

Display results:

vm.vfs_cache_pressure = 50

Similar to swappiness, this only works for the current session, we can change it by adding it to our profile, just like we used our swappiness settings:

$ sudo nano /etc/sysctl.conf

Add at the end:

vm.vfs_cache_pressure=50

Save and close the file when finished.

About "Ubuntu 16.04 on the increase Swap partition method" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report