淺談linux性能調優之十一:內存分配管理

    linux下內存分配的管理主要經過內核參數來控制:
    1.與容量相關的內存可調參數
      如下參數位於 proc 文件系統的 /proc/sys/vm/ 目錄中。
      overcommit_memory :規定決定是否接受超大內存請求的條件。這個參數有三個可能的值:
         * 0 — 默認設置。內核執行啓發式內存過量使用處理,方法是估算可用內存量,並拒絕明顯無效的請求。遺憾的是由於內存是使用啓發式而非準確算法計算進行部署,這個設置有時可能會形成系統中的可用內存超載。不讓過分使用,直接報錯
         * 1 — 內核執行無內存過量使用處理。使用這個設置會增大內存超載的可能性,但也能夠加強大量使用內存任務的性能。應用程序在須要時分配,容許過分使用
         * 2 — 內存拒絕等於或者大於總可用 swap 大小以及 overcommit_ratio 指定的物理 RAM 比例的內存請求。若是您但願減少內存過分使用的風險,這個設置就是最好的。 將swap直接使用,使用的內存 = swap + ram * 50%
            注意:只爲 swap 區域大於其物理內存的系統推薦這個設置
      overcommit_ratio
         將 overcommit_memory 設定爲 2 時,指定所考慮的物理 RAM 比例。默認爲 50。
測試程序:
###############################################################################
#include "stdio.h"
#include "stdlib.h"
#include "errno.h"

#define maxtimes 1000*1000
#define unit 10485760
int main()
{
    char *q[1000000];
    int i=1;
    char *pc;
    for (i=1; i<=maxtimes;i++)
        {
                q[i] = (char *)malloc(unit);
                if ( q[i] == NULL)
                {
                        printf ("malloc error\n");
                }
                printf("%o\n",q[i]);
                pc = (char*)(q[i]);
                for (int j=0;j<10485760;j++)
                {
                        pc[j]='a';
                }
                printf("%s\n",pc);
                printf("have been malloc %dM\n",i);
        }
    return 0;
}

###############################################################################

    測試1:overcommit_memory = 0 也是默認的狀況,我物理內存2G,開機後查看已使用600M左右,我在這裏關閉了swap分區,編譯來測試,結果:使用top 查看a.out的RES(物理內存的使用)到達了1.3g   進程被Killed (緣由在後面說) 1.3g+600多M = 2G  查看日誌:
Jul 16 14:24:48 localhost kernel: Out of memory: kill process 19066 (a.out) score 131753 or a child
Jul 16 14:24:48 localhost kernel: Killed process 19066 (a.out) vsz:8432216kB, anon-rss:1377108kB, file-rss:92kB

    測試2:overcommit_memory = 1,狀況和測試1一致,
[root@localhost Desktop]# free
             total       used       free     shared    buffers     cached
Mem:       1978696    1873644     105052          0       8268      75012
-/+ buffers/cache:    1790364     188332
      咱們能夠看到:內存在剩100M左右時,a.out先卡住了一會,而後有開始執行了 ,它會一直等待可以使用的資源再執行 (都是別的進程釋放的資源),用來不會被殺死,由於這個模式對內存無限制

    測試3:overcommit_memory = 2,overcommit_ratio默認,都使用,內存會在必定剩餘的狀況下不能malloc
程序執行結果:
have been malloc 266M  這裏是2660
malloc error
0
Segmentation fault (core dumped)

淺談linux性能調優之十一:內存分配管理 - 了了 - OscerSong的博客
 

        o: VIRT  --  Virtual Image (kb)
          The total amount of virtual memory used by the  task.   It  includes
          all  code,  data  and  shared  libraries  plus  pages that have been
          swapped out. (Note: you can define the STATSIZE=1 environment  vari-
          able  and  the VIRT will be calculated from the /proc/#/state VmSize
          field.)

          VIRT = SWAP + RES.

       p: SWAP  --  Swapped size (kb)
          The swapped out portion of a task’s total virtual memory image.

       q: RES  --  Resident size (kb)
          The non-swapped physical memory a task has used.

    

    2.Out-of-Memory Kill 可調參數
    內存不足(OOM)指的是全部可用內存,包括 swap 空間都已被分配的計算狀態。默認狀況下,這個狀態可形成系統 panic,並中止如預期般工做。但將 /proc/sys/vm/panic_on_oom 參數設定爲 0 會讓內核在出現 OOM 時調用 oom_killer 功能。一般 oom_killer 可殺死偷盜進程,並讓系統正常工做。
    可在每一個進程中設定如下參數,提升您對被 oom_killer 功能殺死的進程的控制。它位於 proc 文件系統中 /proc/pid/ 目錄下,其中 pid 是進程 ID。

oom_adj
    定義 -16 到 15 之間的一個數值以便幫助決定某個進程的 oom_score。oom_score 值越高,被 oom_killer 殺死的進程數就越多。將 oom_adj 值設定爲 -17 則爲該進程禁用 oom_killer。

    注意:
    由任意調整的進程衍生的任意進程將繼承該進程的 oom_score。例如:若是 sshd 進程不受 oom_killer 功能影響,全部由 SSH 會話產生的進程都將不受其影響。這可在出現 OOM 時影響 oom_killer 功能救援系統的能力。
相關文章
相關標籤/搜索