Wednesday 5 June 2019

Java: VisualGC: Visual Garbage Collection Monitoring Tool


VisualGC stands for Visual Garbage Collection Monitoring Tool. VisualGC attaches to an instrumented HotSpot JVM and collects and graphically displays garbage collection, class loader, and HotSpot compiler performance data.

Installing VisualGC plugin to jvisualvm
Step 1: Execute the command jvisualvm.


Tools -> Plugins

Go to ‘Available Plugins’ tab.
Select ‘Visual GC’ and click on ‘Install’ button.


Click on Next button.

Accept the licence agreement and click on Install button.

Once installation is successful, you can see below window.

Click on Finish button.

Step 2: Close VisualVM application.

Step 3: Run App.java application.
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(2);
				printGarbageCollectorBeansInfo();
			}
		}

	}
}

Step 4: Open terminal or command prompt and execute the command ‘jvisualvm’.


Double click on the Application ‘App’.

Now you can see ‘Visual GC’ tab in Java VisualVM window.
Previous                                                 Next                                                 Home

No comments:

Post a Comment