Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Compressed Oops Example
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 bytes): " + Runtime.getRuntime().totalMemory());
System.out.println("Free Memory (in bytes): " + Runtime.getRuntime().freeMemory());
System.out.println("Max Memory (in bytes): " + Runtime.getRuntime().maxMemory());
while (true) {
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());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment