Created
January 30, 2014 13:34
-
-
Save CodingFabian/8708393 to your computer and use it in GitHub Desktop.
Compressed Oops Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | |
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment