Skip to content

Instantly share code, notes, and snippets.

@amuraru
Last active November 13, 2021 17:45
Show Gist options
  • Save amuraru/2612430d9287c1cd98f8d49c379629f8 to your computer and use it in GitHub Desktop.
Save amuraru/2612430d9287c1cd98f8d49c379629f8 to your computer and use it in GitHub Desktop.
Get

Compile using Java 17

javac --add-exports java.base/jdk.internal.misc=ALL-UNNAMED Main.java

Run using java 17

java --add-opens java.base/jdk.internal.misc=ALL-UNNAMED -cp . Main

maxDirectMemory defaults to default max memory

Set Xmx but not MaxDirectMemorySize

maxDirectMemory defaults to Xmx in this case

java --add-opens java.base/jdk.internal.misc=ALL-UNNAMED -cp . -Xmx200m Main
sun.misc.VM.maxDirectMemory(): 209715200

Set Xmx and MaxDirectMemorySize

java --add-opens java.base/jdk.internal.misc=ALL-UNNAMED -cp . -Xmx200m -XX:MaxDirectMemorySize=60m Main
sun.misc.VM.maxDirectMemory(): 62914560

MaxDirectMemorySize takes precedence in this case

import jdk.internal.misc.SharedSecrets;
import jdk.internal.misc.VM;
public class Main
{
public static void main(String args[])
{
System.out.println("sun.misc.VM.maxDirectMemory(): " + VM.maxDirectMemory());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment