1. 如何手動釋放linux內存的方法
1、首先打開Linux命令窗口,可使用快捷鍵Ctrl+Alt+T打開。
2. 如何為linux釋放緩存
當在Linux下頻繁存取文件後,物理內存會很快被用光,當程序結束後,內存不會被正常釋放,而是一直作為caching。這個問題,貌似有不少人在問,不過都沒有看到有什麼很好解決的辦法。那麼我來談談這個問題。
一、通常情況
先來說說free命令:
# free -m
total used free shared buffers cached
Mem: 249 163 86 0 10 94
-/+ buffers/Cache: 58 191
SWAP: 511 0 511
其中:
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
有了這個基礎後,可以得知,我現在used為163MB,free為86MB,buffer和cached分別為10MB,94MB。
那麼我們來看看,如果我執行復制文件,內存會發生什麼變化。
# cp -r /etc ~/test/
# free -m
total used free shared buffers cached
Mem: 249 244 4 0 8 174
-/+ buffers/cache: 62 187
Swap: 511 0 511
在我命令執行結束後,used為244MB,free為4MB,buffers為8MB,cached為174MB,天吶,都被cached吃掉了。別緊張,這是為了提高文件讀取效率的做法。
為了提高磁碟存取效率,Linux做了一些精心的設計,除了對dentry進行緩存(用於VFS,加速文件路徑名到inode的轉換),還採取了兩種主要Cache方式:Buffer Cache和Page Cache。前者針對磁碟塊的讀寫,後者針對文件inode的讀寫。這些Cache有效縮短了 I/O系統調用(比如read,write,getdents)的時間。
那麼有人說過段時間,linux會自動釋放掉所用的內存。等待一段時間後,我們使用free再來試試,看看是否有釋放?
# free -m
total used free shared buffers cached
Mem: 249 244 5 0 8 174
-/+ buffers/cache: 61 188
Swap: 511 0 511
似乎沒有任何變化。(實際情況下,內存的管理還與Swap有關)那麼我能否手動釋放掉這些內存呢?回答是可以的!
二、手動釋放緩存
/proc是一個虛擬文件系統,我們可以通過對它的讀寫操作做為與Kernel實體間進行通信的一種手段。也就是說可以通過修改/proc中的文件,來對當前kernel的行為做出調整。那麼我們可以通過調整/proc/sys/vm/drop_caches來釋放內存。操作如下:
# cat /proc/sys/vm/drop_caches
0
首先,/proc/sys/vm/drop_caches的值,默認為0。
# sync
手動執行sync命令(描述:sync 命令運行 sync 子常式。如果必須停止系統,則運行sync 命令以確保文件系統的完整性。sync 命令將所有未寫的系統緩沖區寫到磁碟中,包含已修改的 i-Node、已延遲的塊 I/O 和讀寫映射文件)
# echo 3 > /proc/sys/vm/drop_caches
# cat /proc/sys/vm/drop_caches
3
將/proc/sys/vm/drop_caches值設為3
# free -m
total used free shared buffers cached
Mem: 249 66 182 0 0 11
-/+ buffers/cache: 55 194
Swap: 511 0 511
再來運行free命令,會發現現在的used為66MB,free為182MB,buffers為0MB,cached為11MB。那麼有效的釋放了buffer和cache。
有關/proc/sys/vm/drop_caches的用法在下面進行了說明
/proc/sys/vm/drop_caches (since Linux 2.6.16)
Writing to this file causes the kernel to drop clean caches,dentries and inodes from memory, causing that memory to become free.
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.
3. Linux c如何清除標准輸入緩存區數據
gcc可以用fgets或者其他函數將緩沖區數據讀出來清除輸入緩沖區。gcc不支持用fflush清除輸入緩沖區,標准上也沒有定義,但是vc是可以的。
看一下這個:http://blog.csdn.net/thecorr/article/details/1922541
和這個:http://liustb.blog.163.com/blog/static/133708019201131111471597/
4. 如何在Linux操作系統中手工釋放內存緩存
要達到釋放緩存的目的,我們首先需要了解下關鍵的配置文件/proc/sys/vm/drop_caches。這個文件中記錄了緩存釋放的參數,默認值為0,也就是不釋放緩存。他的值可以為0~3之間的任意數字,代表著不同的含義:
0 – 不釋放
1 – 釋放頁緩存
2 – 釋放dentries和inodes
3 – 釋放所有緩存
知道了參數後,我們就可以根據我們的需要,使用下面的指令來進行操作。
首先我們需要使用sync指令,將所有未寫的系統緩沖區寫到磁碟中,包含已修改的 i-node、已延遲的塊 I/O 和讀寫映射文件。否則在釋放緩存的過程中,可能會丟失未保存的文件。
#sync
接下來,我們需要將需要的參數寫進/proc/sys/vm/drop_caches文件中,比如我們需要釋放所有緩存,就輸入下面的命令:
#echo 3 > /proc/sys/vm/drop_caches
此指令輸入後會立即生效,可以查詢現在的可用內存明顯的變多了。
要查詢當前緩存釋放的參數,可以輸入下面的指令:
#cat /proc/sys/vm/drop_caches
下面我們來看一個實際測試:
先看看內存使用狀況
[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
把內存里的數據暫時寫到硬碟里
[root@node1 ~]# sync
修改/proc/sys/vm/drop_caches文件
[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