Glassfish 4 Memory Leaks
It seems Glassfish 4 is full of memory leaks. I've captured a heap dump after Glassfish has been running for a few weeks and I've noticed the old generation space is about full (~600MB) according to jconsole . I've actually had Glassfish lockup because I hadn't restarted it for a few weeks. I used the Eclipse Memory Analyzer to look at the heap and it shows that the class com.sun.enterprise.container.common.impl.managedbean.ManagedBeanManagerImpl is retaining about 400 MB of objects via the jcdiManagedBeanInstanceMap. The Glassfish server is very lightly loaded and is idle 99% of the time. If I take a heap dump after restarting I see that the total heap size is only 135 MB, whereas with the Glassfish instance which is about to hang the total heap size is over 600 MB.
I found a reference to an issue with Weld and replaced the Weld jar to version 2.0.5.Final as suggested here: https://issues.jboss.org/browse/WELD-1564 (discussion here: https://community.jboss.org/message/848709) and it has made things better - now you will no longer see "org.jboss.weld.bean.interceptor.WeldInterceptorClassMetadata" classes grow without bound in your heap dumps. However, a different memory leak seems to still be present.
There is an old bug that seems to match up with what I am seeing: https://java.net/jira/browse/GLASSFISH-18650.
Another similar bug: https://java.net/jira/browse/GLASSFISH-20474.
It seems integrating CDI into Glassfish in a way that doesn't cause memory leaks might need another try.
I've attached the output from "jmap -histo" on the heap dump. You can see there is 2.5 million instances of java.util.ArrayList., 1.1 million instances of org.jboss.weld.util.collections.ArraySet, etc.
There are 162,652 instances of the ForEach tag (org.apache.taglibs.standard.tag.rt.core.ForEachTag). I use only a handful in my JSP pages, Glassfish is basically idle, and I use mostly stateless contexts (RequestScoped / @Stateless EJB), so why are all of these ForEach tags hanging around?
I've also attached the Leak Report output from the Eclipse Memory Analyzer Tool, which shows the jcdiManagedBeanInstanceMap is hanging onto about 400MB.