Today, I like you to know about VisualGC tool which is helpful to find Java Memory Leaks. It is tool related to JVMSTAT technology which became part of Visual VM introduced in Java 8. Before we go into the actual topic, let’s start a meaningful discussion. We grownup comparing benefits between the two popular programming languages named C and Java. Java addresses major concerns of programmers, particularly when they do memory management. This is the reason why all we assume that there is no room to take care of certain things. The beauty of Java is to make us free on workload of taking care of allocating and freeing memory. Which is not the case with C. In Java, garbage collector takes care of the things whereas in C it’s up to the programmer. 

Coming to the point, it’s obvious there is a memory leak concern for you in the program if you see java.lang.OutOfMemoryError after your program executes for a while. To be a perfectionist, one needs to make sure their code is memory leak free. This requires investigation and correction of the code once they written. 

In some cases, you can get on with the code causing memory leak.  This is because of the fact that all allocated memory is returned to the operating system after Java application and its JVM is closed. Which is not the case with C++.

In order to identify a memory leak we tend to use Task Manager Utility. This might not help us. There are memory debugging utilities which helps us to identify a memory leak.

Let’s analyze the possible error messages first:-

java.lang.OutOfMemoryError: Java heap space

java.lang.OutOfMemoryError: PermGen space

java.lang.OutOfMemoryError: Requested array size exceeds VM limit

java.lang.OutOfMemoryError: <any> 

Most common message displayed for memory leak is java.lang.OutOfMemoryError error. But not all java.lang.OutOfMemoryError error messages are due to memory leaks.

For Example:

One day, I got into trouble with eclipse showing OutOfMemoryError error. By default, eclipse uses 256 MB PermGenSpace for the application. If you can open Configuration file with extension .INI of eclipse you will find that. Increasing that size resolved the problem for me.

From this we know that it’s not the application error. It’s a configuration problem for the application showing OutOfMemoryError error.

Understanding Eclipse IDE will help a developer to deal with all sort of problems. That’s a different ball game all together.

Remember understanding JVM working will surely help us. Our concentration here in this article is to know about debugging tool that helps us to identify memory leaks.

With the release of Java 8 it’s obvious that we need to learn the VisulaVM tool which is provided with the JDK. This also helps us to know the performance issues with the code and fix them.

Let’s look at technology called jvmstat first. Folks, understand that no use diving into the sea to find more without mastering this drop in the hand.

Visit the following link. Click on the download link and download zip folder.

Extract the zip folder named jvmstat

Place that folder as you saw in below picture (C Drive). Check the folders are as shown in the picture before moving forward.

Before proceeding set the environment variables as shown below. 

Make sure you add this “C:\Program Files(x86)\Java\jdk1.7.0_40\bin;.”(Without double quotes) to the existing PATH variable

Then add the following environment variables,

Variable name: Java_Home

Variable value: C:\Program Files(x86)\Java\jre7

Variable name: JVMSTAT_JAVA_HOME

Variable value: C:\Program Files(x86)\Java\jdk1.7.0_40

If you are using 64-bit operating system. Notice that Program Files(x86) is equivalent to Progra~2. Don’t confuse between the two. You can use any of those whenever it’s required.

Step 1:
Type the following command in the command prompt

SET PATH= C:\jvmstat\bat;C:\jvmstat\bin;C:\jvmstat;%PATH%

%PATH% - This make sure the existing path setting for the variable name Path doesn’t get disturbed.

Step 2:
Paste the following line in the notepad and save it as jstatd.policy in root of C Drive. Please make sure you put “jstatd.policy” (in double quotes) while saving. 

grant codebase "file:${java.home}/../lib/tools.jar" {     permission; };

Note: If it’s not permitted to save in root of C drive. Save it somewhere else and then copy and paste to the root of the C drive. This worked for me.

Step 3:
Type the following command as shown below

jstat -gcutil 0 1000 3

Before running the following command you need to download java2demo.jar by googling.

I placed it in the path e:\demo\jfc\Java2D\ as shown in the above image. 

Type the following command as shown below

 java –jar e:\demo\jfc\Java2D\java2demo.jar

You will see an application running playing music.

Step 4:
Now, take another instance of command prompt and type the following commands.


SET PATH=C:\jvmstat\bat;%PATH% (We are setting this path to make sure visualgc command works for us. In the sense, we are telling where locate visualgc bat file for the system)

jstat –gcutil 6436 1000 3 (You can notice in the below figure 6436 is the lvmid given as input)

visualgc 6436 (See figure to know about input 6436 given)

Here the output as shown in the following figure

VisualGC 3.0

That’s it. I am done. This article helps you to understand jvmstat technology/VisualGC utility. Which is the building block to understand VisualVM. Extend your knowledge on this. Stay tuned for more.


Post a Comment