Suppose I am testing a Java server application. I know how much time it takes to finish the test. Now I\'d like to know how much was spent on GC during that test. How can I
Similar to @Steve McLeod's answer that uses ManagementFactory, since Java 8 this can also be written in a single line using Java streams:
long collectionTime = ManagementFactory.getGarbageCollectorMXBeans().stream().mapToLong(mxBean -> mxBean.getCollectionTime()).sum();