
The name of the parameter is -XX:+HeapDumpOnOutOfMemoryError, so add -XX:+HeapDumpOnOutOfMemoryError to your command line, script or configuration file – depending on what application server you are using and how you are starting it. This also means you won’t spend time looking for a leak in a heapdump where there is none. The advantage of this, is that you don’t have to try to force the appearance of the leak, in case you don’t know what triggers it. One of the easiest ways to do this, is to add a JVM parameter that makes the (Sun/Oracle) JVM automatically create a heapdump whenever a occurs. The heap should be dumped after at least one ClassLoader instance has leaked, so that you can analyze what references there are to the leaked instance, that prevents it from being garbage collected. The first thing you need to do to find a classloader leak, is to aquire a heap dump to analyze. I will show you step by step how to do it. Nowadays there are Open Source alternatives that makes it relatively easy to find the offending code. Later there were some commercial tools, such as YourKit to help you in the process. The tools at hand were jmap and jhat, which are quite “raw”. Not many years ago, finding the source of a classloader leak was really tricky – or at least I thought so.

After you’ve realized you have classloader leaks, you must identify where those leaks are, before you can fix them. What I will focus on in this first post, is the step between the “what” and the “how” – the “where” that is often forgotten in other online discussions. I will not explain what this error means nor the reason it occurs, since there is lots of information about it on the net – for example, see Frank Kieviet’s blogs on the problem and its solution.

You have probably arrived at this page because your Java web application crashes with the dreaded : PermGen space (or : Metaspace, if you’re on Java 8). I’m planning a series of posts around classloader leaks, also known as PermGen memory leaks.

If you just want a quick fix to the problem without understanding the theory, jump to part IV introducing the ClassLoader Leak Prevention library.
