Changes between Initial Version and Version 1 of jazz/11-02-27


Ignore:
Timestamp:
Feb 27, 2011, 10:16:40 PM (13 years ago)
Author:
jazz
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • jazz/11-02-27

    v1 v1  
     1= 2011-02-27 =
     2
     3== Virtual Memory , HEAP ==
     4
     5 * 長久以來一直不是很懂 top 指令中,VIRT 、RES 與 SHR 的意義。今天還是來了解一下 manpage
     6{{{
     7  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
     8}}}
     9{{{
     10o: VIRT  --  Virtual Image (kb)
     11   The total amount of virtual memory used by the task.
     12   It includes all code, data and shared libraries plus
     13   pages that have been swapped out.
     14
     15   VIRT = SWAP + RES.
     16
     17q: RES  --  Resident size (kb)
     18   The non-swapped physical memory a task has used.
     19
     20t: SHR  --  Shared Mem size (kb)
     21   The amount of shared memory used by a task. It
     22   simply reflects memory that could be potentially
     23   shared with other processes.
     24
     25p: SWAP  --  Swapped size (kb)
     26   The swapped out portion of a task's total virtual
     27   memory image.
     28}}}
     29 * 所以 RES 是目前正在使用中的「實體記憶體(physical memory)」,SHR 是不同程序之間共享的記憶體大小,VIRT 則是「虛擬記憶體(virtual memory)」,包括 CODE, DATA, 共享函式庫與 SWAP。
     30 * 從「[http://stackoverflow.com/questions/561245/virtual-memory-usage-from-java-under-linux-too-much-memory-used Virtual Memory Usage from Java under Linux, too much memory used]」這篇討論,學到一個指令:pmap,隸屬於 [http://packages.debian.org/procps procps 套件]中。用法是 pmap -x $PID (不同身份時,得透過 sudo 取得記憶體狀態)
     31{{{
     32procps: /usr/bin/pmap
     33}}}
     34{{{
     35~$ sudo pmap -x $PID
     36}}}
     37{{{
     38jazz@drbl:~$ sudo pmap 24972
     3924972:   /usr/bin/php5-cgi
     400000000000400000   5144K r-x--  /usr/bin/php5-cgi
     410000000000b06000    356K rw---  /usr/bin/php5-cgi
     420000000000b5f000     32K rw---    [ anon ]
     4300000000010da000   2212K rw---    [ anon ]
     4400007f418a02b000     40K r-x--  /lib/libnss_files-2.7.so
     4500007f418a035000   2048K -----  /lib/libnss_files-2.7.so
     4600007f418a235000      8K rw---  /lib/libnss_files-2.7.so
     4700007f418a237000     28K r-x--  /usr/lib/php5/20060613/pdo_mysql.so
     4800007f418a23e000   2044K -----  /usr/lib/php5/20060613/pdo_mysql.so
     4900007f418a43d000      4K rw---  /usr/lib/php5/20060613/pdo_mysql.so
     50... 略 ...
     51}}}
     52 * 根據「[http://serverfault.com/questions/141988/avoid-linux-out-of-memory-application-teardown Avoid linux out-of-memory application teardown]」這篇討論,如果出現虛擬記憶體使用過多,核心開始執行 out-of-memory(OOM) killer 的話,可以使用以下方法關閉:
     53{{{
     54   1. Disable the OOM Killer (Put vm.oom-kill = 0 in /etc/sysctl.conf)
     55   2. Disable memory overcommit (Put vm.overcommit_memory = 2 in /etc/sysctl.conf)
     56      Note that this is a trinary value: 0 = "estimate if we have enough RAM", 1 = "Always say yes", 2 = "say no if we don't have the memory")
     57}}}
     58 * 關於 /proc/sys/vm 底下的檔案 - 說明詳見 vm.txt
     59{{{
     60linux-doc: /usr/share/doc/linux-doc/sysctl/vm.txt.gz
     61}}}
     62 * 可以透過 sysctl 設定 swap - [http://superuser.com/questions/115565/how-do-i-free-virtual-memory-in-ubuntu How do I free virtual memory in Ubuntu?]
     63{{{
     64$ cat /proc/sys/vm/swappiness
     6560
     66$ sudo sysctl vm.swappiness=0
     67vm.swappiness = 0
     68}}}