Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java MappedByteBuffer.isLoaded()

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.

like image 876
peterh Avatar asked Feb 27 '13 12:02

peterh


1 Answers

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."

like image 198
seoservice.ch Avatar answered Nov 16 '22 01:11

seoservice.ch