ログ ライブラリによって生成されるガベージの量を制限しようとしているので、FileChannel.write が作成しているメモリの量を示すテストをコーディングしました。以下のコードは、私の Mac に ZERO メモリを割り当てますが、Linux ボックス (Ubuntu 10.04.1 LTS) に大量のゴミを作成し、GC をトリガーします。FileChannel は高速で軽量であると想定されています。これが Linux で改善された JRE バージョンはありますか?
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 にはメモリ割り当ての問題があることがわかりました。