我試圖限制由我的日誌庫產生的垃圾數量,所以我編寫了一個測試來向我顯示FileChannel.write創建了多少內存。下面的代碼在我的Mac上分配ZERO內存,但是在我的Linux機器上(Ubuntu 10.04.1 LTS)創建大量垃圾,觸發GC。 FileChannels應該是快速和輕量級的。有沒有一個JRE版本在Linux上更好?Linux上的FileChannel.write會產生大量的垃圾,但不會在Mac上
File file = new File("fileChannelTest.log");
FileOutputStream fos = new FileOutputStream(file);
FileChannel fileChannel = fos.getChannel();
ByteBuffer bb = ByteBuffer.wrap("This is a log line to test!\n".getBytes());
bb.mark();
long freeMemory = Runtime.getRuntime().freeMemory();
for (int i = 0; i < 1000000; i++) {
bb.reset();
fileChannel.write(bb);
}
System.out.println("Memory allocated: " + (freeMemory - Runtime.getRuntime().freeMemory()));
我JRE的細節如下:
java version "1.6.0_19"
Java(TM) SE Runtime Environment (build 1.6.0_19-b04)
Java HotSpot(TM) 64-Bit Server VM (build 16.2-b04, mixed mode)
更新爲:
java version "1.6.0_27"
Java(TM) SE Runtime Environment (build 1.6.0_27-b07)
Java HotSpot(TM) 64-Bit Server VM (build 20.2-b06, mixed mode)
它工作得很好。 : - |
那麼,現在我們知道FileChannelImpl的早期版本有一個內存分配問題。
當我在linux上使用java 1.6.0_26運行這個時,它說內存分配爲「0」。 –
同樣在這裏:但我在11.04。我沒有10.04測試,對不起。 – Dan
正如一個側面說明,我能夠編寫一個記錄庫,在登錄到磁盤時分配ZERO內存。更多詳細信息,請訪問:http://mentalog.soliveirajr.com – TraderJoeChicago