顯示具有 Linux 標籤的文章。 顯示所有文章
顯示具有 Linux 標籤的文章。 顯示所有文章

2009年12月26日 星期六

Ubuntu中如何安裝gcc及kernel原始程式

在Ubuntu中安裝任何東西時,可使用apt-get這個好用的程式。例如,要安裝gcc時,只要執行下面指令,就可以安裝完成

sudo apt-get build-dep gcc

若要安裝kernel source,則使用下列指令

sudo apt-get install linux-source

若要安裝kernel source中的一些header檔案,則使用下列指令

sudo apt-get install linux-headers

指令會回傳不同release的list,此時使用uname –r 指令先確定目前的linux release 後,再重新執行

sudo apt-get install linux-headers-2.6.31-14-generic

2009年12月12日 星期六

清除 Linux memory的 cache

有很多種方法可以查看Linux 記憶體的使用狀況,如vmstat、free、nmon等,這裡以 free 指令為例

[root@db01 ~]# free
                                 total           used                  free     shared    buffers     cached
Mem:                       5144296    3008448    2135848          0       8600        2819464
-/+ buffers/cache:       180384    4963912
Swap:      6160376      18484    6141892

這裡buffers是放即將被寫入硬碟資料的緩衝區;而cache則是資料預先自硬碟讀取暫存在記憶體的暫存區。Linux 作業系統傾向會把資料cache在記憶體中,即使應用程式結束,也不會立即將cache的資料清除。

以下介紹如何把cache的資料清除:

Writing to this will cause the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free.

To free pagecache:

  • echo 1 > /proc/sys/vm/drop_caches

To free dentries and inodes:

  • echo 2 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes:

  • echo 3 > /proc/sys/vm/drop_caches

As this is a non-destructive operation, and dirty objects are not freeable, the user should run "sync" first in order to make sure all cached objects are freed.

Linux Memory使用資訊

轉貼自 http://ssorc.tw/rewrite.php/read-599.html

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

由上面的計算可以知道 MEM:Free 欄位是真的沒有在使用的記憶體(已減掉Buffer及Cache);MEM:Used欄位的值包含其它程式在使用的記憶體 + Buffer及Cache;-/+buffers/cache:used:是真實其它程式在使用的記憶體(即Mem:used – Mem:buffers – Mem:cached)


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.
Quote: http://www.ubuntu.org.tw/modules/newbb/viewtopic.php?topic_id=6132

一般情況下,Linux kernel 會盡可能多地利用 RAM 的空閑空間作為 cache/buffer 以最大幅度地提高系統性能。當系統中運行的應用程序占用的 RAM 增加時,則將 cache/buffer 所占用的空間釋放出來,讓渡給應用程序使用。

Quote: http://web.mit.edu/rhel-doc/4/RH-DOCS/rhel-isa-zh_tw-4/s1-resource-rhlspec.html

Mem: 那一行顯示了實際記憶體的使用率;
Swap: 顯示的是系統 swap 空間的使用率;
-/+ buffers/cache: 則是目前撥給系統緩衝區的實體記憶體數量。

Quote: http://wiki.gentoo.tw/mediawiki/index.php/FAQ_LINUXMEMORY

記憶體管理的概觀
當系統開機一段時間後,像是「top」這種傳統的 Unix 工具常常回報少的可憐的可用記憶體數值,在我寫這篇文章的系統中,就算我總共有 512 MB 的記憶體在我的系統裡,但約開機m後三個小時,我只剩下 60 MB 的可用記憶體,那些記憶體到底跑到那裡去了?

用掉最多記憶體的地方是磁碟快取 (disk cache),目前它總共用了超過 290 MB 的記憶 (在 top 裡的「cached」項目中),快取記憶體 (cached memory) 基本上是空閒的,當有新/執行中的程式需要記憶體的話,它會快速的被取回來。

為什麼 Linux 使用這麼多的記憶體來當作磁碟快取 (disk cache) 呢?主要的原因便是:假如 RAM 沒有被使用的話,它便是閒放在那邊浪費著不用。如果把資料放在用 RAM 組成的磁碟上,它的存取速度比直接從硬碟上存取還要快上 1000 倍。假如在快取裡找不到該資料,當然還是得直接從磁碟裡存取,但就如同上面說的,您將可以節省些微的存取時間。

Quote: http://7cc.tw/bencandy.php?fid=18&id=3280

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%,應該是這樣子吧!!!@@