Java Tech Updates

Java Tech Updates

  • Blog
  • Code
  • NEWS
  • About Us
  • Contact us
  • Privacy
  • Write for us
Home  /  Blog  /  DIAGNOSE MEMORY LEAK

DIAGNOSE MEMORY LEAK

Sandeep Dharak April 12, 2017 Blog Leave a Comment
memoryleak

There are 8 types of OutOfMemoryError. In this, 5 types of OutOfMemoryError happens within the JVM heap.

  1. Java heap space
  2. GC overhead limit exceeded
  3. Requested array size exceeds VM limit
  4. Permgen space
  5. Metaspace – Remaining 3 types of OutOfMemoryError happens outside the heap:
  6. Unable to create new native thread
  7. 7. Kill process or sacrifice child
  8. 8. reason stack_trace_with_native_method

Procedures to diagnose and solve all the 5 types of OutOfMemoryErrors that happen within the JVM heap are same. In this article, let’s explore how to diagnose these 5 types of OutOfMemoryErrors.

Tools
One of the immortal quotes from Abraham Lincoln is: “Give me six hours to chop down a tree, and I will spend the first four sharpening the axe”. He says the tool that we choose for the job plays a crucial role towards the success. It’s very true when it comes to diagnosing memory leaks. Thus, here we would like to recommend 2 important tools for diagnosing memory leaks:

  1. GCeasy

GCeasy is a universal Garbage collection log analyzer, which has the intelligence to analyze and detect memory problems from the garbage collection log files. Using this tool, you will be able to detect whether the application is suffering from memory leaks or not. This is an online tool; you don’t have to download or install it.

  1. Eclipse MAT

Eclipse MAT is a powerful JVM heap dump analysis tool. There are multiple JVM heap dump analysis tools. In fact, JDK itself ships with a heap dump analyzer called JHAT. But in my personal experience, I have found Eclipse MAT to work best, especially when handling large sized heap dumps. You can download Eclipse MAT from this location. Here are some important Eclipse MAT tit-bits to boost your productivity.

Procedure
Here are the steps to follow to diagnose memory leaks:

Step 0: Validate whether memory is leaking
In the military, there is a quote: “Don’t trust anyone.” This is very much applicable when it comes to diagnosing production problems. Because of the heat of the moment, fellow engineers might come up with a wrong conclusion that memory is leaking while there are no memory problems. So the first step in the diagnosis is to confirm whether memory is actually leaking or not. To confirm it, you need to do 2 things:

  1. In your log file or standard error, stream grep whether “java.lang.OutOfMemoryError” is printed. When allocated memory is saturated, JVM prints this error message.
    2. Upload your Garbage Collection log file toGC Easy. It has the intelligence to figure out whether the memory is leaking in the application or not.

If you get confirmation from either one of the sources, then proceed to next step. If not, your application could be experiencing some other problem and not the memory leak.

Step 1: Capture baseline heap dump
A heap dump is a snapshot of the memory of a Java process. The snapshot contains information about the Java objects and classes in a heap at the moment the snapshot is taken.

You need to capture heap dump from the application when it is in a healthy state. Typically memory leak starts to happen only after few minutes or few hours since the application has started. So after starting your application, let the application take traffic for 10 – 15 minutes, after this point capture the heap dump.

Tit-bit 1: 
Here are the step-by-step instructions to capture a heap dump.

Tit-bit 2: 
It’s always better to capture heap dump in the production environment (unless in the test environment you can mirror the exact production traffic pattern). Traffic type and it’s volume plays a primary role in the type and amount of objects created in the memory.

Tit-bit 3: 
Please be advised that capturing heap dumps takes time, anywhere from few seconds to few minutes based on your heap size. Thus when you are capturing heap dump from the production environment, disable the traffic to the JVM instance where you are capturing heap dump. This will help you to avoid any customer impact.

Step 2: Capture troubled heap dump
Let your application run until it starts to exhibit memory problems. Once memory problem starts to surface, capture heap dump once again. Now the question becomes, how will I know whether the application is exhibiting memory problems or not? Typically when memory problems start to happen, you will notice one of the following symptoms:

  1. CPU consumption might start to spike
  2. Application might become unresponsive
  3. “java.lang.OutOfMemoryError” string will be printed in your application log file or standard error stream

Have proper monitoring/alerting in place. Once any of these symptoms starts to happen, capture heap dumps.

Tit-bit 4: 
Sometimes, when application is in the middle of memory problems, you wouldn’t be able to capture heap dumps. In those circumstances, it’s OK (but not ideal) to capture heap dumps right before memory problems starts to appear.

Step 3: Do comparative heap dumps analysis
Upload the heap dump captured in step 1 to Eclipse MAT. Now note down the top 5 objects that are occupying the memory. Most of the time (if not all the time) memory leak surfaces because of one of the top 5 objects.

Now upload the heap dump captured in step 2 to Eclipse MAT. Note down top 5 objects. If any of them shows a significant increase, then that is the object that is leaking. Now you know which the objects that are leaking are. Eclipse MAT has the option to provide you the references of those objects. If you walk through the references, you would be able to figure which Object/method/line of code is causing this memory leak.

In most of the memory leaks, diagnosing is a painful process; fixing it might become very trivial. Equipped with this knowledge, what are the leaking objects and who is creating them, you should be able to fix the root cause of the memory leak.

Every single day millions & millions of people in North America – bank, travel and do commerce using the applications that Ram Lakshmanan has architected. Ram is an acclaimed speaker in major conferences on the scalability, availability, performance topics. Recently he has founded a startup, which specializes in troubleshooting performance problems

Previous Article
Next Article

About Author

Sandeep Dharak

Admin of "Java Tech Updates" Blog. You Can Get Technical Updates and Information About Java. Contact Me to Send or Post Your "Java Tech NEWS and TiPS" on Blog. We Will Post it, If Your Content is Informative, Unique and Helpful To Java Users.

Related Posts

  • Employee Performance

    Mobile & PC Screen Recorder to Measure Employee Performance

    March 18, 2019
  • Social Media Strategy

    Why Animated Video is a Best Strategy for Your Social Media

    March 4, 2019
  • Java anomaly detection tool

    Top 5 Java Anomaly Detection Tools

    February 14, 2018

May i Help You !

Your Name (required)

Your Email (required)

Subject

Your Message

Popular Posts of The Month

  • Java developers Team
    Essential APIs access could be critical with Java 9 October 22, 2015
  • Java development with JRebel & XRebel
    JAVA introducing the two diodes in this language November 19, 2015
  • How To Use Jenkins Web Based Build Management Tool In Java Applications? July 22, 2016
  • Programmers And Developers Need To Wait More For Java 9 December 10, 2015
  • Attractive features in Java SE 8u 40
    Attractive features in Java SE 8u 40 November 9, 2015
  • Java 9 releases by JPI
    New access program for supporting Java 9 releases by JPI November 25, 2015
  • Java Application Development Composure Of Miniprograms February 9, 2016
  • IOT and Java
    Is Java the Missing Link Between IoT and Future Application? February 1, 2017
  • Java Programmers And The Joy Of Application Development May 11, 2016
  • Java
    Tutorial Alert: Chain of Responsibility Pattern With JAVA September 16, 2016
  • Java Outsourcing
    Spring Data Rest – Repositories August 23, 2016
  • PROFESSIONAL JAVA DEVELOPERS
    How To Become Professional Java Developers? March 16, 2016
  • spring-factory-beans
    How to Use Spring Factory Beans in Java Development January 4, 2017
  • cross-culture-business-communication
    The Why and How of Cross-Cultural Business Communication November 8, 2016
  • scala-development-by-javaprofessionals.net
    Scala Development Vs Java- A Never Ending Debate January 1, 2016
  • java outsourcing india
    Top Reasons To Consider Java Development Outsourcing To India August 4, 2016
  • Java Software Development
    JPI To Offer Rapid Deployment Package To Java Development Teams December 21, 2015
  • java application development
    Java App Development Using Hibernate December 10, 2015
  • Java developers Team
    MyEclipse 2015 Updates for Java Developers December 7, 2015
  • cache management in java
    Cache Management in Java – Caching Techniques June 11, 2016

Recent Posts

  • Employee Performance
    Mobile & PC Screen Recorder to Measure Employee Performance March 18, 2019
  • Social Media Strategy
    Why Animated Video is a Best Strategy for Your Social Media March 4, 2019
  • Java anomaly detection tool
    Top 5 Java Anomaly Detection Tools February 14, 2018
  • Java development
    2017, stayed a big year of Twists in Java Development, Must Watch! February 9, 2018

What Others Are Reading

  • Social Media Strategy
    Why Animated Video is a Best Strategy for Your Social Media March 4, 2019
  • Angular 2 for Java
    Angular 2 For Java And Python – A Review By Java Outsourcing Vendor May 5, 2016
  • Java.VS in Visual Studio
    It’s Time to Code with JNBridge’s Java.VS in Visual Studio September 28, 2017
  • Know About the Spy Apps for Android August 14, 2017

Random Posts

  • Java Development Teams Need To Wait More as Java 9 Progress Delayed
    Java Development Teams Need To Wait More as Java 9 Progress Delayed April 8, 2016
  • Social Media Strategy
    Why Animated Video is a Best Strategy for Your Social Media March 4, 2019
  • developers
    How Developers Can Bring Real Talent To Your Company October 14, 2016
  • Future of Big Data
    The Future of Big Data IOT-Internet of Things June 15, 2017

Popular Posts

  • Java developers Team
    Essential APIs access could be critical with Java 9 October 22, 2015
  • Java development with JRebel & XRebel
    JAVA introducing the two diodes in this language November 19, 2015
  • Attractive features in Java SE 8u 40
    Attractive features in Java SE 8u 40 November 9, 2015
  • Programmers And Developers Need To Wait More For Java 9 December 10, 2015
Theme by Dashing Themes | Proudly Powered by WordPress