An effective way to fight duplicated libs and version conflicting classes using Memory Analyzer Tool

In this blog post I suggest a very effective way to spot duplicated libs/classes in your java application. I found and used this tip during the integration phase on an application server - which is quite a common use case. This tutorial will be very helpful in the following cases:

  • NoClassDefFoundError:  there are two different versions of the same class in your classpath.
  • Integration with unknown huge products (not osgi-ed):  detect multiple libs with the same version, and so reduce memory footprint.

Environment preparation

The first step to do - What? You haven't already installed it? - is to install the Memory Analyzer Tool (MAT). Then you have to acquire  a heap dump of your running application. These steps are explained in one of my previous blog post.

Easy and effective

Now that you have the heap dump with MAT, you are just three clicks away from spotting those annoying duplicated libs and version conflicting classes. Go to Open Query browser->Java basics->Duplicated classes:

And here we are - you have the list of all duplicated classes with their classloaders:

Please notice that the class must be already loaded.

A last tip

Since you have made it this far, I will give you another little tip. You can find exactly from which lib the class was loaded using the Inspector view. For instance, in case the duplicated class was loaded by an URLClassloader, just follow these steps:
  • Select the class;
  • Go to Inspector view in Attributes tab;
  • Right-click on  _context;
  • and finally click go into.

From here look at the attribute _war and you will find from which archive the class was loaded.

Do you have some special tips to use MAT?
Note: this was part of my EclipseCon Europe 2011 talk and one of my Eclipse Democamp Grenoble 2011 talk.