Hi ,
I was trying to implement a sort of shared cache between two or more JVMs by memory mapping a particular file using MappedByteBuffer
. From the specifications I see that when we use MappedByteBuffer.load()
it should load the data into a direct buffer. I have a couple of questions on this.
My code snippet::
RandomAccessFile file = new RandomAccessFile("file.txt","rw");
FileChannel fc = file.getChannel();
MappedByteBuffer buf5 = fc.map(MapMode.READ_WRITE, 0, fc.size());
//ByteBuffer buf6 = ByteBuffer.allocateDirect(100000000);
buf5.load();
try
{
Class c = Class.forName("java.nio.Bits");
Field f = c.getDeclaredField("reservedMemory");
f.setAccessible(true);
long reservedMemory = f.getLong(null);
f = c.getDeclaredField("maxMemory");
f.setAccessible(true);
System.out.println(
"Direct Memory Usage: "+ reservedMemory +"/"+ f.getLong(null)+"\n");
}
catch (Throwable t)
{
}
The output of the above code is 0 byte for the Direct Memory Usage (File.txt is 1 GB). But if I uncomment the line ..
ByteBuffer buf6 = ByteBuffer.allocateDirect(100000000);
I get a Direct Memory Usage of 100MB . Not able to understand why is this so, as to why I am not getting any direct memory usage in the first place ( i.e. when the line is commented out )
Although the Direct Memory Usage is 0 B for the above code, I do see that the resident memory ( using unix top ) of the process increase by 1 GB. But if I do a "free -m" on the box, I do not see any increase in memory usage.
In both the cases, I am a bit confused as to where the memory is ending up.
Thanks!