<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Java中幾種文件讀寫的性能對比 準備: ``` static byte[] data = "123456789\n".getBytes(); static String path = "/root/testfileio/out.txt"; ``` 查看系統臟頁及文件被緩存大小: ``` ll -h out.txt && /home/golang/go/bin/pcstat out.txt && cat /proc/vmstat | egrep "dirty|writeback" ``` &nbsp; ## 一、使用最傳統的stream ~~~ public static void testBasicFileIO() throws Exception { File file = new File(path); FileOutputStream out = new FileOutputStream(file); while (true) { out.write(data); } } ~~~ 其中data為10個字節大小的字節數組,在while循環中不斷的將其寫入文件中。查看輸出文件的增長大小,會發現增長的還是比較緩慢的。同時所有的內容也會被pageCache所緩存(緩沖的限制可以查看dirty_ratio參數。) :-: ![](https://img.kancloud.cn/48/83/4883d40dad7c1c96096f41e50636a300_834x125.png) 即是暫停了程序,會發現該文件仍然有緩存在pageCache中,如果這個時候突然斷電,可能就會導致數據丟失,因為有可能沒有被持久化到磁盤中。 &nbsp; ## 二、使用buffer + stream ~~~ public static void testBufferedFileIO() throws Exception { File file = new File(path); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); while (true) { out.write(data); } } ~~~ 源碼: ~~~ public synchronized void write(int b) throws IOException { if (count >= buf.length) { flushBuffer(); } // 先放到本地的buffer中 buf[count++] = (byte)b; } ~~~ 可以發現使用buffer之后其寫入的速度比沒有使用buffer就會快很多。其原因是Buffer流會在用戶空間中準備一個8KB大小的字節數組作為緩沖區,該數組滿了之后才會發生一次調用系統,一次性的寫入8KB的內容。這樣就大大的減少了系統調用的次數,因此要比沒有使用Buffer的快很多。 同時在查看文件的緩存大小的時候會發現系統有明顯的卡頓現象,這是因為當在臟頁的數據超過一定比例之后需要暫停進行臟頁的落盤。 :-: ![](https://img.kancloud.cn/1a/e0/1ae05c8d5fedd74f5e76a1dbebbdf208_1341x507.png) &nbsp; ## 三、使用mmap ~~~ public static void testRandomAccessFileWrite() throws Exception { RandomAccessFile raf = new RandomAccessFile(path, "rw"); FileChannel rafchannel = raf.getChannel(); //mmap 使得jvm堆和pageCache有一塊映射空間 MappedByteBuffer map = rafchannel.map(FileChannel.MapMode.READ_WRITE, 0, 4096 * 1024 * 250); // 1000M的pageCache大小 while (true) { map.put(data); //不是系統調用 但是數據會到達 內核的pagecache } } ~~~ 可以看到快1G的pageCache寫入幾乎是一瞬間的事情。使用mmap會直接在pageCache中開辟一塊空間,作為映射(思考一些虛擬內存的地址轉化),不會發生系統調用。 &nbsp; 使用拓撲圖總結: :-: ![](https://img.kancloud.cn/7d/73/7d73553a005a9bf38ef6687f5d81d611_1240x714.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看