Umapping memory mapped buffers

I'm currently mapping a file contents to memory using ByteBuffer.map method and no issues there. The issue is that I can't unmap buffers, nor can I predict when the buffers are unmapped. My test machine is a Win XP SP2, AMD2100, 2Gig-Mem with 7200RPM disk.

When I close the underlying FileChannel or RandomAccess file, the memory is still not released from the mapped buffers. I verify this using an external process monitor. It is only released when I run the System.gc() manually.

So my question is, what is the best strategy for memory management in Java when memory mapping huge files. My test file is only 400Megs in size, but other files my be upto 500Gigs. I do mapping in various large buffer sizes of about 10Meg in size. I'm considering just running the GC manullay before every buffer mapping since they are in huge chunks, but I don't like this approach. My worry here is that I can not wait for the regular GC to kick in since I'm allocating huge amounts of memory outside the Java VM scope.

BTW: my code is achieving increadible performance of 1.6M variable length records/second read and parsed using this model.

The overall algorithm is simple: map large chuck of file into memory, create a wrapper object (a CapturePacket in my API), create a ByteBuffer view using slice() for the record header, parse the header within the view buffer, create a second view buffer using slice() for the records content (Packet data) and return the created CapturePacket object. And work on the next packet. My tests are able to do the 1.6M times a second, that pretty fast for Java.

[1620 byte] By [voytechsa] at [2007-10-3 3:13:57]
# 1
See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038. There are several workarounds there. Not sure if they are up to date ;-)
ejpa at 2007-7-14 21:05:01 > top of Java-index,Core,Core APIs...
# 2

Thanks EJP, ouch... this is an issue, isn't it.

In Java 5.08 running GC seems to work OK on my test machine. But when I release the code and users start running this on every platform imaginable, I can see this isn't going to work. Hmmm....

Reading the data into direct buffers with the extra copy thats entailed with it, has a huge 40% to 50% performance penalty, but I may be able to use the mapped buffer approach in certain read-only circumstances.

voytechsa at 2007-7-14 21:05:01 > top of Java-index,Core,Core APIs...