My goal is to ensure that an array allocated in java is allocated across contiguous physical memory. The issue that I’ve run into is that the pages an array is allocated across tend not to be contiguous in physical memory, unless I allocate a really large array.
My questions are:
- Why does a really large array ensure
pages which are contiguous in physical memory? - Is there any way to ensure an array is allocated across physical memory, that doesn’t involve making the array really large?
- How can I tell what page or physical address a Java object/array exists in, without measuring cache hits/cache misses?
I’m not looking for answers asking why I am doing this in java. I understand that C would “solve my problem”, and that I’m going against the fundamental nature of java. Nevertheless I have a good reason for doing this.
The answers need not be guaranteed to work all the time. I am looking for answers that work most of the time. Extra points for creative, out-of-the-box answers that no reasonable Java programmer would ever write. It’s OK to be platform specific(x86 32-bit 64-bit).
Given that the garbage collector moves objects around in (logical) memory, I think you are going to be out of luck.
About the best you could do is use ByteBuffer.allocateDirect. That will (typically) not get moved around (logical) memory by the GC, but it may be moved in physical memory or even paged out to disc. If you want any better guarantees, you’ll have to hit the OS.
Having said that, if you can set the page size to be as big as your heap, then all arrays will necessarily be physically contiguous (or swapped out).