It seems to me that MappedByteBuffer.isLoaded() consistently returns false
on Windows. When I test on say BSD Unix I get true
using the same test data.
Should I worry?
I basically cannot get isLoaded()
to return true on Windows no matter what size data I use.
Here's my test code for reference:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MemMapTest {
private static final String FILENAME = "mydata.dat";
private static final int MB_1 = 1048576; // one Mbyte
private static final byte[] testData = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // len = 10 bytes
public static void main(String[] args) throws FileNotFoundException, IOException {
// Create a 10 Mb dummy file and fill it with dummy data
RandomAccessFile testFile = new RandomAccessFile(FILENAME, "rw");
for (int i = 0; i < MB_1; i++) {
testFile.write(testData);
}
MappedByteBuffer mapbuf = testFile.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, MB_1 * 20).load();
testFile.close();
if (mapbuf.isLoaded()) {
System.out.println("File is loaded in memory");
} else {
System.out.println("File is NOT loaded in memory");
}
}
}
I understand that load()
is only a hint but with such small sizes as I use here I would expect isLoaded()
to return true at some point. As said this seems to be related to Windows OS. It basically means that isLoaded()
is of absolutely no use.
Perhaps I should add that my MappedByteBuffer
on Windows works absolutely fine and with stellar performance even if isLoaded()
always returns false. So I don't really believe it when it says it is not loaded.
Tested on Windows 7, using Oracle Java 7 update 9.
No you shouldn't be worried.
According to http://docs.oracle.com/javase/7/docs/api/java/nio/MappedByteBuffer.html the behaviour rely upon the implementation of the "underlying operating system".
"A return value of false does not necessarily imply that the buffer's content is not resident in physical memory."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With