Skip to content

Instantly share code, notes, and snippets.

@okhiwell
Forked from bindiego/Memory.java
Created February 7, 2020 12:04
Show Gist options
  • Save okhiwell/6a64d42ed77e1e10c34dd242101e4976 to your computer and use it in GitHub Desktop.
Save okhiwell/6a64d42ed77e1e10c34dd242101e4976 to your computer and use it in GitHub Desktop.
Check JVM compressed oops option

try to stay below the threshold for zero-based compressed oops; the exact cutoff varies but 26 GB is safe on most systems, but can be as large as 30 GB on some systems. You can verify that you are under the limit by starting Elasticsearch with the JVM options -XX:+UnlockDiagnosticVMOptions -XX:+PrintCompressedOopsMode and looking for a line like the following

heap address: 0x000000011be00000, size: 27648 MB, zero based Compressed Oops

showing that zero-based compressed oops are enabled instead of

heap address: 0x0000000118400000, size: 28672 MB, Compressed Oops with base: 0x00000001183ff000
#!/bin/bash -ex
java -Xmx32766m -XX:+PrintFlagsFinal 2> /dev/null | grep UseCompressedOops
java -Xmx32767m -XX:+PrintFlagsFinal 2> /dev/null | grep UseCompressedOops
public class Memory {
// Dummy Entity representing usual data objects
private static class Entity {
public String name;
public String detail;
public Double amount;
public Integer age;
}
// Linked list offers table inserts and helps illustrating the issue by using multiple
// references per entry
public static java.util.LinkedList<Entity> entities = new java.util.LinkedList<>();
// This threshold should be 2 times Xmn. It ensures the loop stops before a full GC happens.
private static final int MB = 1024 * 1024;
private static final int THRESHOLD = 100 * MB;
public static void main(String... args) {
System.out.println("Total Memory (in GB): " + Runtime.getRuntime().totalMemory() / 1024 / 1024 / 1024);
System.out.println("Free Memory (in GB): " + Runtime.getRuntime().freeMemory() / 1024 / 1024 / 1024);
System.out.println("Max Memory (in GB): " + Runtime.getRuntime().maxMemory() / 1024 / 1024 / 1024);
while (true) {
appendEntitiesToDataStructure();
terminateBeforeFullGCorOOMEcanHappen();
}
}
private static void appendEntitiesToDataStructure() {
entities.add(new Entity());
}
private static void terminateBeforeFullGCorOOMEcanHappen() {
if (Runtime.getRuntime().freeMemory() < THRESHOLD) {
System.out.println("Elements created and added to LinkedList: " + entities.size());
System.exit(0);
}
}
}
#!/bin/bash -ex
m=31 # change to 32 then compare the results
rm -rf *.class
javac Memory.java
java -Xms${m}g -Xmx${m}g -Xmn50m Memory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment