摘要 :使用 yarn build 项目遇到 “Command failed with exit code 137” 错误,原来是 OOM 内存不足,可以通过添加系统交换空间(Swap Space)解决这个问题。

最近新购买的云服务器,在上面测试用yarn build一个 Vue 项目时,遇到error Command failed with exit code 137错误:

$ yarn build
yarn run v1.15.2
$ vue-cli-service build

⠏  Building for production...
Killed
error Command failed with exit code 137.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Google 发现 137 是内存不足(Out of memory)的意思,在 该网友 的提示下用free -h命令看到系统确实没有启用 swap:

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           977M        216M        633M        1.8M        127M        627M

该怎样启用系统的交换空间呢?找到一篇英文文档讲解非常详细,于是抽空把它翻译了过来,以下是译文内容。


原文地址:https://linuxize.com/post/how-to-add-swap-space-on-debian-9/

Add Swap on Debian

Swap 是磁盘上的一个空间,当物理内存已满时会使用到它。Linux 系统在内存耗尽时,会将不活动的页面从内存移动到交换空间(Swap Space)。

交换空间可以以专用交换分区或交换文件的形式存在。通常,运行 Debian 虚拟机是不存在交换分区的,只能选择创建交换文件。

本教程概述了在 Debian 9 系统上添加交换文件所需的步骤。

Swap is a space on a disk that is used when the amount of physical RAM memory is full. When a Linux system runs out of RAM, inactive pages are moved from the RAM to the swap space.

Swap space can take the form of either a dedicated swap partition or a swap file. Usually, when running a Debian virtual machine a swap partition is not present so the only option is to create a swap file.

This tutorial outlines the steps necessary to add a swap file on Debian 9 systems.

在开始之前(Before You Begin)

在继续学习本教程之前,请检查你安装的 Debian 是否已经启用了 swap,输入以下命令:

Before continuing with this tutorial, check if your Debian installation already has swap enabled by typing:

sudo swapon --show

如果输出为空,则表示系统没有启用交换空间。

否则,如果你看到类似如下内容,则说明你的机器上已经启用了 swap。

If the output is empty, it means that the system doesn’t have swap space.

Otherwise, if you get something like below, you already have swap enabled on your machine.

NAME      TYPE      SIZE USED PRIO
/dev/sda2 partition   4G   0B   -1

虽然有可能,但是在一台机器上启用多个交换空间并不常见。

Although possible, it is not common to have multiple swap spaces on a single machine.

创建交换文件(Creating a Swap File)

你的登录用户必须具有 sudo 权限 才能激活 swap。在本指南中,我们将添加1G的 swap,如果你想创建一个更大的 swap,用你需要的 swap 空间大小替换1G

下面的步骤显示了如何在 Debian 9 上添加交换空间。

The user you are logged in as must have sudo privileges to be able to activate swap. In this guide, we will add 1G of swap, if you want to create a bigger swap, replace 1G with the size of the swap space you need.

The steps below show how to add swap space on Debian 9.

  1. 首先创建一个用于 swap 的文件:

    01.Start by creating a file which will be used for swap:

    sudo fallocate -l 1G /swapfile
    

    如果没有安装 fallocate,或者你看到一条错误消息 fallocate failed: Operation not supported,那么使用以下命令创建交换文件:

    If fallocate is not installed or you get an error message saying fallocate failed: Operation not supported then use the following command to create the swap file:

    sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
    
  2. 只有 root 用户才能读写交换文件。执行以下命令来设置合适的权限:

    02.Only the root user should be able to read and write to the swap file. Issue the command below to set the correct permissions:

    sudo chmod 600 /swapfile
    
  3. 使用 mkswap 工具在该文件上设置一个 Linux 交换区域:

    03.Use the mkswap tool to set up a Linux swap area on the file:

    sudo mkswap /swapfile
    
  4. 输入以下命令激活交换文件:

    04.Activate the swap file by typing:

    sudo swapon /swapfile
    

    要想让该修改永久生效,可以打开 /etc/fstab 文件:

    Make the change permanent by opening the /etc/fstab file:

    sudo nano /etc/fstab
    

    并粘贴以下行到文件中:

    and pasting the following line:

    /swapfile swap swap defaults 0 0
    
  5. 要验证 swap 是否处于激活状态,可以使用 swaponfree 命令,如下所示:

    05.To verify the swap is active use either the swapon or free command as shown below:

    sudo swapon --show
    
    NAME      TYPE  SIZE   USED PRIO
    /swapfile file 1024M 507.4M   -1
    
    sudo free -h
                  total        used        free      shared  buff/cache   available
    Mem:           488M        158M         83M        2.3M        246M        217M
    Swap:          1.0G        506M        517M
    

调整 Swappiness 值(Adjusting the Swappiness Value)

Swappiness 是一个 Linux 内核属性,它定义了系统使用交换空间的频率。Swappiness 的值可以在 0 到 100 之间。低值将使内核尽可能避免交换,而高值将使内核更积极地使用交换空间。

默认的 swappiness 值是 60。您可以通过输入以下命令来检查当前的 swappiness 值:

Swappiness is a Linux kernel property that defines how often the system will use the swap space. Swappiness can have a value between 0 and 100. A low value will make the kernel to try to avoid swapping whenever possible while a higher value will make the kernel to use the swap space more aggressively.

The default swappiness value is 60. You can check the current swappiness value by typing the following command:

cat /proc/sys/vm/swappiness
60

对于桌面个人台式机,swappiness 值为 60 是可以的,但是对于生产服务器,你可能需要设置一个更低的值。

例如,要将 swappiness 值设置为 10,输入:

While the swappiness value of 60 is OK for Desktops, for production servers you may need to set a lower value.

For example, to set the swappiness value to 10, type:

sudo sysctl vm.swappiness=10

要使此参数在重启后依然生效,请将以下行添加到/etc/sysctl.conf文件:

To make this parameter persistent across reboots append the following line to the /etc/sysctl.conf file:

vm.swappiness=10

最佳 swappiness 值取决于系统工作负载和内存的使用方式。你应该以小的增量调整此参数,以找到最佳值。

The optimal swappiness value depends on your system workload and how the memory is being used. You should adjust this parameter in small increments to find an optimal value.

删除交换文件(Removing a Swap File)

要停用和删除交换文件,请执行以下步骤:

To deactivate and remove the swap file, perform the steps below:

  1. 首先键入以下命令停用 swap 空间:

    1.First deactivate the swap space by typing:

    sudo swapoff -v /swapfile
    
  2. 接下来,从 /etc/fstab 文件中删除交换文件条目 /swapfile swap default 0 0

    2.Next, remove the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.

  3. 最后,删除实际的 swapfile 文件:

    3.Finally, delete the actual swapfile file:

    sudo rm /swapfile
    

结论(Conclusion)

你已经了解了如何在 Debian 9 机器上创建交换文件并激活和配置交换空间。

如果你遇到了问题或有其他任何反馈,欢迎在下方留言。

You have learned how to create a swap file and activate and configure swap space on your Debian 9 machine.

If you hit a problem or have feedback, leave a comment below.