• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 297,  comments - 15,  trackbacks - 0

            細心的朋友會注意到,當你在linux下頻繁存取文件后,物理內(nèi)存會很快被用光,當程序結(jié)束后,內(nèi)存不會被正常釋放,而是一直作為caching.這個問題,貌似有不少人在問,不過都沒有看到有什么很好解決的辦法.那么我來談談這個問題.

            先來說說free命令

            [root@server ~]# free -m
                         total       used       free     shared    buffers     cached
            Mem:           249        163         86          0         10         94
            -/+ buffers/cache:         58        191
            Swap:          511          0        511

            其中:

            total 內(nèi)存總數(shù)

            used 已經(jīng)使用的內(nèi)存數(shù)

            free 空閑的內(nèi)存數(shù)

            shared 多個進程共享的內(nèi)存總額

            buffers Buffer Cache和cached Page Cache 磁盤緩存的大小

            -buffers/cache 的內(nèi)存數(shù):used - buffers - cached

            +buffers/cache 的內(nèi)存數(shù):free + buffers + cached

            可用的memory=free memory+buffers+cached

            有了這個基礎(chǔ)后,可以得知,我現(xiàn)在used為163MB,free為86,buffer和cached分別為10,94

            那么我們來看看,如果我執(zhí)行復制文件,內(nèi)存會發(fā)生什么變化.

            [root@server ~]# cp -r /etc ~/test/
            [root@server ~]# free -m
                         total       used       free     shared    buffers     cached
            Mem:           249        244          4          0          8        174
            -/+ buffers/cache:         62        187
            Swap:          511          0        511

            在我命令執(zhí)行結(jié)束后,used為244MB,free為4MB,buffers為8MB,cached為174MB,天吶都被cached吃掉了.別緊張,這是為了提高文件讀取效率的做法.

            引用Buffer Cache和Page Cache。前者針對磁盤塊的讀寫,后者針對文件inode的讀寫。這些Cache有效縮短了 I/O系統(tǒng)調(diào)用(比如read,write,getdents)的時間。"

            那么有人說過段時間,linux會自動釋放掉所用的內(nèi)存,我們使用free再來試試,看看是否有釋放>?

            [root@server test]# free -m
                         total       used       free     shared    buffers     cached
            Mem:           249        244          5          0          8        174
            -/+ buffers/cache:         61        188
            Swap:          511          0        511

            MS沒有任何變化,那么我能否手動釋放掉這些內(nèi)存呢???回答是可以的!

            /proc是一個虛擬文件系統(tǒng),我們可以通過對它的讀寫操作做為與kernel實體間進 行通信的一種手段.也就是說可以通過修改/proc中的文件,來對當前kernel的行為做出調(diào)整.那么我們可以通過調(diào)整/proc/sys/vm /drop_caches來釋放內(nèi)存.操作如下:

            [root@server test]# cat /proc/sys/vm/drop_caches
            0
            首先,/proc/sys/vm/drop_caches的值,默認為0

            [root@server test]# sync

            手動執(zhí)行sync命令(描述:sync 命令運行 sync 子例程。如果必須停止系統(tǒng),則運行 sync 命令以確保文件系統(tǒng)的完整性。sync 命令將所有未寫的系統(tǒng)緩沖區(qū)寫到磁盤中,包含已修改的 i-node、已延遲的塊 I/O 和讀寫映射文件)

            [root@server test]# echo 3 > /proc/sys/vm/drop_caches
            [root@server test]# cat /proc/sys/vm/drop_caches
            3

            將/proc/sys/vm/drop_caches值設(shè)為3

            [root@server test]# free -m
                         total       used       free     shared    buffers     cached
            Mem:           249         66        182          0          0         11
            -/+ buffers/cache:         55        194
            Swap:          511          0        511

            再來運行free命令,發(fā)現(xiàn)現(xiàn)在的used為66MB,free為182MB,buffers為0MB,cached為11MB.那么有效的釋放了buffer和cache.

            有關(guān)/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(8) first.

            轉(zhuǎn)自
            http://blog.chinaunix.net/u2/76292/showart_1334673.html
            posted on 2009-12-07 23:57 chatler 閱讀(312) 評論(0)  編輯 收藏 引用 所屬分類: Linux_Coding
            <2009年12月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            常用鏈接

            留言簿(10)

            隨筆分類(307)

            隨筆檔案(297)

            algorithm

            Books_Free_Online

            C++

            database

            Linux

            Linux shell

            linux socket

            misce

            • cloudward
            • 感覺這個博客還是不錯,雖然做的東西和我不大相關(guān),覺得看看還是有好處的

            network

            OSS

            • Google Android
            • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
            • os161 file list

            overall

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            老色鬼久久亚洲AV综合| 久久精品国产99国产精品导航 | 99久久99久久精品国产| 精品久久人妻av中文字幕| 99久久精品国内| 久久强奷乱码老熟女网站| 99精品久久精品一区二区| 国产精品一区二区久久国产| 天天综合久久久网| 国产A三级久久精品| 国产成人精品久久| 麻豆成人久久精品二区三区免费| 精品国产青草久久久久福利| 人妻无码中文久久久久专区| 国内精品久久久久国产盗摄| 漂亮人妻被黑人久久精品| 久久久精品久久久久久| 精品国际久久久久999波多野| 国产成人香蕉久久久久| 少妇久久久久久久久久| 久久亚洲精品国产亚洲老地址 | 亚洲国产精品无码久久久蜜芽 | 久久成人永久免费播放| 精品久久久久久无码专区不卡| 久久久久99精品成人片三人毛片| 久久婷婷国产综合精品| 伊人久久综合精品无码AV专区| 无码国内精品久久人妻麻豆按摩| 久久精品国产亚洲AV无码麻豆| 久久AV无码精品人妻糸列| 久久男人中文字幕资源站| 国产精品久久久99| 91久久福利国产成人精品| 亚洲乱亚洲乱淫久久| 精品久久久久久综合日本| 久久精品国产亚洲精品2020 | 久久精品亚洲精品国产欧美| 久久美女人爽女人爽| 久久久精品午夜免费不卡| 成人国内精品久久久久影院| 久久久精品人妻一区二区三区蜜桃 |