Wednesday, October 28, 2015

Diagnosing memory leaks in Java

Every time I suspect a memory leak, I have to go dig up these commands.
So, here they are for posterity's sake:

First, I use the following command to monitor the process over time:

while ( sleep 1 ) ; do ps -p $PID -o %cpu,%mem,rss  ; done

(and/or New Relic if you have it ;)

If you see memory creeping up, it could be your VM settings. If you haven't explicitly specified memory settings to the JVM, it will default them. To get the defaults, use the following command:

java -XX:+PrintFlagsFinal -version | grep -i HeapSize

If those are out of line with what you want, then you'll need to specify the memory settings for the
JVM. You can set minimum and maximum heap sizes with the following:

java -Xms128m -Xmx256m

Once you have sane memory settings, and you can monitor the process, you might legitimately still see memory increasing over time. To get insight into that, you can start by taking a look at the histogram of object instances using the following:

jmap -histo $PID

If that isn't enough information then you can take heap dumps with the following command:

jmap -dump:format=b,file=/tmp/dump1.hprof $PID

Typically, I'd take two heap dumps and then compare them using jhat using the following command:

jhat -baseline /tmp/dump1.hprof /tmp/dump2.hprof

That fires up an HTTP server that you can use to explore the delta between those two heap dumps. By default, the HTTP server will start on port 7000, which you can just hit in a browser.

If you are behind a firewall, but have ssh access, you can tunnel over to the port using:

ssh -L 7000:localhost:7000 $HOST

If you scroll down to the bottom of the first page, you will see two useful links: "
That will show you all "new" instances between the different heaps, which should give you some idea of where your leak is coming from.  Screenshot below:

And there you have it, a quick synopsis of the magic command-lines you need when diagnosing memory leaks. (which I always forget)


Daniel Drozdzewski said...

Brian, great and succinct set of free tools and practices.
One questions remains in my head:
- Should you not ensure (or rather observe) a garbage collection, before creating a second heap dump?

I think this would filter some false positives.

Thanks a lot!

Steve Smith said...

Great And Useful Article

Online Java Training from India

Java Training Institutes in Chennai

Cheryl Livecodingtv said...

Nice Article!

zafor sadik said...

java is one of the most dynamic, powerful and popular programming languages used today. If you are
new to using Java, you would be amazed at the excelling features that Java offers and why it is the most preferred programming languages among professional programmers.Java is the best platform for
building high-end applications, and there are a number of ways in which programmers can use Java to create reliable and reusable applications. This article discusses seven best books that help developers
to master various features of Java application development.for more information visit:webmarketing

siddu said...

Thank you for sharing helpful information.

online java training
online advanced java training
online core java training

Mervin Parmar said...

Nice article hoping to read more of these from you.

struts training in chennai|struts training|struts training center in chennai|struts course in chennai

Murali Rajesh said...
This comment has been removed by the author.
Murali Rajesh said...

Hibernate and spring are the frameworks of Java. A java developer should be well aware of these frameworks in order to master the technology and work efficeiently.
spring training in chennai | hibernate training in chennai
FITA Academy reviews

Raghu Sharma said...

Java is the most robust secured and multi threaded programming language which is the reason why most the the developers go for java. A single java code can be used for various platforms.
JAVA training in chennai | java training institutes in chennai | FITA Academy Chennai

Murali Rajesh said...

The best thing about HTML5 is that it allows the developers to embed the video files, audio files, and high quality graphics without any third party applications.
html5 training in chennai | html5 training institutes in chennai | FITA Academy reviews

Raghu Sharma said...

Hi, actually I'am new to angularJs and infact I'am learning angularjs with online training. I'am having doubt, if you could solve the doubt for me that would be very helpful. The doubt is, how can I reset a “$timeout”, and disable a “$watch()”?
angularjs training in Chennai | angularjs training | angularjs training Chennai

Dhivya Shree said...

The main thing which i like about web designing is that it needs creativity and we need to work differently acccording to our clients need this needs a creativity and innovation.
web designing course in chennai | web designing training in chennai | web designing courses in chennai

Raghu Sharma said...

Oracle database management system is a very secure and reliable platform for storing database and secured information.Due its reliable and trustworthy factor oracle DBA is famous all around the globe and is prefered by many large MNC which are using database management system.
oracle training in Chennai | oracle dba training in chennai | oracle training institutes in chennai

Dhivya Shree said...

Java is a programing language which needs no introduction. Java is immensly popular anguage which is used in building softwares in mobile app or desktop. Even today java is used to program tools like hadoop, owing to this java has becom imensley popular and one of the most preffered language around the world.
Java training in Chennai | Java training institute in Chennai | Java course in Chennai

Mervin Parmar said...

Hadoop is one of the best cloud based tool for analysisng the big data. With the increase in the usage of big data there is a quite a demand for hadoop professionals.
Big data training in Chennai | Hadoop training Chennai | Hadoop training in Chennai

Mervin Parmar said...

I have completely read your post and the content is crisp and clear.Thank you for posting such an informative article, I have decided to follow your blog so that I can myself updated.
Android training in Chennai | Android course in Chennai | Android training institute in Chennai

Bay Max said...

I am really happy to see this kind of blog.This made me to understand the concepts very clear.Keep sharing.
JAVA Training institutes in chennai | Java Courses in Chennai

Corpits Chennai said...

Awesome post!
Android Training in Chennai | Best Android Training Institute in Chennai

Nguyễn Quỳnh said...

Máy lạnh Reetech
Máy lạnh Samsung
Máy lạnh Aikibi
Máy lạnh Nagakawa
Máy lạnh Midea

Máy lạnh Mitsubishi Electric
Máy lạnh Mitsubishi Heavy
Máy lạnh Sumikura
Máy lạnh Sanyo
Máy lạnh Funiki
Máy lạnh Sharp
Máy lạnh Hitachi
Máy lạnh Ascent
Máy lạnh Kendo

Devi.Angularjs said...

JavaScript Training in Chennai | Ecmascript 6 Training in Chennai | ES6 Training in Chennai | Angular 2 Training in Chennai | HTML5 Training in Chennai

Yeoman Training | D3 Training | ReactJS Training | Gulp Training | CommonJS Training

Devi.Angularjs said...

JMS Training Institutes in Chennai | JSP Training Institutes in Chennai | Java Spring Hibernate Training Institutes in Chennai | EJB Training Institutes in Chennai | Hibernate Training Institutes in Chennai |

JSF Training Institutes in Chennai | Java EE Training Institutes in Chennai | J2EE Training Institutes in Chennai | Core Java Training Institutes in Chennai | Java Training Institutes

Devi.Angularjs said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

Online Java Training Java Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai

1croreprojects said...

very nice post......

be projects in chennai

2016 ieee projects in chennai
java projects in chennai

Rekha J said...

This is my first visit to your blog, your post made productive reading, thank you. dot net training in chennai

1croreprojects said...

Thanks for sharing this valuable information.
ieee java projects in chennai
ieee dotnet projects in chennai
mba projects in chennai
be projects in chennai
ns2 projects in chennai
mca projects in chennai
bulk projects in chennai
bsc projects in chennai
msc projects in chennai

DataBytes Bangalore said...

Enroll for Java Training in Bangalore with DataBytes; best Java Coaching Institutes in Bangalore.

Anamika Sharma said...

Firstly, I would like to appreciate the innovative approach of Android training course trainer to train the students. I guess I’m lucky enough to get trained by your institute. And a special thanks for providing the training materials

Cửa Nhựa said...

Cửa Nhựa Lõi Thép
Cửa nhựa giá rẻ
Cửa sổ mở quay giá rẻ

apto inn said...

You post explain everything in detail and it was very interesting to read. Thank you. nata coaching centres in chennai

Deepakala said...

Hi,Thanks a lot and really happy to see such a wonderful comment.

Java and J2EE Training in Chennai - AmitySoft

jazz said...

Excellent and very cool idea and the subject at the top of magnificence and I am happy to this post..Interesting post! Thanks for writing it.What's wrong with this kind of post exactly? It follows your previous guideline for post length as well as clarity..
Android Training in Chennai

Naveen Yadav said...

Nice post. You just post the right content i was searcing for. Good Job. Java Training in Delhi | Java Training Institute in Delhi Java Training allows you to offer both standard and custom courses that will manipulate you from being a novice to an App-Maker

Jagjeet Kirarkot said...

Most of the ideas can be nice content. Thank you for sharing.

Java Course Training in Sector 14, Gurgaon

Nandhini said...

Thank you for sharing such a nice and interesting blog with us. Hope it might be much useful for us. keep on updating...!! Java Training in Chennai

Karthika Shree said...
This comment has been removed by the author.
Karthika Shree said...

This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
Java Training in Chennai

Shreeja K said...

Informative article, just what I was looking for.seo services chennai

Nancy 12 said...

Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog. Java Training in Chennai

Abiya Carol said...

All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.

Digital Marketing Training in Chennai

Hadoop Training in Chennai

pavitha vinu said...

A memory leak in Java can occur if you forget to close a resource, or a reference to an object is not released.
Java Certification Training in Chennai

Priya Kannan said...

It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command.
Python Training in Chennai

Ajay Raj said...

Nice Posting...

Vamshi Krishna said...

I have seen lot blogs and Information on othersites But in this Java Blog Information is very useful thanks for sharing it........

Sarah Rose said...

Before choosing a Job Oriented Training program it is important to evaluate your skills, interests, strength and weakness. Job Oriented Courses enable you to get a identity once you finish the same. Choose eNvent software Technology that suits you and make your career worthwhile.

venkatesh raghunath said...

Good one, very informative.. thanks for sharing your views and ideas.. it is very useful to me.. thanks once again..

Android Training in chennai | PHP Training in chennai | Web Designing Training in chennai

Abinaya V said...

This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.So join as Start You Career . Java Training Institute in Chennai |
Dot Net Training Institute in Chennai |

Codec Networks said...

your blog is good information about JAVA training in Delhi . thank for post this blog
JAVA certification in Delhi