I'm trying to use Java MappedByteBuffers in READ_WRITE mode to map a large file (tens of GB) or a set of numerous smallish files (~128MB). This is to implement high performance B-trees.
My problem is that on my Windows 7 laptop with 8GB of RAM and JDK 7, everything goes fine until the physical memory is full and the OS starts actually writing the data to the files. At this point, Windows slows down to a crawl. The I/Os seem to completely starve any other activity. The mouse pointer can barely move and I generally end up having to force reboot the machine.
The following code demonstrates the issue:
public static void testMap() throws Exception
{
MappedByteBuffer[] mbbs = new MappedByteBuffer[512];
for (int i = 0; i < 512; i++)
{
System.out.printf("i=%d%n", i);
RandomAccessFile raf = new RandomAccessFile(String.format("D:/testMap.%d.bin", i), "rw");
mbbs[i] = raf.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 128*1024*1024);
for (int j = 0; j < 128*1024; j++) {
mbbs[i].put(j*1024, (byte)(i*j));
}
}
}
I don't mind if the I/Os take some time. After a while, the OS must actually start to write the bytes into the files anyway. But, here, the process essentially starves the whole OS. How can I avoid this?