In Java, assume 3 java application running.
a) Firstly, My application running
b) Secondly, Not made by me java application running
c) Thirdly, Not made by me java other applications running
d) Fourthly, another application by me running this following method:
public static void printGCStats() {
long totalGarbageCollections = 0;
long garbageCollectionTime = 0;
for(GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
long count = gc.getCollectionCount();
if(count >= 0) {
totalGarbageCollections += count;
}
long time = gc.getCollectionTime();
if(time >= 0) {
garbageCollectionTime += time;
}
}
System.out.println("Total Garbage Collections: "
+ totalGarbageCollections);
System.out.println("Total Garbage Collection Time (ms): "
+ garbageCollectionTime);
}
Question is: d) will show only d garbage collection details or, it will show all including a + b + c +d ? And once i request System.gc or runtime.gc will it request to single or all of them?
Each Java process runs in a separate JVM. Each JVM has its own heap, separate from the heaps of other JVMs.
The statistics that you’re looking at are for that process only. Similarly,
System.gc()only works with the heap of the process that invoked it.