Wednesday 5 June 2019

jvisualvm: GUI to view information about Java Applications


‘jvisualvm’ is a tool that comes as part of jdk, it is GUI tool used to view the information about Java Applications.

Let’s see it by an example.

App.java
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
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 {

      RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
       String jvmName = bean.getName();
       long pid = Long.valueOf(jvmName.split("@")[0]);
           System.out.println("PID  = " + pid);
           
      int counter = 0;
      
      List<BigObject> bigObjs = new ArrayList<> ();
      

      while (true) {
         BigObject obj = new BigObject();
         
         counter++;
         
         bigObjs.add(obj);
         
         //System.out.println(counter);

         if (counter % 100000 == 0) {
            bigObjs = new ArrayList<> ();
            counter = 0;
            TimeUnit.SECONDS.sleep(30);
            printGarbageCollectorBeansInfo();
         }
      }

   }
}

Run App.java.


Open another terminal or command prompt and execute ‘jvisualvm’ command.

As you see the left side window, under Local widget, it displays all the java applications that are running in current system.

Double click on the application ‘App’.


Now, you can monitor CPU performance, Heap memory, metasapce memory, thread and lot more.

Previous                                                 Next                                                 Home

No comments:

Post a Comment