I just want to know these two libraries use system functionality?
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
Unsafe un = (Unsafe) f.get(null);
int ps = un.pageSize();
System.out.println("Page size: " + ps);
This code is not safe and not cross-platform-compatible.
Unsafe
itself is public supported Java API from the package java.lang.reflect
. This code will run on all platforms, with all implementations of JVMs, because it is part of Java's public API.un
you are getting using the public supported API itself is not present on any JVMs other than the Oracle/Sun JVM, because it's private API to that JVM implementation. For example, your code will not run on the IBM JVM, since sun.misc.Unsafe
will not be present there.I think the Java 8 javac
compiler by Oracle/Sun will even warn you when you compile your code (this compiler is not used if you build your project with Eclipse's built-in features), because you are using a private API.
Yes, your solution will return you the native memory page size, but it will possibly not run on all platforms.