Blog

The latest expert opinions, articles, and guides for the Java professional.

Reloading Java Classes 201: How do ClassLoader leaks happen?

  • Responses (26)

    1. Avatar  

      Chas Emerick

      December 10, 2009 @ 2:48 pm

      The corollary to this is that, if one isn’t holding on to any object references from the old classloader regime, then leaks are strictly impossible. Correct, or no?

    2. Avatar  

      Chas Emerick

      December 10, 2009 @ 2:48 pm

      The corollary to this is that, if one isn’t holding on to any object references from the old classloader regime, then leaks are strictly impossible. Correct, or no?

    3. Avatar  

      Jevgeni Kabanov

      December 10, 2009 @ 2:58 pm

      @Char
      The things that can hold the classloader are classes loaded with it, objects created from them and direct references to the classloader instance. If none of them are held, the classloader and its classes are garbage collected. However references can dangle from most surprising places, e.g. javax.security.auth.Policy saves a reference to the context classloader that you can’t remove no matter what you do.

    4. Avatar  

      Jevgeni Kabanov

      December 10, 2009 @ 2:58 pm

      @Char
      The things that can hold the classloader are classes loaded with it, objects created from them and direct references to the classloader instance. If none of them are held, the classloader and its classes are garbage collected. However references can dangle from most surprising places, e.g. javax.security.auth.Policy saves a reference to the context classloader that you can’t remove no matter what you do.

    5. Avatar  

      Markus Kohler

      December 10, 2009 @ 4:27 pm

      Great blog! BTW the acronym we usually use it “MAT” :-)

    6. Avatar  

      Markus Kohler

      December 10, 2009 @ 4:27 pm

      Great blog! BTW the acronym we usually use it “MAT” :-)

    7. Avatar  

      Khairul Ikhwan

      December 10, 2009 @ 4:48 pm

      Great, i had this problem before and already solved it. Now, im working for new Java project for client and that project is already live on the production server which i cannot be remote from my office. I use tomcat6 to run it and i have file manager to upload or download any file from the server. When i made changes to the classes or library, i need to reload the application’s contact. Then i use tomcat application manager to stop and start that application. Then i face new problem that is OutOfMemoryError: PermGen space. This error is very bad as it will crashed the tomcat after a few minutes. When i use Java monitoring tools, i see that the PermGen space does not cleaned when i stop and start the context and will increase until it reach maximum. To overcome this, i use to set the MaxPermGenSpace in the Java_Opts. However, this only increase the memory and not sweep all the unused garbage.
      Maybe you can provide some tips on this problems.. :)

    8. Avatar  

      Khairul Ikhwan

      December 10, 2009 @ 4:48 pm

      Great, i had this problem before and already solved it. Now, im working for new Java project for client and that project is already live on the production server which i cannot be remote from my office. I use tomcat6 to run it and i have file manager to upload or download any file from the server. When i made changes to the classes or library, i need to reload the application’s contact. Then i use tomcat application manager to stop and start that application. Then i face new problem that is OutOfMemoryError: PermGen space. This error is very bad as it will crashed the tomcat after a few minutes. When i use Java monitoring tools, i see that the PermGen space does not cleaned when i stop and start the context and will increase until it reach maximum. To overcome this, i use to set the MaxPermGenSpace in the Java_Opts. However, this only increase the memory and not sweep all the unused garbage.
      Maybe you can provide some tips on this problems.. :)

    9. Avatar  

      Jevgeni Kabanov

      December 10, 2009 @ 5:59 pm

      @Markus
      Thanks! MAT doesn’t make sense anymore :)

    10. Avatar  

      Jevgeni Kabanov

      December 10, 2009 @ 5:59 pm

      @Markus
      Thanks! MAT doesn’t make sense anymore :)

    11. Avatar  

      Sakuraba

      December 12, 2009 @ 11:50 am

      That and nothing else is the biggest issue of Java and it terrifies me that for at least a decade nothing has been achieved or at least been started by Sun to finally get rid of this mess.

    12. Avatar  

      Sakuraba

      December 12, 2009 @ 11:50 am

      That and nothing else is the biggest issue of Java and it terrifies me that for at least a decade nothing has been achieved or at least been started by Sun to finally get rid of this mess.

    13. Avatar  

      SMiGL

      December 23, 2009 @ 11:21 am

      Great, Thanks!

    14. Avatar  

      SMiGL

      December 23, 2009 @ 11:21 am

      Great, Thanks!

    15. Avatar  

      Ahriman

      January 29, 2011 @ 9:34 am

      Jevgeni,

      Can i make translation of this post and publish it to http://habrahabr.ru?
      Please answer ahriman@tpu.ru

    16. Avatar  

      Jevgeni Kabanov

      January 29, 2011 @ 9:59 am

      You mean the whole series or this particular article?

    17. Avatar  

      Ahriman

      January 29, 2011 @ 10:35 am

      Sure.

    18. Avatar  

      Ahriman

      January 30, 2011 @ 7:26 am

      Jevgeni,
      Sorry, “sure” should be “sure the whole series”.

    19. Avatar  

      Valeriy Soglaev

      June 29, 2011 @ 8:53 pm

      Евгений, я могу сделать перевод этого поста и опубликовать его на http://eProfil.net ?

    20. Avatar  

      Jevgeni Kabanov

      June 30, 2011 @ 9:30 am

      Пожалуйста.

    21. Avatar  

      Hrish

      October 11, 2011 @ 9:39 am

      On the contrary, a lot had been achieved by the erswhilte Sun in the form of optimizing the JVM and building some great tools to debug such issues.

    22. Avatar  

      bob

      November 20, 2011 @ 4:11 am

      Thread.currentThread().sleep(3000);

      should be written as
      Thread.sleep(3000);since sleep is a static method

    23. Avatar  

      Grudje

      November 15, 2012 @ 11:50 am

      Mine never crashes. I increased the cache to 50 million elements and as I type it has got to over 1000 iterations (I reduced the wait time !!). If I increase it to 100 million elements it crashes immediately with out of memory error.

    24. Avatar  

      Grudje

      November 15, 2012 @ 12:23 pm

      I tried the downloaded version and that works although I did have to replace ExampleFactory.getClassPath() with the one that gets the real classpath. And I set the cache size at 10 million and it failed after 16 iterations.

    25. Avatar  

      Grudje

      November 15, 2012 @ 2:27 pm

      OK eventually it did crash, after about 1500 iterations. Not sure what the difference was.

    26. Avatar  

      Mattias Jiderhamn

      August 22, 2013 @ 7:44 pm

      I’d recommend anyone having problems with classloader leaks to have a look at this library that helps preventing OutOfMemoryError: PermGen space by cleaning up the leaks: http://github.com/mjiderhamn/classloader-leak-prevention

      You can also read more about what causes the leaks and how to avoid them on my blog http://java.jiderhamn.se/2011/12/11/classloader-leaks-i-how-to-find-classloader-leaks-with-eclipse-memory-analyser-mat/

    RSS feed for comments on this post.

    Leave a comment