Reputation: 693
I am trying to store the memory each unit test uses into in a database. So i calculate the free memory before and after the tests with:
Runtime.getRuntime().freeMemory()
If there are garbage collections during the test i count them via:
//returns garbage collection count
private long calculateGarbageCollectionCount(){
List<GarbageCollectorMXBean> garbageCollectorMXBean = ManagementFactory.getGarbageCollectorMXBeans();
return garbageCollectorMXBean.get(garbageCollectorMXBean.size()-1).getCollectionCount();
}
Now i need an idea how much memory on average the garbage collections sets free to do something like this:
gcCount*memoryReleased+freeMemoryAtStart-freeMemoryAtEnd
Is there at least a rough estimation how much memory gets release during a garbage collection? I tried using MemoryPoolMXBean with the getPeak method but the value i get is confusing me. Its bigger than the total Memory the JVM has.
Upvotes: 0
Views: 2498
Reputation: 693
Found a useful class that helped: https://github.com/Netflix/spectator/blob/master/spectator-ext-gc/src/main/java/com/netflix/spectator/gc/GcLogger.java
With this class it is possible to catch garbage collection events and more so gather information what happened in them.
As a rough estimation how much memory a unit tests uses i trigger a garbage collection before the start. Calculate the free memory before and after the test and sum up how much memory got released during garbage collections.
Upvotes: 1
Reputation: 140417
Don't try to create your own "profiling application". You should rather choose between:
In other words: don't invent your own thing here. Rely on existing, working, robust technology instead.
Upvotes: 2