Thursday, December 26, 2013

JStack - Generating stack dump


This would be useful to analyze a root cause, when an application has suddenly stopped working or not responding . There could be a some kind of deadlock or waiting processes or something else. 
JStack would help in getting the stack dump to a currently running java process.

JStack comes along with JDK kit, need not to install separately.

Go to command prompt, type below command with java process id.

>jstack <pid>

To identify the java process id, you can 'jps' command.

>jps  => this will list out the all java process id's.
Output:
5730 main  => this is my java process id.
10800 jps
8732

To get more details about which process ID belongs to which process use below command
> jps -ml

To get the additional information about the locks.
>jps  -l  <pid>

Jstack can also be used to get the thread dump for remotely running processes.
>jps  <remote host name/ip address>

To write stack dump to a file:
>jstack -l 7840 >  D:\Test\kklog.txt

">" is important to write into a file.


Some useful resources on this.
http://www.ibm.com/developerworks/library/j-5things8/
http://docs.oracle.com/javase/7/docs/technotes/tools/share/jstack.html


Monday, December 9, 2013

UseCompressedOops flag with java

Recently one of colleague was unable to launch eclipse, it says ‘Java returned with exit code =1’ , we have tried lot of options by increasing heap memory and all, but finally what worked out for him was by setting a  UseCompressedOops flag in eclipse configuration file.

He was using 64 bit machine and 64 bit JVM.

Here is the story, what compressed flag does.

The -XX:+UseCompressedOops option can improve performance of the 64-bit JRE when the Java object heap is less than 32 gigabytes in size. In this case, HotSpot compresses object references to 32 bits, reducing the amount of data that it must process.

Compressed oops is supported and enabled by default in Java SE 6u23 and later. In Java SE 7, use of compressed oops is the default for 64-bit JVM processes when -Xmx isn't specified and for values of -Xmx less than 32 gigabytes. For JDK 6 before the 6u23 release, use the -XX:+UseCompressedOops flag with the java command to enable the feature.

In summary:
Java version <6u23 - use command to set it.
Java version >=6u23 - by default enabled.
JDK7 - if -xmx not specified or -xmx <32GB => configured bydefault

You might have a question how does 64 bit pointer fits into 32 bit pointer, how it will be compressed. Please go through below link to understand more about it.

This is how JRocket JVM is compressing it, this will give us some understanding.





Java code to Java Heap

This is an excellent article which describes how Java 32 bit and 64 bit processes works with various primitives and objects in Java.

http://www.ibm.com/developerworks/opensource/library/j-codetoheap/index.html

Comparing 2 files


If you wanted to know whether 2 files is having same content or not, you need not to compare two files line by line, instead you can compare checksums of each file.

For this, you need to use Apache common-codec_1.7.jar file.




Example:


package com.kk;

import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.codec.digest.DigestUtils;

public class CompareFiles {

public static boolean compare(String oldFile, String newFile) throws IOException {
FileInputStream fis1 = new FileInputStream(oldFile);
String oldmd5 = DigestUtils.md5Hex(fis1);// old file checksum
FileInputStream fis = new FileInputStream(newFile);
String latestmd5 = DigestUtils.md5Hex(fis); // new file checksum
return oldmd5.equals(latestmd5);
}
public static void main(String[] args) {
String oldFile = "D:\\Work\\test\\js\\kklibrary.js";
String newFile = "D:\\Work\\test\\kklibrary.js";
try {
boolean compare = compare(oldFile, newFile);
System.out.println("Both files are: "+ compare);
} catch (IOException e) {
e.printStackTrace();
}
}
}