今天驚愕地發現,主節點上8G內存被不知道什么進程吃掉了整整6G有余,正常的計算快要維持不下去了,遂處理之。
先看看內存使用狀況
[root@node1 ~]# free -m
total used free shared buffers cached
Mem: 8004 6557 1446 0 163 5630
-/+ buffers/cache: 763 7240
Swap: 1983 0 1983
其中:
total 內存總數
used 已經使用的內存數
free 空閑的內存數
shared 多個進程共享的內存總額
buffers Buffer Cache和cached Page Cache 磁盤緩存的大小
-buffers/cache 的內存數:used - buffers - cached
+buffers/cache 的內存數:free + buffers + cached
可用的memory=free memory+buffers+cached
把內存里的數據暫時寫到硬盤里
[root@node1 ~]# sync
修改 /proc/sys/vm/drop_caches文件(/proc/sys/vm/drop_caches的默認值為0)
[root@node1 ~]# echo 3 > /proc/sys/vm/drop_caches
[root@node1 ~]# cat /proc/sys/vm/drop_caches
再看內存
[root@node1 ~]# free -m
total used free shared buffers cached
Mem: 8004 631 7372 0 0 60
-/+ buffers/cache: 570 7433
Swap: 1983 0 1983
終于釋放出來了。
有關/proc/sys/vm/drop_caches的用法在下面進行了說明(原文:http://www.linuxinsight.com/proc_sys_vm_drop_caches.html):
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.
This tunable was added in 2.6.16.
真實情況是 “當有某程序需要占用大內存時linux會自動釋放cache”
linux的理念是內存空著不用白不用,因此會使用空閑內存來做buffer和cache。
我的一臺虛擬機運行了JBOSS,在運行一段時間后,查看內存使用情況:
[root@JBOSS ~]# free -m
total used free shared buffers cached
Mem: 3949 2928 1021 0 308 1278
-/+ buffers/cache: 1341 2608
Swap: 4094 0 4094
可以看出,內存已用2928M,但實際除去 buffers/cache后實際只使用了1341M。
你若不管它,內存是不會釋放的,而且也沒必要釋放。