文章

折腾小服务器 - swap

内存啊内存啊,vps的内存越来越捉急了。现在内存这么便宜,好想买个8G内存条插到vps上啊……这么一想,vps卖的也太贵了……一个8G内存条现在也就二三百块钱。奸商啊!

  1. swap升级
  2. 内存占用
    1. RAM
    2. swap
  3. 感想

swap升级

默认的swap是512M,打算按照推荐增大到2G

想起大学时老王说的以前看到说建议设置为2倍ram,就设了很大的swap,却发现swap怎么都用不满。后来发现2倍ram是曾经ram很小的时候的一个推荐……早就不适用于这个时代了。但是放到vps上,还是适用的。

升级swap的流程本身比较简单。需要注意swap文件的名字不是固定的,我的是/swap

1
2
3
4
5
$ sudo swapoff -a
# 2G
$ sudo dd if=/dev/zero of=/swap bs=128M count=16
$ sudo mkswap /swap
$ sudo swapon /swap

查看新的swap效果:

1
2
$ grep -i swaptotal /proc/meminfo
SwapTotal:       1999996 kB

需要注意的是,swapoff的时候,占用的内存页会从swap里挪到ram里。如果ram不够装下这么多页,就会OOM,导致swapoff被oom killer kill掉。所以要关掉内存占用比较大的程序,腾出ram,再swapoff

1
[1]    2800660 killed     sudo swapoff -a

dd也同理。因为swapoff后最终ram只有不到200m可用,所以dd的block size无法设置1G,只能设置128M,否则dd也会被oom killer挂掉:

1
2
3
dd: memory exhausted by input buffer of size 1073741824 bytes (1.0 GiB)

[1]    2801087 killed     sudo dd if=/dev/zero of=/swap bs=1GB count=1

惨绝人寰……

可以从dmesg看到两个难兄难弟:

1
2
3
4
5
6
$ sudo dmesg -T | grep -i "out of memory"
[Fri Mar 10 10:53:23 2023] Out of memory: Killed process 2800661 (swapoff) total-vm:7008kB, anon-rss:144kB, file-rss:0kB, shmem-rss:0kB, UID:0 pgtables:52kB oom_score_adj:0                                                                                
[Fri Mar 10 10:53:56 2023] Out of memory: Killed process 2800692 (swapoff) total-vm:7008kB, anon-rss:140kB, file-rss:0kB, shmem-rss:0kB, UID:0 pgtables:52kB oom_score_adj:0                                                                                
[Fri Mar 10 10:56:46 2023] Out of memory: Killed process 2758245 (mongod) total-vm:2629080kB, anon-rss:111640kB, file-rss:0kB, shmem-rss:0kB, UID:999 pgtables:656kB oom_score_adj:0
[Fri Mar 10 10:56:47 2023] Out of memory: Killed process 2801088 (dd) total-vm:981976kB, anon-rss:179120kB, file-rss:4kB, shmem-rss:0kB, UID:0 pgtables:408kB oom_score_adj:0
[Fri Mar 10 10:57:57 2023] Out of memory: Killed process 2801333 (dd) total-vm:981976kB, anon-rss:225864kB, file-rss:4kB, shmem-rss:0kB, UID:0 pgtables:500kB oom_score_adj:0

内存占用

RAM

使用ps查看内存占用大头,排个序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ ps aux --sort -rss | head -20
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
pichu    2758906  0.4  9.0 964584 92000 ?        Ssl  01:50   2:49 node /app/app.js
systemd+ 2801173  1.2  8.2 2616344 83368 ?       Ssl  11:05   0:08 mongod --bind_ip_all
root        6414  0.5  5.5 1753580 56636 ?       Ssl   2022 1595:46 /usr/sbin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
pichu    2758616  0.2  3.5 762848 35520 ?        Sl   01:50   1:13 node /usr/local/bin/pm2-runtime --raw pm2.config.js
root     2409700  0.0  2.8  48796 28728 ?        S    Mar06   0:10 /usr/local/bin/python /root/.local/bin/gunicorn -b 0.0.0.0:5000 dailytxt.application:create_app() --daemon
root     2799669  0.1  2.5 1337352 25912 ?       Ssl  10:51   0:02 /usr/bin/containerd
root     2604559  0.0  2.3 875024 24288 ?        Ssl  Mar08   0:22 /usr/lib/snapd/snapd
pichu    2758365  0.0  2.2 764324 22980 ?        Ssl  01:49   0:01 npm start
root        6873  0.0  2.1 745296 21356 ?        Ssl   2022  70:38 /portainer
root     2616524  0.0  1.4 129300 14836 ?        Ss   Mar08   0:09 /lib/systemd/systemd-journald
root     2801151  0.0  1.4 1451560 14588 ?       Sl   11:05   0:00 /usr/bin/containerd-shim-runc-v2 -namespace moby -id cffda1136e814224e21b2430d3d6358a77a126a00e04625f6179cc3a457a6391 -address /run/containerd/containerd.sock
pichu       3592  0.0  1.3  19184 13832 ?        Ss    2022 247:16 tmux
root     2409615  0.0  1.2 1525548 13108 ?       Sl   Mar06   0:21 /usr/bin/containerd-shim-runc-v2 -namespace moby -id 7df2b26b6121174b9ef57f06ac6a6fa4407beaa5cff84d6717de30306c30a013 -address /run/containerd/containerd.sock
root        6812  0.0  1.2 1600880 12368 ?       Sl    2022  24:37 /usr/bin/containerd-shim-runc-v2 -namespace moby -id be1ae7bb87ad230b81ca7775ecb4fe5ad731ae6872badadc353038030bc4439d -address /run/containerd/containerd.sock
root     2409690  0.0  1.2  24420 12344 ?        S    Mar06   1:09 /usr/local/bin/python /root/.local/bin/gunicorn -b 0.0.0.0:5000 dailytxt.application:create_app() --daemon
root        6869  0.3  1.1 720700 12072 ?        Ssl   2022 963:52 v2ray -config=/etc/v2ray/docker.config.json
root        6733  0.0  1.1 1223628 11732 ?       Sl    2022   0:43 /usr/sbin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 9443 -container-ip 172.17.0.2 -container-port 9443
root     1847247  0.0  1.0  21112 10556 ?        Ss    2022  17:18 /usr/bin/perl -wT /usr/sbin/munin-node --foreground
root     2758341  0.0  1.0 1451816 10396 ?       Sl   01:49   0:02 /usr/bin/containerd-shim-runc-v2 -namespace moby -id 12fdf139b7edd32a20b4e7112dd44f617f755a29913e4bd1aa0cea8a61211b72 -address /run/containerd/containerd.sock

rss代表resident set sizeps manual的STANDARD FORMAT SPECIFIERS介绍了各个字段的含义,其中明确说明了rss是“纯物理RAM占用量,放到swap里的不算”

1
rss         RSS       resident set size, the non-swapped physical memory that a task has used (in kilobytes).  (alias rssize, rsz).

排序可以用rss,也可以用r,具体可以参考ps manual的OBSOLETE SORT KEYS部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
OBSOLETE SORT KEYS
       These keys are used by the BSD O option (when it is used for sorting).  The GNU --sort option doesn't use these keys, but the specifiers described below in the STANDARD FORMAT SPECIFIERS section.  Note that the values used in sorting are
       the internal values ps uses and not the "cooked" values used in some of the output format fields (e.g.  sorting on tty will sort into device number, not according to the terminal name displayed).  Pipe ps output into the sort(1) command
       if you want to sort the cooked values.

       KEY   LONG         DESCRIPTION
       c     cmd          simple name of executable
       C     pcpu         cpu utilization
       f     flags        flags as in long format F field
       g     pgrp         process group ID
       G     tpgid        controlling tty process group ID
       j     cutime       cumulative user time
       J     cstime       cumulative system time
       k     utime        user time
       m     min_flt      number of minor page faults
       M     maj_flt      number of major page faults
       n     cmin_flt     cumulative minor page faults
       N     cmaj_flt     cumulative major page faults
       o     session      session ID
       p     pid          process ID
       P     ppid         parent process ID
       r     rss          resident set size
       R     resident     resident pages
       s     size         memory size in kilobytes
       S     share        amount of shared pages
       t     tty          the device number of the controlling tty
       T     start_time   time process was started
       U     uid          user ID number
       u     user         user name
       v     vsize        total VM size in KiB
       y     priority     kernel scheduling priority

youtubedl被搞进swap了,rss变小了

swap

也可以按swap占用排序,需要额外安装smem:

1
2
3
4
5
6
7
8
9
10
11
$ smem -s swap -r | head
  PID User     Command                         Swap      USS      PSS      RSS 
2758906 pichu    node /app/app.js               39052    43456    46510    49568 
2758365 pichu    npm start                      18268     5068     5068     5072 
 3592 pichu    tmux                            5020     8668     8848     9324 
 3635 pichu    -zsh                            2812       28       28       32 
2019143 pichu    -zsh                            2676      104      104      108 
2019189 pichu    -zsh                            2604        8        8       12 
 3593 pichu    -zsh                            2544       24       24       28 
2597500 pichu    -zsh                            2272       56       56       60 
2937327 pichu    -zsh                            2200       56       56       60 

感想

服务器貌似流畅了很多:D(心理作用)

本文由作者按照 CC BY 4.0 进行授权