LoginSignup
15

More than 5 years have passed since last update.

`npm install` が殺される on 低容量メモリ環境

Last updated at Posted at 2016-01-11

Digital Ocean の安いやつで試しに node/npm をインストールした時に起こるこれと戦う:

root@ubuntu-512mb-nyc3-01:~/k-ui.jp# npm install
Killed
root@ubuntu-512mb-nyc3-01:~/k-ui.jp# echo $?
137

原因

/var/log/syslog を確認すると、OOM で殺されているのが分かる。メモリ 512 MB ではダメらしい。

root@ubuntu-512mb-nyc3-01:~/k-ui.jp# tail /var/log/syslog
...
Jan 11 03:04:14 ubuntu-512mb-nyc3-01 kernel: [  684.969707] Out of memory: Kill process 2282 (npm) score 882 or sacrifice child
Jan 11 03:04:14 ubuntu-512mb-nyc3-01 kernel: [  684.969927] Killed process 2282 (npm) total-vm:1614964kB, anon-rss:454708kB, file-rss:0kB

対処法

npm install を省メモリ化するアプローチはまだできないっぽいので、スワップ領域を追加するしか無さそう。

terraform を使っているので、下記のようなスクリプトを Droplet 作成時に適応するようにした:

attach-swapfile.bash
#!/bin/bash
set -eux

# attach swapfile
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo "/swapfile none swap sw 0 0" >> /etc/fstab

# swappiness
echo "vm.swappiness = 10" > /etc/sysctl.d/10-swappiness.conf
sysctl --load /etc/sysctl.d/10-swappiness.conf

このスクリプトでは 2GB のスワップ領域を追加している。ついでに swappiness も設定しているけれど、要らないかも。

確認する:

$ free -h | grep ^Swap
Swap:         2.0G       136K       2.0G

これで npm install は殺されなくなる。。。が、依存パッケージが増えたら 2G でも足らなくなることがありそうな気もする。

おわり。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15