Linux內(nèi)存不知去向如何釋放找回

2017-07-21 10:54:12來(lái)源:威易網(wǎng)作者:mou

今天用spot on light 查了一下開(kāi)發(fā)服務(wù)器的內(nèi)存占用,只剩下60MB。用下面的方法就可以實(shí)現(xiàn)清空緩存……

今天用spot on light 查了一下開(kāi)發(fā)服務(wù)器的內(nèi)存占用,只剩下60MB

用下面的方法就可以實(shí)現(xiàn)清空緩存

頻繁的文件訪問(wèn)會(huì)導(dǎo)致系統(tǒng)的Cache使用量大增

首先使用free -m查看剩余內(nèi)存

[root@Oracle ~]# free -m
total used free shared buffers cached
Mem: 3383 3319 63 0 97 2395
-/+ buffers/cache: 826 2556
Swap: 1983 195 1788


total 內(nèi)存總數(shù) used 已經(jīng)使用的內(nèi)存數(shù) free 空閑的內(nèi)存數(shù) shared 多個(gè)進(jìn)程共享的內(nèi)存總額
說(shuō)明,釋放前最好sync一下,防止丟數(shù)據(jù)。

使用方式 : sync

使用說(shuō)明 : Linux 系統(tǒng)中欲寫入硬盤的資料有的時(shí)候會(huì)了效率起見(jiàn),
     會(huì)寫到 filesystem buffer 中,這個(gè) buffer 是一塊記憶體空間,
     如果欲寫入硬盤的資料存于此 buffer 中,而系統(tǒng)又突然斷電的話,
     那么資料就會(huì)流失了,sync 指令會(huì)將存于 buffer 中的資料強(qiáng)制寫入硬盤中。

[root@oracle ~]# echo 1 > /proc/sys/vm/drop_caches
[root@oracle ~]# sysctl -p

[root@oracle ~]# free -m
total used free shared buffers cached
Mem: 3383 1952 1431 0 1 1136
-/+ buffers/cache: 814 2568
Swap: 1983 195 1788

說(shuō)明:
1>. /proc是一個(gè)虛擬文件系統(tǒng),我們可以通過(guò)對(duì)它的讀寫操作作為與kernel實(shí)體間進(jìn)行通信的一種手段。也就是說(shuō)可以通過(guò)修改/proc中的文件,來(lái)對(duì) 當(dāng)前kernel的行為做出調(diào)整。也就是說(shuō)我們可以通過(guò)調(diào)整/proc/sys/vm/drop_caches來(lái)釋放內(nèi)存。
0 – 不釋放
1 – 釋放頁(yè)緩存
2 – 釋放dentries和inodes
3 – 釋放所有緩存
數(shù)字1是用來(lái)清空最近放問(wèn)過(guò)的文件頁(yè)面緩存
數(shù)字2是用來(lái)清空文件節(jié)點(diǎn)緩存和目錄項(xiàng)緩存
數(shù)字3是用來(lái)清空1和2所有內(nèi)容的緩存。

2>. 關(guān)于drop_caches的官方說(shuō)明如下:
Writing to this file causes the kernel to drop clean caches,dentries and inodes from memory, causing that memory to becomefree.
To free pagecache, use echo 1 > /proc/sys/vm/drop_caches;
to free dentries and inodes, use echo 2 > /proc/sys/vm/drop_caches;
to free pagecache, dentries and inodes, use echo 3 >/proc/sys/vm/drop_caches.
Because this is a non-destructive operation and dirty objects are not freeable, the user should run sync first.
 

關(guān)鍵詞:Linux內(nèi)存