Linux的OOMkiller簡單測試

付磊-起揚發表於2016-02-17

 

  一、一些概念

 

  1. OOM killer

 

       顧名思義,OOM(out of memory) killer,是Linux作業系統發現記憶體不足時,它會強制殺死一些使用者程式(非核心程式),來保證系統有足夠的實體記憶體進行分配。

 

  2. 記憶體overcommit

 

    Linux對大部分申請記憶體的請求都回復”yes”,以便能跑更多更大的程式。因為申請記憶體後,並不會馬上使用記憶體。這種技術叫做Overcommit。

 

    vm.overcommit_memory這個系統引數是用來設定記憶體分配策略的,它有三個可選值:

vm.overcommit_memory 含義
0

表示核心將檢查是否有足夠的可用記憶體供應用程式使用;如果有足夠的可用記憶體,記憶體申請允許;否則,記憶體申請失敗,並把錯誤返回給應用程式。

1

表示核心允許分配所有的可用記憶體,而不管當前的記憶體狀態如何。

2

表示核心允許分配超過所有實體記憶體和交換空間總和的記憶體

 
OOM killer與記憶體overcommit:兩者是相互作用的:
對於一臺16G的Linux伺服器。
如果使用overcommit_memory=0,如果沒有沒有足夠的記憶體分配,那麼應用想分配記憶體就會失敗。
如果使用overcommit_memory>0,每次的應用記憶體申請都會成功,這種成功是建立在OOM killer會殺掉部分程式來實現的。
 

  二、試驗:

 

 1. 環境:

 
(1) 記憶體:沒有開啟swap,可用記憶體12397M
             total       used       free     shared    buffers     cached
Mem:         15948       3606      12342          0         10         45
-/+ buffers/cache:       3550      12397
Swap:            0          0          0
 
(2) overcommit_memory當前是0
cat /proc/sys/vm/overcommit_memory
0

 

 

2. 開啟應用強制吃記憶體。

我對於redis稍微熟悉一點,redis-server有個引數叫做–test-memory,可以吃掉系統的記憶體,單位是M
redis-server --test-memory 1024

 

(1) 開啟一個redis-server –test-memory 5120,吃掉5G記憶體,記憶體變為:
[@zw_53_159 fl]# free -m
             total       used       free     shared    buffers     cached
Mem:         15948       8704       7243          0         10         47
-/+ buffers/cache:       8646       7301
Swap:            0          0          0

 

(2) 再開啟一個redis-server –test-memory 5120,吃掉5G記憶體,記憶體變為:
[@zw_53_159 fl]# free -m
             total       used       free     shared    buffers     cached
Mem:         15948      13839       2109          0         10         47
-/+ buffers/cache:      13781       2166
Swap:            0          0          0

 

(3) 此時還有2166可用記憶體,如果在開啟3G的Redis會怎樣呢?
[@zw_53_159 ~]# redis-server --test-memory 3072
Unable to allocate 3072 megabytes: Cannot allocate memory

 沒有足夠的記憶體了?為什麼呢:

  • 沒有swap可用
  • overcommit_memory=0,不會用overcommit機制。

 (4) 修改overcommit_memory=1,

 

echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
sysctl vm.overcommit_memory=1

 

 

(a) 當前系統只有一些Redis程式:

 

 

root     26738 21729 99 15:03 pts/5    00:06:10 redis-server --test-memory 5120
root     26910 22005 99 15:04 pts/7    00:05:34 redis-server --test-memory 5120

 

 

(b) 觀察OOM killer日誌:

   

tail -f /var/log/messages

 

 

 繼續開啟3G的Redis: 開啟成功,但是OOM把之前一個redis-server給kill掉了,從兩個方面看
(a) 程式:程式26738不見了。
  
root     26910 22005 99 15:04 pts/7    00:08:46 redis-server --test-memory 5120
root     27805 21964 93 15:12 pts/6    00:00:27 redis-server --test-memory 3072

 

(b) OOM killer日誌:裡面涉及到細節很多,例如計算使用者程式的分數,這個分數(oom_score_adj)決定該使用者程式被殺掉的可能性,
    最後算出:Out of memory: Kill process 26738 (redis-server) score 291 or sacrifice child  Killed process 26738, UID 0, (redis-server) total-vm:5359844kB, anon-rss:5247132kB, file-rss:16kB
Feb 17 15:12:53 zw_53_159 kernel: redis-server invoked oom-killer: gfp_mask=0x201da, order=0, oom_adj=0, oom_score_adj=0
Feb 17 15:12:53 zw_53_159 kernel: redis-server cpuset=/ mems_allowed=0
Feb 17 15:12:53 zw_53_159 kernel: Pid: 32518, comm: redis-server Not tainted 2.6.32-279.el6.x86_64 #1
Feb 17 15:12:53 zw_53_159 kernel: Call Trace:
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff810c4971>] ? cpuset_print_task_mems_allowed+0x91/0xb0
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff811170e0>] ? dump_header+0x90/0x1b0
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff812146fc>] ? security_real_capable_noaudit+0x3c/0x70
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff81117562>] ? oom_kill_process+0x82/0x2a0
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff811174a1>] ? select_bad_process+0xe1/0x120
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff811179a0>] ? out_of_memory+0x220/0x3c0
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff811276be>] ? __alloc_pages_nodemask+0x89e/0x940
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff8115c1da>] ? alloc_pages_current+0xaa/0x110
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff811144e7>] ? __page_cache_alloc+0x87/0x90
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff8112a10b>] ? __do_page_cache_readahead+0xdb/0x210
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff8112a261>] ? ra_submit+0x21/0x30
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff81115813>] ? filemap_fault+0x4c3/0x500
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff8113ec14>] ? __do_fault+0x54/0x510
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff8113f1c7>] ? handle_pte_fault+0xf7/0xb50
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff8113fe04>] ? handle_mm_fault+0x1e4/0x2b0
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff81044479>] ? __do_page_fault+0x139/0x480
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff81039678>] ? pvclock_clocksource_read+0x58/0xd0
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff8103876c>] ? kvm_clock_read+0x1c/0x20
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff81038779>] ? kvm_clock_get_cycles+0x9/0x10
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff8109c9a0>] ? getnstimeofday+0x60/0xf0
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff8150326e>] ? do_page_fault+0x3e/0xa0
Feb 17 15:12:53 zw_53_159 kernel: [<ffffffff81500625>] ? page_fault+0x25/0x30
Feb 17 15:12:53 zw_53_159 kernel: Mem-Info:
Feb 17 15:12:53 zw_53_159 kernel: Node 0 DMA per-cpu:
Feb 17 15:12:53 zw_53_159 kernel: CPU    0: hi:    0, btch:   1 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    1: hi:    0, btch:   1 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    2: hi:    0, btch:   1 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    3: hi:    0, btch:   1 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    4: hi:    0, btch:   1 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    5: hi:    0, btch:   1 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    6: hi:    0, btch:   1 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    7: hi:    0, btch:   1 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    8: hi:    0, btch:   1 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    9: hi:    0, btch:   1 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU   10: hi:    0, btch:   1 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU   11: hi:    0, btch:   1 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU   12: hi:    0, btch:   1 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU   13: hi:    0, btch:   1 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU   14: hi:    0, btch:   1 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU   15: hi:    0, btch:   1 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: Node 0 DMA32 per-cpu:
Feb 17 15:12:53 zw_53_159 kernel: CPU    0: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    1: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    2: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    3: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    4: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    5: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    6: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    7: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    8: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    9: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU   10: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU   11: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU   12: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU   13: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU   14: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU   15: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: Node 0 Normal per-cpu:
Feb 17 15:12:53 zw_53_159 kernel: CPU    0: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    1: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    2: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    3: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    4: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    5: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    6: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    7: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    8: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU    9: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU   10: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU   11: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU   12: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU   13: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU   14: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: CPU   15: hi:  186, btch:  31 usd:   0
Feb 17 15:12:53 zw_53_159 kernel: active_anon:3223340 inactive_anon:756981 isolated_anon:9
Feb 17 15:12:53 zw_53_159 kernel: active_file:234 inactive_file:91 isolated_file:0
Feb 17 15:12:53 zw_53_159 kernel: unevictable:0 dirty:0 writeback:0 unstable:0
Feb 17 15:12:53 zw_53_159 kernel: free:33241 slab_reclaimable:2879 slab_unreclaimable:8604
Feb 17 15:12:53 zw_53_159 kernel: mapped:139 shmem:55 pagetables:10616 bounce:0
Feb 17 15:12:53 zw_53_159 kernel: Node 0 DMA free:15676kB min:60kB low:72kB high:88kB active_anon:0kB inactive_anon:0kB active_file:0kB inactive_file:0kB unevictable:0kB isolated(anon):0kB isolated(file):0kB present:15272kB mlocked:0kB dirty:0kB writeback:0kB mapped:0kB shmem:0kB slab_reclaimable:0kB slab_unreclaimable:0kB kernel_stack:0kB pagetables:0kB unstable:0kB bounce:0kB writeback_tmp:0kB pages_scanned:0 all_unreclaimable? yes
Feb 17 15:12:53 zw_53_159 kernel: lowmem_reserve[]: 0 3512 16137 16137
Feb 17 15:12:53 zw_53_159 kernel: Node 0 DMA32 free:65068kB min:14692kB low:18364kB high:22036kB active_anon:3141792kB inactive_anon:404kB active_file:48kB inactive_file:32kB unevictable:0kB isolated(anon):0kB isolated(file):0kB present:3596496kB mlocked:0kB dirty:0kB writeback:0kB mapped:80kB shmem:4kB slab_reclaimable:204kB slab_unreclaimable:48kB kernel_stack:16kB pagetables:276kB unstable:0kB bounce:0kB writeback_tmp:0kB pages_scanned:0 all_unreclaimable? no
Feb 17 15:12:53 zw_53_159 kernel: lowmem_reserve[]: 0 0 12625 12625
Feb 17 15:12:53 zw_53_159 kernel: Node 0 Normal free:52220kB min:52824kB low:66028kB high:79236kB active_anon:9751568kB inactive_anon:3027520kB active_file:888kB inactive_file:332kB unevictable:0kB isolated(anon):36kB isolated(file):0kB present:12928000kB mlocked:0kB dirty:0kB writeback:0kB mapped:476kB shmem:216kB slab_reclaimable:11312kB slab_unreclaimable:34368kB kernel_stack:3472kB pagetables:42188kB unstable:0kB bounce:0kB writeback_tmp:0kB pages_scanned:146 all_unreclaimable? no
Feb 17 15:12:53 zw_53_159 kernel: lowmem_reserve[]: 0 0 0 0
Feb 17 15:12:53 zw_53_159 kernel: Node 0 DMA: 3*4kB 2*8kB 0*16kB 1*32kB 2*64kB 1*128kB 0*256kB 0*512kB 1*1024kB 1*2048kB 3*4096kB = 15676kB
Feb 17 15:12:53 zw_53_159 kernel: Node 0 DMA32: 81*4kB 73*8kB 64*16kB 48*32kB 36*64kB 31*128kB 29*256kB 30*512kB 30*1024kB 1*2048kB 0*4096kB = 65292kB
Feb 17 15:12:53 zw_53_159 kernel: Node 0 Normal: 753*4kB 473*8kB 314*16kB 216*32kB 162*64kB 86*128kB 31*256kB 10*512kB 0*1024kB 0*2048kB 0*4096kB = 53164kB
Feb 17 15:12:53 zw_53_159 kernel: 335 total pagecache pages
Feb 17 15:12:53 zw_53_159 kernel: 0 pages in swap cache
Feb 17 15:12:53 zw_53_159 kernel: Swap cache stats: add 53710499, delete 53710499, find 25961598/29739317
Feb 17 15:12:53 zw_53_159 kernel: Free swap  = 0kB
Feb 17 15:12:53 zw_53_159 kernel: Total swap = 0kB
Feb 17 15:12:53 zw_53_159 kernel: 4194303 pages RAM
Feb 17 15:12:53 zw_53_159 kernel: 111523 pages reserved
Feb 17 15:12:53 zw_53_159 kernel: 1456 pages shared
Feb 17 15:12:53 zw_53_159 kernel: 4044970 pages non-shared
Feb 17 15:12:53 zw_53_159 kernel: [ pid ]   uid  tgid total_vm      rss cpu oom_adj oom_score_adj name
Feb 17 15:12:53 zw_53_159 kernel: [  651]     0   651     2750      188   0     -17         -1000 udevd
Feb 17 15:12:53 zw_53_159 kernel: [ 1494]     0  1494     1538       27   1       0             0 portreserve
Feb 17 15:12:53 zw_53_159 kernel: [ 1501]     0  1501    62992     1755   0       0             0 rsyslogd
Feb 17 15:12:53 zw_53_159 kernel: [ 1518]     0  1518     2763      194   0     -17         -1000 udevd
Feb 17 15:12:53 zw_53_159 kernel: [ 1543]     0  1543     2285       55   2       0             0 irqbalance
Feb 17 15:12:53 zw_53_159 kernel: [ 1561]    32  1561     4742       58   0       0             0 rpcbind
Feb 17 15:12:53 zw_53_159 kernel: [ 1746]     0  1746    19667      210   4       0             0 master
Feb 17 15:12:53 zw_53_159 kernel: [ 1766]    89  1766    19729      213   0       0             0 qmgr
Feb 17 15:12:53 zw_53_159 kernel: [ 1778]     0  1778    27541       36   0       0             0 abrtd
Feb 17 15:12:53 zw_53_159 kernel: [ 1786]     0  1786    27016       53   6       0             0 abrt-dump-oops
Feb 17 15:12:53 zw_53_159 kernel: [ 1810]     0  1810     1754       27   8       0             0 rhsmcertd
Feb 17 15:12:53 zw_53_159 kernel: [ 1855]     0  1855     1014       21   5       0             0 mingetty
Feb 17 15:12:53 zw_53_159 kernel: [ 1857]     0  1857     1014       22   2       0             0 mingetty
Feb 17 15:12:53 zw_53_159 kernel: [ 1859]     0  1859     1014       22   2       0             0 mingetty
Feb 17 15:12:53 zw_53_159 kernel: [ 1861]     0  1861     1014       21   5       0             0 mingetty
Feb 17 15:12:53 zw_53_159 kernel: [ 1862]     0  1862     2749      187   9     -17         -1000 udevd
Feb 17 15:12:53 zw_53_159 kernel: [ 1864]     0  1864     1014       22   3       0             0 mingetty
Feb 17 15:12:53 zw_53_159 kernel: [ 1865]     0  1865     1017       22   0       0             0 agetty
Feb 17 15:12:53 zw_53_159 kernel: [ 1867]     0  1867     1014       22  13       0             0 mingetty
Feb 17 15:12:53 zw_53_159 kernel: [ 2502]     0  2502     9198      108  12       0             0 keepalived
Feb 17 15:12:53 zw_53_159 kernel: [ 2503]     0  2503     9724      140   4       0             0 keepalived
Feb 17 15:12:53 zw_53_159 kernel: [ 2504]     0  2504     9724      127   6       0             0 keepalived
Feb 17 15:12:53 zw_53_159 kernel: [ 2695]     0  2695     9198      108   8       0             0 keepalived
Feb 17 15:12:53 zw_53_159 kernel: [ 2696]     0  2696     9724      141   8       0             0 keepalived
Feb 17 15:12:53 zw_53_159 kernel: [ 2697]     0  2697     9724      128  12       0             0 keepalived
Feb 17 15:12:53 zw_53_159 kernel: [ 2515]     0  2515     5066      127   0       0             0 nginx
Feb 17 15:12:53 zw_53_159 kernel: [31653]     0 31653     1561       24   0       0             0 oscmd
Feb 17 15:12:53 zw_53_159 kernel: [16865]    99 16865     5166      225   8       0             0 nginx
Feb 17 15:12:53 zw_53_159 kernel: [16866]    99 16866     5166      225   0       0             0 nginx
Feb 17 15:12:53 zw_53_159 kernel: [16867]    99 16867     5166      225  12       0             0 nginx
Feb 17 15:12:53 zw_53_159 kernel: [16868]    99 16868     5166      225   2       0             0 nginx
Feb 17 15:12:53 zw_53_159 kernel: [16869]    99 16869     5166      225  15       0             0 nginx
Feb 17 15:12:53 zw_53_159 kernel: [16870]    99 16870     5166      225   5       0             0 nginx
Feb 17 15:12:53 zw_53_159 kernel: [16871]    99 16871     5166      225   0       0             0 nginx
Feb 17 15:12:53 zw_53_159 kernel: [16872]    99 16872     5166      225   2       0             0 nginx
Feb 17 15:12:53 zw_53_159 kernel: [26947]     0 26947    16018      166   0     -17         -1000 sshd
Feb 17 15:12:53 zw_53_159 kernel: [ 2739]     0  2739    29303      159  10       0             0 crond
Feb 17 15:12:53 zw_53_159 kernel: [ 6394]     0  6394    19966      172   0       0             0 monitor
Feb 17 15:12:53 zw_53_159 kernel: [ 6401]     0  6401    64605     3298  14       0             0 monitor_cron
Feb 17 15:12:53 zw_53_159 kernel: [ 6402]     0  6402    19529      117   0       0             0 monitor_agent
Feb 17 15:12:53 zw_53_159 kernel: [31724]     0 31724     4816       40   2       0             0 nutcracker
Feb 17 15:12:53 zw_53_159 kernel: [ 7144]     0  7144    34895     1490   4       0             0 redis-server
Feb 17 15:12:53 zw_53_159 kernel: [31641]     0 31641    34361     1517  10       0             0 redis-server
Feb 17 15:12:53 zw_53_159 kernel: [31649]     0 31649    34361     2098  14       0             0 redis-server
Feb 17 15:12:53 zw_53_159 kernel: [32512]     0 32512    34361     1614   7       0             0 redis-sentinel
Feb 17 15:12:53 zw_53_159 kernel: [32518]     0 32518    34361     1614   0       0             0 redis-server
Feb 17 15:12:53 zw_53_159 kernel: [32524]     0 32524    34361     1614  13       0             0 redis-sentinel
Feb 17 15:12:53 zw_53_159 kernel: [ 7333]     0  7333    34361     1507   4       0             0 redis-server
Feb 17 15:12:53 zw_53_159 kernel: [17108]     0 17108   180793     1783  13       0             0 redis-server
Feb 17 15:12:53 zw_53_159 kernel: [21470]     0 21470   851513   785994   7       0             0 redis-server
Feb 17 15:12:53 zw_53_159 kernel: [ 5708]     0  5708    34361     2125   6       0             0 redis-server
Feb 17 15:12:53 zw_53_159 kernel: [ 5716]     0  5716    34361     2061   4       0             0 redis-server
Feb 17 15:12:53 zw_53_159 kernel: [ 5724]     0  5724    34361     2122  10       0             0 redis-server
Feb 17 15:12:53 zw_53_159 kernel: [ 5732]     0  5732    34361     2124   7       0             0 redis-server
Feb 17 15:12:53 zw_53_159 kernel: [ 6214]     0  6214    34361     2091   6       0             0 redis-server
Feb 17 15:12:53 zw_53_159 kernel: [29556]     0 29556    34361     1563   4       0             0 redis-server
Feb 17 15:12:53 zw_53_159 kernel: [32104]     0 32104    34361     1614  13       0             0 redis-sentinel
Feb 17 15:12:53 zw_53_159 kernel: [32539]     0 32539    34361     1614  11       0             0 redis-sentinel
Feb 17 15:12:53 zw_53_159 kernel: [14316]     0 14316   442937     8430   4       0             0 redis-server
Feb 17 15:12:53 zw_53_159 kernel: [32038]     0 32038    24454      234   0       0             0 sshd
Feb 17 15:12:53 zw_53_159 kernel: [32042]     0 32042    27119      128   1       0             0 bash
Feb 17 15:12:53 zw_53_159 kernel: [ 3901]     0  3901    24454      231   0       0             0 sshd
Feb 17 15:12:53 zw_53_159 kernel: [ 3905]     0  3905    27118      117   5       0             0 bash
Feb 17 15:12:53 zw_53_159 kernel: [19921]    89 19921    19691      206   6       0             0 pickup
Feb 17 15:12:53 zw_53_159 kernel: [20382]     0 20382    24454      235   0       0             0 sshd
Feb 17 15:12:53 zw_53_159 kernel: [20386]     0 20386    27119      116  10       0             0 bash
Feb 17 15:12:53 zw_53_159 kernel: [20412]     0 20412    25235       22   6       0             0 tail
Feb 17 15:12:53 zw_53_159 kernel: [20413]     0 20413    25813       34   6       0             0 grep
Feb 17 15:12:53 zw_53_159 kernel: [21515]     0 21515    24454      236   0       0             0 sshd
Feb 17 15:12:53 zw_53_159 kernel: [21519]     0 21519    27119      119   0       0             0 bash
Feb 17 15:12:53 zw_53_159 kernel: [21725]     0 21725    24454      237  13       0             0 sshd
Feb 17 15:12:53 zw_53_159 kernel: [21729]     0 21729    27119      117   1       0             0 bash
Feb 17 15:12:53 zw_53_159 kernel: [21960]     0 21960    24454      234  10       0             0 sshd
Feb 17 15:12:53 zw_53_159 kernel: [21964]     0 21964    27119      125   5       0             0 bash
Feb 17 15:12:53 zw_53_159 kernel: [22001]     0 22001    24454      247  13       0             0 sshd
Feb 17 15:12:53 zw_53_159 kernel: [22005]     0 22005    27119      118   0       0             0 bash
Feb 17 15:12:53 zw_53_159 kernel: [22039]     0 22039    24454      234  10       0             0 sshd
Feb 17 15:12:53 zw_53_159 kernel: [22043]     0 22043    27119      127   8       0             0 bash
Feb 17 15:12:53 zw_53_159 kernel: [26738]     0 26738  1339961  1311787   1       0             0 redis-server
Feb 17 15:12:53 zw_53_159 kernel: [26910]     0 26910  1339961  1311787  14       0             0 redis-server
Feb 17 15:12:53 zw_53_159 kernel: [27566]     0 27566    25235       22  10       0             0 tail
Feb 17 15:12:53 zw_53_159 kernel: [27567]     0 27567    25813       32  10       0             0 grep
Feb 17 15:12:53 zw_53_159 kernel: [27805]     0 27805   815673   522185  13       0             0 redis-server
Feb 17 15:12:53 zw_53_159 kernel: Out of memory: Kill process 26738 (redis-server) score 291 or sacrifice child
Feb 17 15:12:53 zw_53_159 kernel: Killed process 26738, UID 0, (redis-server) total-vm:5359844kB, anon-rss:5247132kB, file-rss:16kB
^C

 

   

三、結論:

 

   overcommit機制雖然能夠保證,每次申請記憶體都能成功,但是也存在使用者程式被OOM killer的可能性,如果設定也會存在很大問題。
   具體要設定成0或者>0,還是要取決於場景,注意本實驗沒有開啟swap。
   測試伺服器關閉swap的方法是swapoff -a,未線上上測試過會有什麼影響,請小心使用。
 

 四、後續測試:

 

  1. 新增了swap之後,記憶體概括如下:
  
[@zw_53_159 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:         15948        361      15587          0          3         13
-/+ buffers/cache:        344      15604
Swap:        16383       0      16383

 

  2. 使用第二節進行測試,發現OOM killer是針對可用記憶體(實體記憶體+swap)才生效,不像網上有些文章說的只要實體記憶體不夠,就執行OOM killer
  

 五、參考文獻

 

  有關swap開關可以參考:Linux服務端swap配置和監控
 
 
 
 
 
 
 
 
 


相關文章