Pages Navigation Menu

Coding is much easier than you think

Java Heap Dump Analysis using Eclipse Memory Analyzer (MAT)

Posted by in Core Java, Eclipse, Java, JVM, Software

 
In this article we will learn what a Java heap dump is and how to analyze a heap dumps generated through OutOfMemoryError using Memory Analyzer in Eclipse.
 

What is a heap dump?

 
A heap dump is a snapshot of memory at a given point in time. It contains information on the Java objects and classes in memory at the time the snapshot was taken.
 

Why would we want to read heap dump?

 
If your Java application crashes with an OutOfMemoryError it is possible to automatically get a heap dump to analyze. This view into the memory profile of the application at the time it crashed can help us to figure out what caused the error. This can help decide what to optimize in our code.
 

How to get a heap dump?

 
To generate heap dump we have to execute a JVM with the following parameters in eclipse
 

-XX:+HeapDumpOnOutOfMemoryError writes heap dump on first  OutOfMemoryError

 

Here the heap dump will be generated in the “current directory” of the JVM by default. It can be explicitly redirected with
 

-XX:HeapDumpPath= for example -XX: HeapDumpPath=/disk/myFolder.

 
GC_Heap Dump analysis_Run_Configuration
 
GC_HeapDump anaylysis_Run_Configuration_JVM
 

How to read a heap dump?

 
Heap dump will be in binary format so you don’t read the plain file. Instead use a tool like Memory Analyzer Tool.
Download MAT plugin from this location, and install it in your eclipse.
 

OOM Java program

 

Here the Java program below is used to trigger an OutOfMemoryError. This program is basically creating multiple String instances within a List data structure until the Java Heap depletion.
 

package com.simplecode.heap;

import java.util.ArrayList;
import java.util.List;

public class OOMHeapGenerator
{
public static void main(String[] args)
{
	System.out.println("JVM OutOfMemoryError Simulator");
	List<String> leakingVariable = new ArrayList<String>();
	
	try
	{
		while (1 < 2)
		{
		leakingVariable.add("OutOfMemoryError");
		}
	}
	catch (Throwable exp)
	{
	     if (exp instanceof java.lang.OutOfMemoryError)
	     {
	     System.out.println("OutOfMemoryError triggered! " + "[" + exp + "]");
	     }
	     else
	     {
	     System.out.println("Other Exception! " + "[" + exp + "]");
	     }
	}
	System.out.println("Simulator done!");
}
}

 

On running this program, when the JVM ran out of memory it created a heap dump file java_ pid1244.hprof.
 
GC_Heap Dump Analysis_Run_Java_Program_Exec
 
Press F5 in your project folder, so now the generated heap dump file will appear in eclipse.

 
Eclipse Heap Dump Memory Analyser
 

Load Heap Dump

 
Just double click on the heap dump file and select the Leak Suspects Report, and then this file will be loaded into MAT.
 
GC_Heap_Dump_analysis_Leak Suspect Report Using MAT
 

On clicking finish the following Screen is obtained
 
GC_Heap_Dump_analysis_Chart
 

Analyze Heap Dump

 
Clicking on the “See stacktrace” link will give you the exact line of code that was building up the huge List causing the application to crash.

 
GC_Heap_Dump_analysis _Stacktrace
 

Now we know where to go look in at the code to fix this bug. Hence by analysing the Heap Dump using the MAT we could easily identify our primary leaking Java class and data structure.

 

Read More

Introduction to Struts 2 Framework

Posted by in Struts-2

 
In our last article we have learned about the difference between Model 1 and Model 2 (MVC) Architecture. In this article we will learn about Struts 2 framework and about the Architecture which it follows.
 
In general if you are about to build a standard application using JSP and Servlets, then you will end up in a following MVC pattern.
 
a
 
** UPDATE: Struts 2 Complete tutorial now available here.
 

Why web Framework ?

 
Web framework is a basic ready made underlying of MVC structure, where you have to just add components related to your business. It forces the team to implement their code in a standard way. (helps debugging, fewer bugs etc).

 

Step involved in building a framework.

 
Step 1: Handle the request parameters

Step 2: Create a interface class(Model) for business service class

Step 3: View part(Jsp) to render the result

Step 4: Router to route the request to the correct MODEL and correct View layer
 
The above steps are depicted photographically below.
 

b
 

Struts 2 developers have identified all these modules and had builded the same. Below is how a struts 2 design will look like.
 
d
 
Here Interceptor is the gate way through which a request is passed. It takes care of getting user request parameters,
 
Struts 2 framework get all the request parameters from interceptor and makes it available for Action class and we don’t need to fetch it in the action class, because struts 2 makes it available.
 
Here Struts.xml acts as controller which Controls the execution flow of the request, Action class represents Model and Jsps represent View layer.
 
Note:
 
In struts 2 the data can be displayed in jsp with tab library,
 
In our next tutorial, we will learn to set up struts 2 environment in eclipse.
 

Read More

What’s New in JDK 8

Posted by in Java

 
Oracle announced the general availability of Java 8 on March 18 2014. One of the features is complete removal of Permanent Generation (PermGen) space. The JDK 8 HotSpot JVM is now using native memory for the representation of class metadata and is called Metaspace”; similar to the Oracle JRockit and IBM JVM’sThe good news is that it means no more java.lang.OutOfMemoryError: PermGen problems and no need for you to tune and monitor this memory space anymore.

In Java 8, we will still need to worry about the class metadata memory footprint. Please also keep in mind that this new feature does not magically eliminate class and classloader memory leaks. You will need to track down these problems using a different approach and by learning the new naming convention.

  • Refer the article at Dzone.com for details about the PermGen successor: Metaspace.
  • JDK 8 Release Notes can be found here.  It covers – what’s new in JDK8, Compatibility info, known issues, troubleshooting guide etc.
  • Here is another article from techempower.com, which contains a summary of the developer faced changes introduced in java 8.
  • The motive for removing permanent generation is mentioned at openjdk.java.net
Read More

Extracting Data from Corrupted files using WinRAR

Posted by in Software

 
If your RAR file is corrupted and when you try to extract the file in it, all you get is an empty folder.
 
To overcome this issue, check the Keep Broken files checkbox, while choosing the location to save the file to be extracted as shown below.
 
winrar
 

Read More

How to download video from YouTube without any software

Posted by in Software | 1 comment

 
In this article we shall an easy way to download video from YouTube without using any software.
 
youtube_instant_2
 

Step 1:

 
Go to the desired video in YouTube which you are about to download
 
download youtube vedio
 
Here the URL is

www.youtube.com/watch?v=ip_0CFKTO9E

 
**Update:- Read Article on how to extract Data from Corrupted files using WinRAR
 

Step 2:

 
To download this video you just need to type in the letters ‘ss’ before the YouTube url, for example after typing the letters ‘ss’ the url will be as below.
 

www.ssyoutube.com/watch?v=ip_0CFKTO9E

 
Now on clicking enter button in the URL, your page will be redirected to another website as shown below.
 
download youtube vedio 2
 
In this website you will be provided with the privilege to download videos at different format.
 

Read More
Page 5 of 41«First...34567...102030...Last»