Monitoring and detecting memory leaks in your java application

So your application is running out of memory, you’re spending days and nights analyzing your application hoping to catch the memory holes in your objects.

The next steps will explain how to monitor and detect your memory leaks to make sure your app is on the safe side.

1. Memory leak suspicion:

If you have a suspicion there is a memory leak a convenient way to make sure it’s really there is using jconsole.

You can locally or remotely connect jconsole to your app and let it monitor for a while(Hour, Half day, Overnight, Week..)

After connecting jconsole to your app start analyzing the “Memory” tab.

A memory leak suspicion would look like this:


memory-leak

2. How to find the leaking sources in your application:

For this purpose I recommend using jisualVM. this tool is part of the JDK.

Inside jvisualVM you can take Heap Dump( Inside the “Monitor” Tab).

Please keep in mind that it’s not possible to create Heap-Dump remotely.

You’ll need to either run jvisualvm on the same machine or execute jmap command to produce a Heap-Dump file and import it later into jvisualvm.

* Jmap is an oracle tool that prints all objects memory map tree for a given process.

Here’s a jmap documentation:
http://docs.oracle.com/javase/7/docs/technotes/tools/share/jmap.html.

So basically you run the jmap on your remote server(for instance your production environment) and then analyze that file locally.

I recommend to do several Heap dumps. That will give you better picture whether you have memory leaks or not.

3. Analyzing the Heap dump file:

I personally like to use MAT(Memory Analyzer) (http://www.eclipse.org/mat/)

MAT takes the heap dump file and helps you find memory leaks.

MAT shows exactly which instances have memory growth suspects.

You might notice Java libraries instances as a ‘Problem Suspect’ such as: “java.lang.Class” but this is normal.

Example for a leak detection:

leaked

Here you can see the exact instance which is suspected as a leaking component.

4. Analyze suspected objects:

Next step is to press on the details field of the suspected instance and investigate the objects inside:

leak_details

In the above example we can see clearly that field of type TreeMap is growing.

5. Fix your leak and run the test again:

 Now what’s left is to understand and fix your leaking source – but ofcoruse this is individual for each object.

These step-by step directions will help you detecting the leaking memory objects.

That’s it for now,

Idan

Related Articles:

(11634)

Spring Integration key notes with real life scenarios

Hi,

This post will summarize Spring Integration (SI) important key notes together with messaging Integration patterns.

For each key note I will add a real life example based on my experience.

Splitter:

a. What it does?

The Splitter is actually a pattern which receives one message in one point and split it into several.

b. Why is it good for me?

The Splitter can be used when you want to split your content into multiple parts. Each part will process the content asynchronously.

c. Real life example

In trading systems you can have a trading-request. The request suppose to be executed following a couple of steps: (execute it against a provider, logging it in the DB, send notification to the client)

With Splitter you can do all at once.

d. SI example:

<int:splitter id="tradeSplitter" input-channel="tradeSplitterInputChannel" output-channel="outPutChannel">

Router:

a. What it does?

The Router is a mechanism when a message being received in one point and by a content based criteria it will dispatch to a specific destination.

b. Why is it good for me?

The router is very handy when you actually have different message inputs that need to be handled  differently based on their content.

c. Real life example

So you have a notification feature in your infrastructure. Each client chose to be noticed differently (Email, Sms, Fax, Webservice,  etc..). Notification request being sent to the client and based on the request content the router will choose to which component the request will be handed: EmailService, SmscService, FaxService and so on..

d. SI example:

router id="notificationsRouter" input-channel="notificationInputChannel"

Aggregator:

a. What it does?

The Aggregator role is to wait for a group of related messages. After all expected messages retrieved it will be able to merge them and send a single out result. The Aggregator able to handle different group messages simultaneity by a group correlation Id.

b. Why is it good for me?

Usually the Aggregators works together with a Splitter. After you splitting your request to multiple destinations parallel you might need to aggregate all destinations results and send final result as an output.

c. Real life example

In trading systems you get different prices from different providers. It’s very often to choose the best price among all providers. The Aggregator will be able to aggregate all prices and send back the best one.

d. SI example:

<int:aggregator input-channel="bestPriceAgg" output-channel="bestPriceChannel"
send-partial-result-on-expiry="true">
</int:aggregator>

Filter:

a. What it does?

In short: “Do you want to get this message?”

b. Why is it good for me?

Your component might get different messages. Instead of processing unnecessary messages you could filter the messages based on the message payload ( In the EJB world it’s very similar to Message Driven Bean Selectors).

c. Real life example:

In the trading system world you could choose whether the commission in the final price should be included or not. Your component role is to add commissions only to the clients which wish for it. That component via filter will receive only the prices which are commission based.

d. SI example:

<filter input-channel="commisionsChannel" ref="selector" output-channel="output"/>

Transformer:

a. What it does?

The transformer can receive a message and convert it’s payload from one type to another.

b. Why is it good for me?

You could have convert your message format from one to another based on your destination.

b. Why is it good for me?

Many times we receiving input in one format and we need to parse or convert them to other format after adding/modifying/deleting the payload content ( For example from XML into String and vice versa).

c. Real life example

So you have an order request. That request need to be placed in DB in a specific format, After that it needs to be sent over to the client within an xml format. You could transform the payload to fit your destination needs.

d. SI example:

<object-to-string-transformer input-channel="in" output-channel="out"/>

There are  additional components in SI. Those are the most popular ones. That’s all for now, Idan.

Related Articles:

(11634)