Thursday, November 20, 2014

Linux Memory Usage 記憶體使用率

free
                      total         used           free     shared     buffers      cached
Mem:       1023916     975816       48100              0      26376     465844
-/+ buffers/cache:      483596      540320
Swap:      2096440    105564    1990876
計算方式
                      total         used           free     shared     buffers      cached
Mem:                a               b                c              d           e               f
-/+ buffers/cache:             g                h
Swap:               i                j                 k
a = 總記憶體大小
b = 配給 buffers 與 cache 的記憶體大小(包含未用的 buffers 與 cache)
c = 剩下的記憶體大小
e = 配給 buffers 但未用的記憶體大小
f = 配給 cache 但未用的記憶體大小
g = buffers 與 cache 被使用掉的記憶體大小,也就是實際被應用程式用走的
h = 那這個就是實際剩下的記憶體大小
a  = b + c
a  = g + h
g = b  – e  –  f
h = c  + e  + f
buffer 與 cache 的區別:
A buffer is something that has yet to be "written" to disk.
A cache is something that has been "read" from the disk and stored for later use.
一般情況下,Linux kernel 會盡可能多地利用 RAM 的空閑空間作為 cache/buffer 以最大幅度地提高系統性能。當系統中運行的應用程序占用的 RAM 增加時,則將 cache/buffer 所占用的空間釋放出來,讓渡給應用程序使用。
Mem: 那一行顯示了實際記憶體的使用率;
Swap: 顯示的是系統 swap 空間的使用率;
-/+ buffers/cache: 則是目前撥給系統緩衝區的實體記憶體數量。
記憶體管理的概觀
當系統開機一段時間後,像是「top」這種傳統的 Unix 工具常常回報少的可憐的可用記憶體數值,在我寫這篇文章的系統中,就算我總共有 512 MB 的記憶體在我的系統裡,但約開機m後三個小時,我只剩下 60 MB 的可用記憶體,那些記憶體到底跑到那裡去了?
用掉最多記憶體的地方是磁碟快取 (disk cache),目前它總共用了超過 290 MB 的記憶 (在 top 裡的「cached」項目中),快取記憶體 (cached memory) 基本上是空閒的,當有新/執行中的程式需要記憶體的話,它會快速的被取回來。
為什麼 Linux 使用這麼多的記憶體來當作磁碟快取 (disk cache) 呢?主要的原因便是:假如 RAM 沒有被使用的話,它便是閒放在那邊浪費著不用。如果把資料放在用 RAM 組成的磁碟上,它的存取速度比直接從硬碟上存取還要快上 1000 倍。假如在快取裡找不到該資料,當然還是得直接從磁碟裡存取,但就如同上面說的,您將可以節省些微的存取時間。
Free中的buffer和cache︰(它們都是佔用內存)︰
  buffer : 作為buffer cache的內存,是塊設備的讀寫緩沖區
  cache: 作為page cache的內存, 文件系統的cache
  如果 cache 的值很大,說明cache住的文件數很多。如果頻繁訪問到的文件都能被cache住,那麼磁盤的讀IO bi會非常小。
/proc/meminfo 解釋參考: http://www.redhat.com/advice/tips/meminfo.html
ref: http://www.wujianrong.com/archives/linux/2.html
ref: http://linux.chinaunix.net/bbs/viewthread.php?tid=887896
對 Memory Usage 的詳細說明及當 Out of Memory 的解決方法參考: http://rimuhosting.com/howto/memory.jsp
Out of Memory
      more /var/log/messages
kernel: Mem-info:
kernel: Zone:DMA freepages:  2916 min:     0 low:     0 high:     0
kernel: Zone:Normal freepages:   758 min:   766 low:  4031 high:  5791
kernel: Zone:HighMem freepages:   125 min:   253 low:   506 high:   759
kernel: Free pages:        3799 (   125 HighMem)
kernel: ( Active: 237940/1195, inactive_laundry: 1, inactive_clean: 0, free: 3799 )
kernel:   aa:0 ac:0 id:0 il:0 ic:0 fr:2916
kernel:   aa:209592 ac:2087 id:1190 il:1 ic:0 fr:758
kernel:   aa:26033 ac:232 id:0 il:0 ic:0 fr:125
kernel: 0*4kB 2*8kB 4*16kB 2*32kB 4*64kB 2*128kB 1*256kB 1*512kB 0*1024kB 1*2048kB 2*4096kB = 11664kB)
kernel: 8*4kB 1*8kB 175*16kB 6*32kB 0*64kB 0*128kB 0*256kB 0*512kB 0*1024kB 0*2048kB 0*4096kB = 3032kB)
kernel: 1*4kB 0*8kB 1*16kB 1*32kB 1*64kB 1*128kB 1*256kB 0*512kB 0*1024kB 0*2048kB 0*4096kB = 500kB)
kernel: Swap cache: add 5156731, delete 5156620, find 19439534/21006374, race 0+19
kernel: 4115 pages of slabcache
kernel: 1566 pages of kernel stacks
kernel: 13 lowmem pagetables, 5378 highmem pagetables
kernel: 32 bounce buffer pages, 32 are on the emergency list
kernel: Free swap:            0kB
kernel: 261760 pages of RAM
kernel: 32384 pages of HIGHMEM
kernel: 5781 reserved pages
kernel: 14591 pages shared
kernel: 116 pages swap cached
kernel: Out of Memory: Killed process 25580 (java).
Quote: http://rimuhosting.com/howto/memory.jsp Resolving: High Java Memory Usage
To determine how much memory you can spare for Java, try this: stop your Java process; run free -m; subtract the 'used' value from the "-/+ cache" row from the total memory allocated to your server and then subtract another 'just in case' margin of about 10% of your total server memory.  The number you come up with is a rough indicator of the largest -Xmx setting you can use on your server.
意思就是配給 -Xmx數值多少m = a – g – a x 10%,應該是這樣子吧!!!@@

http://ssorc.tw/599

No comments: