So, I have this extremely memory-intensive Java application that I create that creates a tree of millions of nodes. Using convenient Runtime methods Runtime to get heap information, I built a nice little method that displays the current memory usage, as shown below:
public void displayMemoryUsage() {
long maxMem = Runtime.getRuntime().maxMemory();
long freeMem = Runtime.getRuntime().freeMemory();
long heapMem = Runtime.getRuntime().totalMemory();
long usedMem = heapMem - freeMem;
System.out.println("Memory used: " + (int) (usedMem * 100 / maxMem) + "%");
}
So, to check this, I had a tree expanding its root node (up to 2128 children), and then expanded each of these children (total about 4 million nodes in the tree). 11% is displayed in memory. Then I set the root of the tree as one of the root children, and all the references to the root of the other children are deleted. Theoretically, this should remove 2127/2128 of the original root children. I ran the Java method Runtime.getRuntime (). Gc () to force garbage collection and asked it to display memory usage again. This time I got 10%. Theoretically, should this new percentage not be more than 0.05% or the 1 / 2128th value before installing a new root?
Any thoughts why he is not collecting garbage properly?
source
share