交换区域(Swap Area)有什么作用?

交换分区是操作系统在内存不足(或内存较低)时的一种补充。通俗的说,如果说内存是汽油,内存条就相当于油箱,交换区域则相当于备用油箱。

Ubuntu Linux安装时可以在磁盘上划出一个分区用作内存交换区域。文件则介绍如何使用文件作为内存交换区域。这两种方法在使用效果上没有太大区别,但文件可以在分区之后创建,且调整大小更容易,所以这种方案更加灵活。

本文在Ubuntu上试验成功,在其他发行版操作类似。

原文地址:https://www.howtoforge.com/ubuntu-swap-file

How to Add a Swap File On Ubuntu Linux

Contents

1.1 What does Swapping Mean in Linux
1.2 Important: Preliminary Check before setting up a Swap file
1.3 Adding a Swap File on Ubuntu - A Step-By Step Guide
1.3.1 Step 1-Checking the File System
1.3.2: Step 2-Creating the Swap File
1.3.3: Step 3- Enabling the Swap File
1.3.4: Step 4-Adding the Swap to the fstab file
1.3.5: Step 5-Setting Up Appropriate Permission Levels on the Swap File
1.4 Test

1.1-What Does Swapping Mean in Linux?

A swap file enables an operating system (OS) to utilize hard disk space to simulate additional memory. Effectively, as the system goes on the low-memory mode, it swaps a certain section of the RAM that an idle program may be using onto the hard disk to release memory capacity for other vital programs. Subsequently, when the user returns to the swapped out program, it swaps places with another program in RAM. This intelligent mix of RAM and swap files is commonly referred to as virtual memory, and its use allows the system to run a far greater number of programs than possible using only the built-in RAM.

Expressly in the context of Linux, the RAM on the Linux server comprises memory chunks technically termed as pages. To free up these RAM pages, a LINUX swap may be executed to copy the memory page from the parent RAM to a preconfigured space on the hard disk. Linux swaps of this nature allow a system to exploit more physical memory than was initially made available. Though a useful tool, swapping does suffer from a few demerits. Firstly, the activity triggers a sizeable amount of hard disk reading and writing, which invariably slows down your system to a considerable extent. Secondly, swap-thrashing may happen if the system gets inundated with a heavy load of files being swapped in and out frequently.

1.2-Important: Preliminary Check before setting up a Swap file

Before the users proceed with the process of setting up a swap file, it is imperative to check for any existing swap files that may have been enabled on the VPS. This can be done by reviewing the summary of swap usage. The following command shall lead you to the required summary:

sudo swapon -s

The above command shall lead to the following results:

Filename Type Size Used Priority

If the above happens to be an empty list, you may be rest assured that no swap files have been enabled.

1.3-Adding a Swap File on Ubuntu - A Step-By Step Guide

Once the preliminary check has been done, the users are now all geared up for adding a Swap File on Ubuntu. All they need to do is follow the steps given below:

1.3.1: Step 1 - Checking the File System

Once it has been firmly established that there do not exist any active swap files on the virtual server, one must check the amount of space available on the server using the df command. The swap file would normally occupy 256MB of space, which would amount to merely 8% of the /dev/sda on a typical 32GB capacity server, one may assuredly proceed with the process The following set of commands shall help users complete the first step.

df

Filesystem 1K-blocks Used Available Use% Mounted on

/dev/sda 20907056 1437188 18421292 8% /
udev 121588 4 121584 1% /dev
tmpfs 49752 208 49544 1% /run
none 5120 0 5120 0% /run/lock
none 124372 0 124372 0% /run/shm

The above step shows the free space on your harddisk. Ensure that there is enough free space for your swap file. next we will create the actual file with dd as explained below.

1.3.2: Step 2 - Creating the Swap File

The second step comprises creating the Swap File with a size of 256 MB by using the dd command follows:

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

of=/swapfile” designates the file’s name. In this case the name is swapfile.

Consequently the swap file can be prepared by creating a designated Linux swap area using the following command:

sudo mkswap /swapfile

The above command shall display the following results:

Setting up swapspace version 1, size = 262140 KiB

no label, UUID=103c4545-5fc5-47f3-a8b3-dfbdb64fd7eb

Completion of Step 2 shall lead to the creation of the Swap File. The next step, as described below, shall lead to the activation of the Swap File thus created.

1.3.3: Step 3 - Enabling the Swap File

The process of creating the Swap File on Ubuntu needs to befinished by enabling the file so created. This can be done by using the following command:

sudo swapon /swapfile

Completion of Step 3 shall enable users to view the new swap file when under swap summary, as follows:

swapon -s

Filename Type Size Used Priority

/swapfile file 262140 0 -1

The file displayed as per details above shall exist on the virtual private server till the time the machine reboots. However, users enjoy the option of making the Swap File permanent by following the next step. 1.3.4: Step 4-Adding the Swap to the fstab file In order to lend permanence to the swap file you created, you must add the same to the fstab file. The following command that will help you open up the file remains the first in the process that shall help you do that:

sudo nano /etc/fstab

Subsequently, users need to paste in the following line:

 /swapfile       none    swap    sw      0       0

Post the above step, the Swappiness in the file must ideally be set to 10, by using the following set of commands:

echo 10 | sudo tee /proc/sys/vm/swappiness
echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf

Important: Doing away with the above important sub-step may invariably lead to poor system performance, while configuring the setting to 10 shall lead to the swap acting as an emergency buffer, thus averting irksome out-of-memory crashes.

1.3.5: Step 5 - Setting Up Appropriate Permission Levels on the Swap File

The last and final step in the process of creating a Swap File on Ubuntu remains setting up apt permission levels for the file. This is essential to prevent the file from becoming world-readable and lending appropriate security to the same. The same may be executed by using the following either of the following commands (depending upon the case)::

sudo chown root:root /swapfile

sudo chmod 0600 /swapfile

1.4 Test

Finally we test that the swap file is active. Run the top command

top

the swap space should be shown in the column

KiB Swap: 262140

如何在Linux上使用文件作为内存交换区(Swap Area)的更多相关文章

  1. 如何在 Linux 上复制文件/文件夹到远程系统?

    从一个服务器复制文件到另一个服务器,或者从本地到远程复制是 Linux 管理员的日常任务之一. 我觉得不会有人不同意,因为无论在哪里这都是你的日常操作之一.有很多办法都能处理这个任务,我们试着加以概括 ...

  2. 如何在Linux上清理内存缓存、缓冲与交换空间

    如何在Linux上清理内存缓存.缓冲与交换空间 与其他类型的操作系统一样,GNU/Linux已经有效的实现了内存管理,甚至更加优秀.但是如果任何进程正在吃光你的内存,并且你想清理它,Linux提供了一 ...

  3. [转帖]如何在Linux上使用命令行查看硬件信息

    如何在Linux上使用命令行查看硬件信息 时间:2016-01-13   作者:admin 分类:新手入门 阅读:126次 http://embeddedlinux.org.cn/emb-linux/ ...

  4. 如何在Linux上通过grub添加内核参数

    转自Linux中国 我们可以在linux内核启动时为其提供各种各样的参数.这些参数可以自定义内核默认的行为,或者通知内核关于硬件的配置信息.内核参数应在内核启动时通过引导装载程序,如GRUB或LILO ...

  5. 如何在Linux上使用VIM进行.Net Core开发

    对于在Linux上开发.Net Core的程序员来说, 似乎都缺少一个好的IDE. Windows上有Visual Studio, Mac上有Visual Studio for Mac, 难道Linu ...

  6. springboot打成的jar包如何在Linux上持久运行

    一.首先说说在没有springboot的时候,项目是如何部署的? 1.动态web项目 动态web项目部署很方便,基本上上传文件到服务器的tomcat里面的webapps文件夹下即可完成部署.当然了,这 ...

  7. 如何在 Linux 上设置密码策略

    https://linux.cn/article-2518-1.html 用户帐号管理是系统管理员最重要的工作之一.而密码安全是系统安全中最受关注的一块.在本教程中,我将为大家介绍如何在 Linux ...

  8. 如何在 Linux 上安装应用程序

    如何在 Linux 上安装应用程序 编译自:https://opensource.com/article/18/1/how-install-apps-linux作者: Seth Kenlon原创:LC ...

  9. 关于如何在Linux上使用Nugix反向代理部署net core3.1项目

    本文意在教大家如何在Linux上部署net core web项目,本人通过实践已经成功可以通过外网访问我部署在阿里云服务器上的站点. 一:需要用到的东西如下: 1:一个基于net core框架下的we ...

随机推荐

  1. iOS:实现图片的无限轮播(二)---之使用第三方库SDCycleScrollView

    iOS:实现图片的无限轮播(二)---之使用第三方库SDCycleScrollView 时间:2016-01-19 19:13:43      阅读:630      评论:0      收藏:0   ...

  2. Visual Studio 使用Web Deploy 3.6发布项目

    工具:Web Deploy 3.6 点击下载 (强烈推荐使用独立的Web Deploy 安装包安装) 配置: 1.安装web deploy,安装好之后,点击IIS根目录,此处应有如下图标 另外,需要注 ...

  3. Django补遗(一)

    链接MYSQL数据库 修改项目中的配置文件: DATABASES = { 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME' ...

  4. 很不错的Intent用法 适用于正在开发的伙伴。自己看到了,也分享给大家吧。

    本文介绍Android中Intent的各种常见作用. 1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的开 ...

  5. mysqlbinlog

    一.描述 转换二进制日志为易读的文本格式或用于管道后恢复数据 二.用法 -d, --database=name 仅列出指定数据库的条目 --start-datetime=name 从指定时间开始读取事 ...

  6. C#技术漫谈之垃圾回收机制(GC)

    GC的前世与今生 虽然本文是以.NET作为目标来讲述GC,但是GC的概念并非才诞生不久.早在1958年,由鼎鼎大名的图林奖得主John McCarthy所实现的Lisp语言就已经提供了GC的功能,这是 ...

  7. ADB

    adb logcat 命令详解 log过滤 http://blog.csdn.net/liao277218962/article/details/50129009 如何使用 adb logcat 查看 ...

  8. Spring Boot入门实例

    简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置 ...

  9. iPhone屏幕尺寸/launch尺寸/icon尺寸

    屏幕尺寸 6p/6sp     414 X 736 6/6s         375 X 667 5/5s         320 X 568  4/4s         320 X 480   la ...

  10. Xshell5 里桌面显示数据调大!!!

    今天告诉大家一个好用的技巧, 在Xshell5下工作,屏幕上很容易积攒数据量,尤其是编译的时候,(当然你编译的时候可以重定向到一个文件中,也可以只重定向编译错误的信息): 这个时候,你想观看之前的编译 ...