| 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 | {{{ |
| 10 | o: 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 | |
| 17 | q: RES -- Resident size (kb) |
| 18 | The non-swapped physical memory a task has used. |
| 19 | |
| 20 | t: 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 | |
| 25 | p: 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 | {{{ |
| 32 | procps: /usr/bin/pmap |
| 33 | }}} |
| 34 | {{{ |
| 35 | ~$ sudo pmap -x $PID |
| 36 | }}} |
| 37 | {{{ |
| 38 | jazz@drbl:~$ sudo pmap 24972 |
| 39 | 24972: /usr/bin/php5-cgi |
| 40 | 0000000000400000 5144K r-x-- /usr/bin/php5-cgi |
| 41 | 0000000000b06000 356K rw--- /usr/bin/php5-cgi |
| 42 | 0000000000b5f000 32K rw--- [ anon ] |
| 43 | 00000000010da000 2212K rw--- [ anon ] |
| 44 | 00007f418a02b000 40K r-x-- /lib/libnss_files-2.7.so |
| 45 | 00007f418a035000 2048K ----- /lib/libnss_files-2.7.so |
| 46 | 00007f418a235000 8K rw--- /lib/libnss_files-2.7.so |
| 47 | 00007f418a237000 28K r-x-- /usr/lib/php5/20060613/pdo_mysql.so |
| 48 | 00007f418a23e000 2044K ----- /usr/lib/php5/20060613/pdo_mysql.so |
| 49 | 00007f418a43d000 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 | {{{ |
| 60 | linux-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 |
| 65 | 60 |
| 66 | $ sudo sysctl vm.swappiness=0 |
| 67 | vm.swappiness = 0 |
| 68 | }}} |