Friday, March 8, 2019

Java verbose option with its usage


Hello  Friends, Welcome back to my blog. Today our topic for this blogs is to understand one of the parameter called "verbose" that we can pass to the java while running your application. As from the parameter name we can guess that it means to have information in details. But how and which cases we should use this, let's have a look.

We have 3 verbose parameters as below

  • verbose:class
  • verbose:gc
  • verbose:jni

Let's understand each of them in detail.

verbose:class
This parameter output the information about which classes are loaded and from which jar. You can use this parameter to identify when you have multiple jars on the classpath, and some of the jars are having to contradict package structure with the same class name. You won't recognize from which jar the class is loaded. you can enable verbose option with class level to get those details. This option can also be helpful in case of the custom class loader where your classes are loaded by the custom classloader based on certain condition so in this case, you won't be able to figure out which class is loaded so this parameter can help you in this case. Let's look at the sample code.

Program:
package org.javahotfix.blog.application;

import java.util.Date;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class MyJavaApplication {

 public static void main(String args[]) {
 
  MyJavaBlog blog = new MyJavaBlog();
  blog.setId("blog-1");
  blog.setName("java verbose options in detail");
  blog.setCreatedOn(new Date());
  blog.setLastModifiedOn(new Date());
  
  GsonBuilder gb = new GsonBuilder().setPrettyPrinting();
  Gson gson = gb.create();
  
  String jsonString = gson.toJson(blog);
  System.out.println(jsonString);
 }
}

Output :
I:\Workspace\test-gradle-application\bin>java -verbose:class -cp C:/Users/Anil/.gradle/caches/modules-2/files-2.1/com.google.code.gson/gson/2.8.5/f645ed69d59
5b24d4cf8b3fbb64cc505bede8829/gson-2.8.5.jar;. org.javahotfix.blog.application.MyJavaApplication

[Opened C:\Program Files\Java\jre8\lib\rt.jar]
[Loaded java.lang.Object from C:\Program Files\Java\jre8\lib\rt.jar]
[Loaded java.io.Serializable from C:\Program Files\Java\jre8\lib\rt.jar]
[Loaded java.lang.Comparable from C:\Program Files\Java\jre8\lib\rt.jar]
[Loaded java.lang.CharSequence from C:\Program Files\Java\jre8\lib\rt.jar]
[Loaded java.lang.String from C:\Program Files\Java\jre8\lib\rt.jar]
[Loaded java.lang.reflect.AnnotatedElement from C:\Program Files\Java\jre8\lib\rt.jar]
[Loaded java.lang.reflect.GenericDeclaration from C:\Program Files\Java\jre8\lib\rt.jar]
[Loaded java.sql.Date from C:\Program Files\Java\jre8\lib\rt.jar]
[Loaded com.google.gson.internal.bind.JsonAdapterAnnotationTypeAdapterFactory from file:/C:/Users/Anil/.gradle/caches/modules-2/files-2.1/com.google.code.gson/gson/2.8.5/f645ed69d595b24d4cf8b3fbb64cc505bede8829/gson-2.8.5.jar]
[Loaded com.google.gson.internal.bind.TreeTypeAdapter from file:/C:/Users/Anil/.gradle/caches/modules-2/files-2.1/com.google.code.gson/gson/2.8.5/f645ed69d595b24d4cf8b3fbb64cc505bede8829/gson-2.8.5.jar]
.
.
.
.
{
  "id": "blog-1",
  "name": "java verbose options in detail",
  "createdOn": "Mar 8, 2019 12:59:47 AM",
  "lastModifiedOn": "Mar 8, 2019 12:59:47 AM"
}
[Loaded java.lang.Shutdown from C:\Program Files\Java\jre8\lib\rt.jar]
[Loaded java.lang.Shutdown$Lock from C:\Program Files\Java\jre8\lib\rt.jar]



verbose:jni
Before starting with this parameter, let understand what is JNI. Java Native Interface is API in Java which allows it to communicate with the underlying operating system like interacting with hardware, executing operating system routine, executing programs on the operating system. verbose option with jni will output all the calls to the operating system. instead, we can say all the java native method execution. Let see below program which is running on windows and calling existing program installed on the operating system.

Program:
package org.javahotfix.blog.application;

import java.io.IOException;

public class JNIDemo {
 public static void main(String args[]) throws InterruptedException, IOException {
  System.out.println("============= My Application code started =========================");  
  Process p = Runtime.getRuntime().exec("gm -version");
  while(p.isAlive()) {
   Thread.currentThread().sleep(1000);
  }
  int exit = p.exitValue();
  System.out.println("Exit Value : " + exit);
  System.out.println("============= My Application code ended =========================");
 }
}

Output:
I:\gitech\Java\Workspace\test-gradle-application\bin>java -verbose:jni org.javahotfix.blog.application.JNIDemo
[Dynamic-linking native method java.lang.Object.registerNatives ... JNI]
[Registering JNI native method java.lang.Object.hashCode]
[Registering JNI native method java.lang.Object.wait]
[Registering JNI native method java.lang.Object.notify]
[Registering JNI native method java.lang.Object.notifyAll]
[Registering JNI native method java.lang.Object.clone]
.
.
.
============= My Application code started =========================
[Dynamic-linking native method java.lang.ProcessImpl.getStillActive ... JNI]
[Dynamic-linking native method java.lang.ProcessEnvironment.environmentBlock ... JNI]
[Dynamic-linking native method java.lang.ProcessImpl.create ... JNI]
[Dynamic-linking native method java.lang.ProcessImpl.isProcessAlive ... JNI]
[Dynamic-linking native method java.lang.ProcessImpl.getExitCodeProcess ... JNI]
Exit Value : 0
============= My Application code ended =========================



verbose:gc
This option will print the garbage collection invocation details to the stdout. It will be good if we get it logged in to the file. this file then can be analyzed by the memory analyzer tools which can help us to finetune the GC settings on the JVM. there are many tools in the market which analyze the GC logs and help to fine-tune the GC settings. I would recommend using the -XX:+PrintGCTimeStamps -XX:+PrintGCDetails with it so it will print the timestamp and summery. when we enable it looks like below.

Output
I:\Workspace\test-gradle-application\bin>java -verbose:gc -XX:+PrintGCTimeStamps -XX:+PrintGCDetails  -cp C:/Users/Anil/.gradle/caches/modules-2/files-2.1/co
m.google.code.gson/gson/2.8.5/f645ed69d595b24d4cf8b3fbb64cc505bede8829/gson-2.8.5.jar;. org.javahotfix.blog.application.MyJavaApplication
0.224: [GC (Allocation Failure) [PSYoungGen: 512K->496K(1024K)] 512K->504K(62976K), 0.0075313 secs] [Times: user=0.00 sys=0.00, real=0.02 secs]
0.314: [GC (Allocation Failure) [PSYoungGen: 993K->480K(1536K)] 1001K->597K(63488K), 0.0037784 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
0.428: [GC (Allocation Failure) [PSYoungGen: 1504K->496K(1536K)] 1621K->878K(63488K), 0.0043473 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
0.614: [GC (Allocation Failure) [PSYoungGen: 1520K->496K(2560K)] 1902K->1208K(64512K), 0.0053639 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
{
  "id": "blog-1",
  "name": "java verbose options in detail",
  "createdOn": "Mar 8, 2019 1:24:52 AM",
  "lastModifiedOn": "Mar 8, 2019 1:24:52 AM"
}
Heap
 PSYoungGen      total 2560K, used 550K [0x00000000ebd80000, 0x00000000ec080000, 0x0000000100000000)
  eden space 2048K, 2% used [0x00000000ebd80000,0x00000000ebd8da60,0x00000000ebf80000)
  from space 512K, 96% used [0x00000000ec000000,0x00000000ec07c010,0x00000000ec080000)
  to   space 512K, 0% used [0x00000000ebf80000,0x00000000ebf80000,0x00000000ec000000)
 ParOldGen       total 61952K, used 712K [0x00000000c3800000, 0x00000000c7480000, 0x00000000ebd80000)
  object space 61952K, 1% used [0x00000000c3800000,0x00000000c38b2078,0x00000000c7480000)
 Metaspace       used 4372K, capacity 5024K, committed 5248K, reserved 1056768K
  class space    used 506K, capacity 528K, committed 640K, reserved 1048576K

I:\gitech\Java\Workspace\test-gradle-application\bin>



Ok, that's all for this blogs friends if you have any comments suggestion please feel free to comment. i will definatly try to answer your query or consider you suggestion and try to improve my blogs.

Thanks
Happy coding and have a wonderful day

1 comment: