當前位置:首頁 > 芯聞號 > 充電吧
[導讀]釋放Linux操作系統(tǒng)文件緩存?? 轉(zhuǎn)自:http://pthread.blog.163.com/blog/static/1693081782011111402639863/ 自從工作了,再

釋放Linux操作系統(tǒng)文件緩存?? 轉(zhuǎn)自:http://pthread.blog.163.com/blog/static/1693081782011111402639863/ 自從工作了,再也沒有更新過這個技術(shù)博客。一來工作了沒什么好寫的,二來確實也挺忙。最近稍微有點空閑,先開一個寫一點吧。 記得在公司做新人習題的時候,題目是通過網(wǎng)絡和本地分別讀取一個約12G的大文件,從中讀取每一行,對每行特定的幾個字段,調(diào)用分詞庫分詞并統(tǒng)計詞頻。當時遇到一個很郁悶的事情就是,12G的文件讀取一次了之后,系統(tǒng)中有緩存;然后第二次再次運行的時候,因為有緩存的影響,性能差異挺大(本地讀取幾乎三倍性能差距)。但是當時的開發(fā)機器上,自己只有普通用戶權(quán)限,無法通過修改/proc/sys/vm/drop_cache來達到目的。所以最后還是沒有搞定這個問題。

后來發(fā)現(xiàn)Linux的一個系統(tǒng)調(diào)用: ?#include ?int posix_fadvise(int fd, off_t offset, off_t len, int advice);
有一個選項POSIX_FADV_DONTNEED可以做這件事情。網(wǎng)上找了下好像挺多人也遇到這個無問題的,所以就把我的解決辦法放到這里。于是寫了一個小工具,一次批量清除文件在系統(tǒng)中的緩存。 ?

#define _FILE_OFFSET_BITS 64 #define __USE_XOPEN2K #include #include #include #include #include #include #include #include const struct option dcache_options[] = { {"sync",0,NULL,'s'}, {"help",0,NULL,'h'}, {NULL,0,NULL,0} }; void usage(char* proc_name,int exit_code) { printf("dcache is an utility to drop file cache.n" "usage:%s [-s] filen" "t-s,--sync, sync data before drop cache.n" "t-h,--help, print help.n",proc_name); exit(exit_code); } int dcache(int fd, int sync_data) { off_t off,len; struct stat buf; int save_errno; save_errno = errno; if (sync_data) { if (fsync(fd) < 0) { printf("%sn",strerror(errno)); errno = save_errno; return -1; } } if (fstat(fd,&buf) < 0) { printf("%sn",strerror(errno)); errno = save_errno; return -1; } off = 0; len = buf.st_size; if (posix_fadvise(fd,off,len,POSIX_FADV_DONTNEED) < 0) { printf("%sn",strerror(errno)); errno = save_errno; return -1; } return 0; } int main(int argc, char* argv[]) { int c,fd; char* file; int long_index = 0; int print_help = 0; int sync_data = 0; while ((c = getopt_long(argc,argv,"sh",dcache_options,&long_index)) != -1) { switch (c) { case 's': sync_data = 1; break; case 'h': print_help = 1; break; default: printf("unknown option -%cn",c); usage(argv[0],EXIT_FAILURE); break; } } if (print_help) { usage(argv[0],EXIT_SUCCESS); } if (optind >= argc) { printf("file name requiredn"); exit(EXIT_FAILURE); } for (c = optind; c < argc; ++c) { file = argv[c]; if ((fd = open(file,O_RDWR)) < 0) { printf("open %s failed.n",file); } else { printf("drop cache of %s %s.n",file,dcache(fd,sync_data) == 0?"success":"failed"); close(fd); } } exit(EXIT_SUCCESS); }

? 使用方法: dcache -h dcache is an utility to drop file cache. usage:dcache [-s] file ? ? ? ? -s,--sync, sync data before drop cache. ? ? ? ? -h,--help, print help.
--sync選項用于將數(shù)據(jù)寫回硬盤。因為man posix_fadvise說了:
?POSIX_FADV_DONTNEED ?attempts ?to ?free ?cached ?pages ?associated with the specified region. ?This is useful, for ?example, whilestreaming large files. ?A program may periodically request the kernel to free cached data that has already ?been ?used, ?so ?that?more useful cached pages are not discarded instead.
?Pages ?that ?have ?not ?yet ?been ?written ?out ?will be unaffected, so if the application wishes to guarantee that pages will be ?released, it should call fsync(2) or fdatasync(2) first.
說POSIX_FADV_DONTNEED只釋放clean頁面,dirty頁面,并不受此影響,所以如果你是寫了文件而沒有用--sync選項的話,那么臟頁面不會被釋放,緩存也就不會被釋放掉啦。所以使用dcache的時候應當清楚何時使用--sync選項。
在公司機器上做了一下實驗,用一個4kw+行的文本文件,8GB做實驗。
先 free -m看一下cached總數(shù)為48345M ? ? ? ? ? ? ?total ? ? ? used ? ? ? free ? ? shared ? ?buffers ? ? cached Mem: ? ? 64334 ? ?50469 ? ?13864 ? ? ? ? ?0 ? ? ? ?196 ? ? ?48345
然后wc -l 5kw.txt,讀取一遍文件,wc輸出文件行數(shù)為 44731963 5kw.txt
然后再free -m一遍看內(nèi)存情況: ? ? ? ? ? ? ?total ? ? ? used ? ? ? free ? ? shared ? ?buffers ? ? cached Mem: ? ? 64334 ? 58802 ? ? 5532 ? ? ? ? ?0 ? ? ? ?204 ? ? ? ?56670 可以看到,cached page增加了8325M,與我們文件大小接近。然后使用dcache工具釋放對應文件在系統(tǒng)中的緩存:
dcache 5kw.txt drop cache of plsi_index.5kw.txt success. 再次使用free -m看到cached果然被釋放了8GB,說明工具確實起到了作用。 ?free -m ? ? ? ? ? ? ?total ? ? ? used ? ? ? free ? ? shared ? ?buffers ? ? cached Mem: ? ?64334 ? ?50477 ? ? ?13856 ? ? ? ? ?0 ? ? ? ?204 ? ? ?48346
本站聲明: 本文章由作者或相關(guān)機構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫毥谦F公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉(zhuǎn)型技術(shù)解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關(guān)鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動 BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務能7×24不間斷運行,同時企業(yè)卻面臨越來越多業(yè)務中斷的風險,如企業(yè)系統(tǒng)復雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報道,騰訊和網(wǎng)易近期正在縮減他們對日本游戲市場的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會上,華為常務董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機 衛(wèi)星通信

要點: 有效應對環(huán)境變化,經(jīng)營業(yè)績穩(wěn)中有升 落實提質(zhì)增效舉措,毛利潤率延續(xù)升勢 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務引領(lǐng)增長 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競爭力 堅持高質(zhì)量發(fā)展策略,塑強核心競爭優(yōu)勢...

關(guān)鍵字: 通信 BSP 電信運營商 數(shù)字經(jīng)濟

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺與中國電影電視技術(shù)學會聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會上宣布正式成立。 活動現(xiàn)場 NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會上,軟通動力信息技術(shù)(集團)股份有限公司(以下簡稱"軟通動力")與長三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉