This API is usable since JDK8 and there is some new additions in JDK9.
But how to use it ?
This API is not part of the standard API but in the test library from OpenJDK. you can find it here.
Download the source of OpenJDK then either you build it entirely and grab the wb.jar or
- go to test/testlibrary/whitebox directory
- javac -sourcepath . -d . sun\hotspot\**.java
- jar cf wb.jar .
Place you wb.jar next to your application and launch it with:
java -Xbootclasspath/a:wb.jar -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI ...
Here is an examble you can run with WhiteBox jar:
For this example you need to add -XX:MaxTenuringThreshold=1 to make it work as expected.
Now you have an API to trigger minor GC and test if an object resides in Old generation, pretty awesome !
You can also trigger JIT compilation on demand for some methods and change VM flags on the fly:
Unlike unsafe, this API seems difficult to use in production environment, but at least you can have fun in labs or adding this like the OpenJDK for your low level tests. Enjoy!