I was calling the function "unsafe.allocateMemory" for a bulk of Memory from off-heap. But the value returned is odd.
The run-time environment shows here:
Here is my code:
import java.lang.reflect.Field;
import sun.misc.Unsafe;
public class UnsafeTest{
public static void main(String[] args){
try{
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
Unsafe unsafe = (Unsafe) theUnsafe.get(null);
long addr = unsafe.allocateMemory(16);
System.out.println("the address is :"+addr);
unsafe.freeMemory(addr);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
Result:
I thought something's wrong, because the outcome value is so much bigger than the total memory size(about 6G). I wonder if the value was a memory address ? If so, how could it be allocated as there are not so much memory actually. If not, what the base address of the memory/heap in OS.