Sunday, December 23, 2007

jhat - Java Heap Analysizer

Since I get involved in some jhat related project with one of my senior team members. I decided to make some blog entries. jhat is java heap analysis tool which is shipped in jdk6. There is an independent project name hat on hat.dev.java.net. jhat is flavor of this project only.

If you are a guy, who write lot of Java code and keep on looking for optimization, if you are getting errors like memory overflow.. then yes its a tools for you only.

jhat is all about giving readable information from java heap dump. Java Heap Dump contains information like how many classes, how many instances, execution flow and many more.
But all these information are in a binary format. And the duty of jhat is to read that file and provide useful information to you.

There are lot of ways to generate heap dump:

1. From normal java execution program, you can see the heap by Ctrl + Break.
2. jdk6,5 is coming with some tools like jmap, jconsole which help us to generate heap dump. Usages of these tools are very easy.
3. jdk5 also comes up with a tool called HPROF which can be used to generate heap dump.

So, here are the steps, how to approach the problem:

Make heap dump with jmap:

1. Run the java program, say SwingSet2 in the example.
2. Open other terminal, and run jmap
jmap -dump:format=b,file=heap.bin (On Windows, you can get pid by Ctrl-Alt-Del -> Process -> PID )

jmap will generate a file with name heap.bin which contains heap dump. Now pass this heap.bin to jhat.

3. jhat heap.bin

There are lot many optional argument, check the detail here.

Open browser, type: http://localhost:7000 (7000 is the default port), you can see hell lot of information. Grab the information of your interest. Or if you want to parse the information from yourself use OQL(Object Query Language) and write query.

Sunday, December 09, 2007

How Java handles Method ?

In my previous blog "Performance - Final Keyword ? " One of the blog readers asked question about How Java handles overriding ! So, I decided to make one entry(I guess more than one) for the answer.

Yes overriding tactics in Java is very different from C++ as methods by default in Java can be overridden unlike C++. In C++, the concept of overriding functions are handled by Virtual Table, VTable(This wiki link contains lot of information). Whereas in Java there is some other concept. Before going into the depth, let's see some of the basic things which one should need to know before making hand dirty in overriding concept.

Here is a Simple HelloWorld Program:

class Hello {
public static void main(String[] args)
{
System.out.println("Hello Bloggers!");
}
}

Lets see what the bytecode is generating, javap -c Hello(more about javap)

Compiled from "Hello.java"
class Hello extends java.lang.Object{
Hello();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."":()V
4: return

public static void main(java.lang.String[]);
Code:
0: new #2; //class Hello
3: dup
4: invokespecial #3; //Method "":()V
7: astore_1
8: getstatic #4; //Field java/lang/System.out:Ljava/io/PrintStream;
11: ldc #5; //String Hello it is
13: invokevirtual #6; //Method java/io/PrintStream.println:(Ljava/lang/Str
ing;)V
16: return

}

Have a look on these lines:

1: invokespecial #1; //Method java/lang/Object."":()V
4: invokespecial #3; //Method "":()V
13: invokevirtual #6; //Method java/io/PrintStream.println:(Ljava/lang/Str
ing;)V

These are the lines related to method invocation. So what the heck is this invokespecial and invokevirtual ? Actually JVM used 4 different kinds of instructions for method invocation those are :

- invokevirtual - This is for instance method like System.out.println("Hello Bloggers!") here.
- invokestatic - This is for class methods.
- invokespecial - This is for special things. It is used when
- call , instance initialization.
- super call, when you will call something from super.method
- private methods. As private methods can't be overridden so we need to put this in a special category.
- invokeinterface - invoking instance method with interface reference(Soon we will see the example)

Now we are very clear that why invokespecial has been used at #1 and #3 whereas invokevirtual at #6. Ok, lets write some code which can see the usages of all four.


interface interfaceForHello {
public void noUse();
}

class Hello implements interfaceForHello {
public void noUse() {
System.out.println("No use");
}
public static void staticMethod()
{
System.out.println("Static method");
}
public static void main(String[] args)
{
interfaceForHello iface = new Hello();
iface.noUse();
Hello.staticMethod();
System.out.println("Hello Bloggers ! ");
}
}

And here goes the javap -a Hello:

Compiled from "Hello.java"
class Hello extends java.lang.Object implements interfaceForHello{
Hello();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."":()V
4: return

public void noUse();
Code:
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3; //String No use
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return

public static void staticMethod();
Code:
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #5; //String Static method
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return

public static void main(java.lang.String[]);
Code:
0: new #6; //class Hello
3: dup
4: invokespecial #7; //Method "":()V
7: astore_1
8: aload_1
9: invokeinterface #8, 1; //InterfaceMethod interfaceForHello.noUse:()V
14: invokestatic #9; //Method staticMethod:()V
17: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
20: ldc #10; //String Hello Bloggers !
22: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
25: return

}

In next blog we will continue the same.

Saturday, December 01, 2007

OpenJDK Build on Netbeans with Windows / Solaris - Part II

Talking of some of the details about OpenJDK building(on Netbeans), we can start with Windows build(x86). In general,building on windows is tougher than Solaris/Linux because there are lot of dependencies on other tools and we can't set up the tools on network. Now lets see the list of the downloads required (don't abuse me, I know its too much :-))


- OpenJDK - Download open source JDK

- Binary Plugin - Download Binary Plugin

We need to install binary of Java SE 6, which we can get from Sun site and Java SE 7 which we can download from http://download.java.net/jdk7/binaries

Now this is all about JDK downloads. Time to put hand into the dirty world of other dependent tools:

1. As we have lot of C++ code(native code) inside JDK, we need a C++ compiler. For windows, JDK document suggests to use .NET2003. Though we have made the build on .NET 2003 but I guess compiler of .NET 2003 is free on net and you can give one try with free compiler. Note here: Don't try with .NET 2005 as many of the API get deprecated.

2. Cygwin Installation: This is a area of concern. We have invested(wasted :D) 90 percent of the build time in this tool. Installing, uninstalling, reinstalling .. and it keep on going. On the first installation page of Cygwin itself , you will get two options: Unix/Binary or Dos/Text. Any option can work, can't work depending on your luck :-). Most of the engineer suggested me to install Unix/Binary but Dos/Text worked for us :D. Download from: Cygwin Site

NOTE: Don't forget to download Developer, System, Utility, Archive package of its full mode not the default mode.

3. Install Microsoft DirectX 9.0 SDK(Summer Package) - Download it from Microsoft site

4. Download and Install FindBugs and Ant

5. Download Microsoft Unicode Library

6. Download and install latest version of freetype from www.freetype.org

7. Oops, if you don't have Netbeans 6 then download that also :)

Time to take a cup of coffee. Let the download to go on.

OK, back from coffee ...

Add Ant, Findbugs, Cygwin(till \bin) in the path variable of system. Open cmd, run vsvars32.bat (Micros~1.NET\Common7\Tools\vsvars32.bat) to initialize the .NET variables. Run Netbeans.exe from the same command line.

Open openjdk/jdk/make/netbeans/ in netbeans and open project jdk. Open some of the important file like build.propertied and make.xml. In make.xml file, you will get the path of make, change it to the cygwin make file {cygwin/bin }.

Now IMPORTANT, get ready to write build.properties file:

bootstrap.jdk={PATH_JDK7_INSTALL}
make.options=\
// this is important because code contains many
//line which work on if(OPENJDK==true) code
OPENJDK=true
// write the path in ~ format (dir /x)
ALT_COMPILER_PATH={MS.NET2003_PATH}/Vc7/bin \
ALT_MSDEVTOOLS_PATH= {MS.NET2003_PATH}/Common7/Tools/Bin \
ALT_MSVCR71_DLL_PATH= {MS.NET2003_PATH}/SDK/v1.1/Bin \
ALT_BOOTDIR={PATH_JDK6_INSTALL} \
ALT_BINARY_PLUGS_PATH={PATH_BINARY_PLUGS}
ALT_JDK_IMPORT_PATH={PATH_JDK7_INSTALL} \
ALT_DXSDK_PATH={DIRECTX_PATH} \
ALT_FREETYPE_LIB_PATH={FREE_TYPE_PATH}/lib \
ALT_FREETYPE_HEADERS_PATH={FREE_TYPE_PATH}/include \
ALT_UNICOWS_LIB_PATH={UNICOWS_PATH} \
ALT_UNICOWS_DLL_PATH={UNICOWS_PATH} \
LD_LIBRARY_PATH= \
CLASSPATH= \
JAVA_HOME= \
SHLVL=1 \


All Done ! Fire the build now, if you are lucky enough you will get it done in some 20-30 minutes. In the next blog, we are going to talk about the problem(s) with some of this software and version dependencies.

OpenJDK Build on Netbeans with Windows / Solaris - Part I

Our team is ready with OpenJDK build for FOSS.IN, better to say partially ready :). Me and Vikram is going to give the live demo about building OpenJDK and how you can optimize the source code according to your own need. For Example, if you don't like the default LookAndFeel of Java, change it according to your like because now its your world, its your Java. Let me show you one simple code change we have done with the gradient color. This is the normal Ocean LookAndFeel of Java.

Before the code Change:

After the code change:



Though building OpenJDK is little cumbersome process but come on, its a one time process. Just make the build ready once and then screw it as you want both on the Java level and on native level . Even more process and standard documents will come as OpenJDK goes into its maturity stage. You will love to know that Netbeans 5.5+ is giving a high class support for making OpenJDK across all platforms.

So for the real die-hard of Java, I am going to write Part II, III, ... upto N(covering some technicalities of building process) till I am not able to make you happy. Thanks to Igor and Sandeep who have replied my all shots of stupid questions.