Possible Memory leak in Number of Loaded classes in Java Application

前端 未结 6 608
被撕碎了的回忆
被撕碎了的回忆 2021-02-02 02:29

I recently began profiling an osgi java application that I am writing using VisualVM. One thing I have noticed is that when the application starts sending data to a client (ove

6条回答
  •  别跟我提以往
    2021-02-02 03:36

    I'm willing to bet that your problem is related to bytecode generation.

    Many libraries use CGLib, BCEL, Javasist or Janino to generate bytecode for new classes at runtime and then load them from controlled classloader. The only way to release these classes is to release all references to the classloader.

    Since the classloader is held by each class, this also means that you should not release the references to all classes as well [1]. You can catch these with a decent profiler (I use Yourkit - search for multiple classloader instances with the same retained size)

    One catch is that the JVM does not unload classes by default (the reason is backwards compatibility - that people assume (wrongly) that static initializers would be executed only once. The truth is that they get executed every time a class is loaded.) To enable unloading, you should pass some use the following options:

    -XX:+CMSPermGenSweepingEnabled -XX:+CMSClassUnloadingEnabled
    

    (tested with JDK 1.5)

    Even then, excessive bytecode generation is not a good idea, so I suggest you look in your code to find the culprit and cache the generated classes. Frequent offenders are scripting languages, dynamic proxies (including the ones generated by application servers) or huge Hibernate model (in this case you can just increase your permgen).

    See also:

    1. http://blogs.oracle.com/watt/resource/jvm-options-list.html
    2. http://blogs.oracle.com/jonthecollector/entry/presenting_the_permanent_generation
    3. http://forums.sun.com/thread.jspa?messageID=2833028

提交回复
热议问题