wiki:jazz/11-02-27

2011-02-27

Virtual Memory , HEAP

  • 長久以來一直不是很懂 top 指令中,VIRT 、RES 與 SHR 的意義。今天還是來了解一下 manpage
      PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
    
    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.
    
       VIRT = SWAP + RES.
    
    q: RES  --  Resident size (kb)
       The non-swapped physical memory a task has used.
    
    t: SHR  --  Shared Mem size (kb)
       The amount of shared memory used by a task. It
       simply reflects memory that could be potentially
       shared with other processes.
    
    p: SWAP  --  Swapped size (kb)
       The swapped out portion of a task's total virtual
       memory image.
    
  • 所以 RES 是目前正在使用中的「實體記憶體(physical memory)」,SHR 是不同程序之間共享的記憶體大小,VIRT 則是「虛擬記憶體(virtual memory)」,包括 CODE, DATA, 共享函式庫與 SWAP。
  • 從「Virtual Memory Usage from Java under Linux, too much memory used」這篇討論,學到一個指令:pmap,隸屬於 procps 套件中。用法是 pmap -x $PID (不同身份時,得透過 sudo 取得記憶體狀態)
    procps: /usr/bin/pmap
    
    ~$ sudo pmap -x $PID
    
    jazz@drbl:~$ sudo pmap 24972
    24972:   /usr/bin/php5-cgi
    0000000000400000   5144K r-x--  /usr/bin/php5-cgi
    0000000000b06000    356K rw---  /usr/bin/php5-cgi
    0000000000b5f000     32K rw---    [ anon ]
    00000000010da000   2212K rw---    [ anon ]
    00007f418a02b000     40K r-x--  /lib/libnss_files-2.7.so
    00007f418a035000   2048K -----  /lib/libnss_files-2.7.so
    00007f418a235000      8K rw---  /lib/libnss_files-2.7.so
    00007f418a237000     28K r-x--  /usr/lib/php5/20060613/pdo_mysql.so
    00007f418a23e000   2044K -----  /usr/lib/php5/20060613/pdo_mysql.so
    00007f418a43d000      4K rw---  /usr/lib/php5/20060613/pdo_mysql.so
    ... 略 ...
    
  • 根據「Avoid linux out-of-memory application teardown」這篇討論,如果出現虛擬記憶體使用過多,核心開始執行 out-of-memory(OOM) killer 的話,可以使用以下方法關閉:
       1. Disable the OOM Killer (Put vm.oom-kill = 0 in /etc/sysctl.conf)
       2. Disable memory overcommit (Put vm.overcommit_memory = 2 in /etc/sysctl.conf)
          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")
    
  • 關於 /proc/sys/vm 底下的檔案 - 說明詳見 vm.txt
    linux-doc: /usr/share/doc/linux-doc/sysctl/vm.txt.gz
    
  • 可以透過 sysctl 設定 swap - How do I free virtual memory in Ubuntu?
    $ cat /proc/sys/vm/swappiness
    60
    $ sudo sysctl vm.swappiness=0
    vm.swappiness = 0
    
  • 20.如何取得記憶體的統計資料(memory statistics)? 介紹如何寫 Java 記憶體
    public class MemoryStats{
        public static void main(String args[]){
            long totalMem = Runtime.getRuntime().totalMemory();
            long freeMem = Runtime.getRuntime().freeMemory();
            System.out.println("Total memory"+ totalMem);
            System.out.println("Free memory" + freeMem);
        }
    }
    
Last modified 13 years ago Last modified on Feb 27, 2011, 11:14:46 PM