‘GarbageCollectorMXBean’
class provides information about the total number of collections that have
occurred, approximate accumulated collection elapsed time in milli seconds
etc.,
Example
private
static void printGarbageCollectorBeansInfo() {
System.out.println("---------------------------------------------");
List<GarbageCollectorMXBean> beans
= ManagementFactory.getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean bean :
beans) {
System.out.println("Name
: " + bean.getName());
System.out.println("Collection
Count : " + bean.getCollectionCount());
System.out.println("Collection
Time : " + bean.getCollectionTime());
String[] memoryPoolNames =
bean.getMemoryPoolNames();
for (String poolName :
memoryPoolNames) {
System.out.println(poolName);
}
}
System.out.println("---------------------------------------------");
}
App.java
import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; public class App { private static class BigObject { int i; int ii; int iii; int iiii; int iiiii; int iiiiii; int iiiiiii; int iiiiiiii; int iiiiiiiii; int iiiiiiiiii; public BigObject() { i = 1; ii = 11; iii = 111; iiii = 1111; iiiii = 11111; iiiiii = 111111; iiiiiii = 1111111; iiiiiiii = 11111111; iiiiiiiii = 111111111; iiiiiiiiii = 1111111111; } } private static void printGarbageCollectorBeansInfo() { System.out.println("---------------------------------------------"); List<GarbageCollectorMXBean> beans = ManagementFactory.getGarbageCollectorMXBeans(); for (GarbageCollectorMXBean bean : beans) { System.out.println("Name : " + bean.getName()); System.out.println("Collection Count : " + bean.getCollectionCount()); System.out.println("Collection Time : " + bean.getCollectionTime()); String[] memoryPoolNames = bean.getMemoryPoolNames(); for (String poolName : memoryPoolNames) { System.out.println(poolName); } } System.out.println("---------------------------------------------"); } public static void main(String args[]) throws InterruptedException { int counter = 0; List<BigObject> bigObjs = new ArrayList<> (); while (true) { BigObject obj = new BigObject(); counter++; bigObjs.add(obj); //System.out.println(counter); if (counter % 1000000 == 0) { bigObjs = new ArrayList<> (); counter = 0; TimeUnit.SECONDS.sleep(5); printGarbageCollectorBeansInfo(); } } } }
Run App.java
by executing below command.
java
-Xmx512M App
You can
see following kind of output.
--------------------------------------------- Name : PS Scavenge Collection Count : 1 Collection Time : 30 PS Eden Space PS Survivor Space Name : PS MarkSweep Collection Count : 0 Collection Time : 0 PS Eden Space PS Survivor Space PS Old Gen --------------------------------------------- --------------------------------------------- Name : PS Scavenge Collection Count : 2 Collection Time : 56 PS Eden Space PS Survivor Space Name : PS MarkSweep Collection Count : 0 Collection Time : 0 PS Eden Space PS Survivor Space PS Old Gen --------------------------------------------- --------------------------------------------- Name : PS Scavenge Collection Count : 2 Collection Time : 56 PS Eden Space PS Survivor Space Name : PS MarkSweep Collection Count : 0 Collection Time : 0 PS Eden Space PS Survivor Space PS Old Gen --------------------------------------------- --------------------------------------------- Name : PS Scavenge Collection Count : 3 Collection Time : 78 PS Eden Space PS Survivor Space Name : PS MarkSweep Collection Count : 0 Collection Time : 0 PS Eden Space PS Survivor Space PS Old Gen ---------------------------------------------
Try to run
the same application with different garbage collectors.
$java -Xmx512M -XX:+UseConcMarkSweepGC App --------------------------------------------- Name : ParNew Collection Count : 1 Collection Time : 149 Par Eden Space Par Survivor Space Name : ConcurrentMarkSweep Collection Count : 0 Collection Time : 0 Par Eden Space Par Survivor Space CMS Old Gen ---------------------------------------------
$java -Xmx512M -XX:+UseSerialGC App --------------------------------------------- Name : Copy Collection Count : 1 Collection Time : 69 Eden Space Survivor Space Name : MarkSweepCompact Collection Count : 0 Collection Time : 0 Eden Space Survivor Space Tenured Gen ---------------------------------------------
$java -Xmx512M -XX:+UseG1GC App --------------------------------------------- Name : G1 Young Generation Collection Count : 3 Collection Time : 60 G1 Eden Space G1 Survivor Space Name : G1 Old Generation Collection Count : 0 Collection Time : 0 G1 Eden Space G1 Survivor Space G1 Old Gen ---------------------------------------------
No comments:
Post a Comment