Home  >  Core Java

Example of GarbageCollectorMXBean in Java

By Arvind Rai, March 25, 2013
GarbageCollectorMXBean is the interface that belongs to the package java.lang.management. GarbageCollector needs to know unreachable object to free the space. GarbageCollectorMXBean provides the information for the same. JVM can have more than one instance of GarbageCollectorMXBean. Instance can be craeted as
List<GarbageCollectorMXBean> gcmxb = ManagementFactory.getGarbageCollectorMXBeans();
Find the sample usage.
GarbageCollectorMXBeanTest.java
package com.test;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.List;
public class GarbageCollectorMXBeanTest {
  class UserThread extends Thread {
      public void run() {        
             List<GarbageCollectorMXBean> gcmxb = ManagementFactory.getGarbageCollectorMXBeans();
             for(GarbageCollectorMXBean ob: gcmxb){
                 System.out.println("---------");
                 System.out.println("name of memory manager:"+ob.getName());
                 System.out.println("CollectionTime:"+ob.getCollectionTime());
                 String[] str=ob.getMemoryPoolNames();
                 for(int i=0;i<str.length;i++){
                     System.out.println(str[0].intern());
                 }
             }
      }
  }
  public static void main(String[] a){
      Thread th= new GarbageCollectorMXBeanTest().new UserThread();
      Runtime.getRuntime().addShutdownHook(th);
  }
}
Output
---------
name of memory manager:PS Scavenge
CollectionTime:0
PS Eden Space
PS Eden Space
---------
name of memory manager:PS MarkSweep
CollectionTime:0
PS Eden Space
PS Eden Space
PS Eden Space
PS Eden Space 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us