Find which garbage collector you are using through cmd and java program




Contents of page >
  • 1) Find which garbage collector you are using through cmd
  • 2) Find which garbage collector you are using through java program





1) Find which garbage collector you are using through cmd

Use java -XX:+PrintCommandLineFlags -version to find out which garbage collector you are using.

E:\>java -XX:+PrintCommandLineFlags -version
-XX:InitialHeapSize=266885312 -XX:MaxHeapSize=4270164992 -XX:+PrintCommandLineFlags -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)

E:\>


Check out which garbage collector your system is using it may be Serial collector / Serial GC (Garbage collector) or




2) Find which garbage collector you are using through java program

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.List;
public class FindGarbageCollectorWhichYouAreUsing {
   public static void main(String[] args) {
          try {
                 List<GarbageCollectorMXBean> getGarbageCollectorMXBeansList =

                                       ManagementFactory.getGarbageCollectorMXBeans();
                 System.out.println("Display garbage collector name > ");

                 for (GarbageCollectorMXBean getGarbageCollectorMXBeans :
                                                                                                                                                    
                                                    getGarbageCollectorMXBeansList) {
                       System.out.println(getGarbageCollectorMXBeans.getName());
                 }
          } catch (Exception exp) {
                 throw new RuntimeException(exp);
          }
   }
}

Above java program will display the garbage collector you are using.


Summary -

So in this core java tutorial we learned how to find out which garbage collector you are using through cmd and java program.


Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

Most important and frequently used VM (JVM) PARAMETERS with examples in JVM Heap memory in java


What are -Xms and -Xmx JVM parameters in java, And differences between them with examples


How to use -verbose:gc VM argument

How to use Jstat for monitoring the garbage collection in java




eEdit
Must read for you :