Search code examples
javamemory-managementmemory-leaksclassloader

Are ClassLoader Memory Leaks As Bad As They Seem?


I have been working with java for a long time now, and I am starting to consider memory leaks. I intend to use classloader because I want to make my program more dynamic. After a bunch of googling, I read more about the ugly objects that stay loaded even when you dereference them.

I am concerned about my program having memory leaks. I know that all programs will have some amount of memory leaks, but is java really on the ugly side for leaking memory? My question in that department:

Is java worth dealing with for the memory leaks and are the memory leaks in java really as huge as google made them sound?

How can I write my code to keep down memory leaks, like how should I reference objects with classloader and how should I dereference them?

Is there an alternative to classloader without the problems (or as bad of problems) with retaining objects?


Solution

  • Classloader memory leaks will only affect you if you do a lot of dynamic class loading, one way or another. For example:

    • by calling Class.forName() repeatedly
    • by creating dynamic proxy classes repeatedly
    • by repeated hot redeployment of webapps (e.g. with Tomcat)

    Most applications don't do these things, and therefore don't have to worry about classloader memory leaks.