title: Understanding Groovy/Grails classloader leak
body: <p>Yesterday I deployed my first Grails (2.3.6) app to a dev server and began monitoring it. I just got an automated monitor stating that CPU was pinned on this machine, and so I SSHed into it. I ran <code>top</code> and discovered that it was my Java app's PID that was pinning the server. I also noticed memory was at 40%. After a few seconds, the CPU stopped pinning, went down to a normal level, and memory went back down into the ~20% range. Classic major GC.</p>
  
  <p>While it was collecting, I did a heap dump. After the GC, I then opened the dump in JVisualVM and saw that most of the memory was being allocated for an <code>org.codehaus.groovy.runtime.metaclass.MetaMethodIndex.Entry</code> class. There were almost 250,000 instances of these in total, eating up about 25 MB of memory.</p>
  
  <p>I googled this class and took a look at it's <a href="http://groovy.codehaus.org/api/org/codehaus/groovy/runtime/metaclass/MetaMethodIndex.Entry.html" rel="noreferrer">ultra helpful Javadocs</a>. So I still have no idea what this class does.</p>
  
  <p>But googling it also brought up about a dozen or so related articles (some of them SO questions) involving this class and a PermGen/classloader leak with Grails/Groovy apps. And while it seems that my app did in fact clean up these 250K instance with a GC, it still is troubling that there were so many instances of it, and that the GC pinned CPU for over 5 minutes.</p>
  
  <p>My questions:</p>
  
  <ul>
  <li>What is this class and what is Groovy doing with it?</li>
  <li>Can someone explain <a href="https://stackoverflow.com/a/779221/892029">this answer</a> to me? Why would <code>-XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled</code> help this particular problem?</li>
  <li>Why is this class particularly troublesome for the PermGen?</li>
  </ul>