Search code examples
javatomcatmemory-leaksglassfishthread-local

ThreadLocal memory leak in Glassfish


Will the ThreadLocal cause memory leak in Glassfish server like it leaks in Tomcat? Why? http://wiki.apache.org/tomcat/MemoryLeakProtection


Solution

  • Yes, it will leak and Glassfish won't even warn you according to this relatively recent Glassfish JIRA issue:

    http://java.net/jira/browse/GLASSFISH-14128

    What needs to be said however is that the ThreadLocal specific leaking is not a 'bug' in app/web servers per se, but a problem with code in components running in those containers (whether these components are servlets, session beans or whatever).

    What app servers/web containers try to do in general is to shield developer from writing a lot of maintenance code and to make him focus on business logic. There needs to be however some understanding on his part of how the application server works (thread pools, classloaders, deploy/undeploy mechanism, ...) so that stuff like this ThreadLocal issue is done properly or avoided. It is not always easy and it can be very tricky. I remember reading about a memory leak issue in Glassfish? related to use of custom log levels.

    What Apache Tomcat does is that it has a helper mechanism to warn user/deal with some commonly occurring memory leak issues in user code. But even in the link provided in the question, you may read that not all possible ThreadLocal memory leaks are done automatically using this mechanism.

    Glassfish does not seem to have this added functionality yet.