setContextClassLoader implications

I'm trying to clean up Tomcat warnings about memory leaks due to creating my own threads. proposes that I call myThread.setContextClassLoader(null) prior to starting the thread.

What is the implication of this call? Will the code in the run() method still be able to resolve classes from my application?


Yes it will. The Thread.getContextClassLoader() is a mechanism for generic frameworks load resources from further down the class loader tree.

Take Tomcat's class loader hierarchy.

       /     \
  Webapp1   Webapp2 ... 

The servlet or JSP framework resides in the Common class loader. If one of these frameworks is to load a classpath resource from Webapp1, they could try:

getClass().getResource('/some/resource/in/webapp1'); // fail

But since class loading mechanics only delegates calls up the class loader chain, this would fail. This means that all frameworks that needs to load a resource instead do:


And the servlet container makes sure this is the Webapp1 class loader whenever the thread is executing in that context. So the thread's context classloader is effectively a way for frameworks to load classes from "the wrong direction".

When you spawn a new thread, that thread gets, by default the context class loader of its parent (your Webapp1 classloader). If you consequently stop Webapp1, tomcat is supposed to be able to GC that webapp, but is unable to do so as long as there's any reference left to the Webapp1 class loader - hence the warning.

Good article about context class loaders:

Need Your Help

BlackBerry 10 development toolkit

android blackberry-10 blackberry-eclipse-plugin android-2.2-froyo

I am confused how to start BlackBerry 10 development. I followed this to start with a simple project to get to know BlackBerry 10 development. I have everything in place that is said on the page. W...

Why are blank and null distinct options for a django model?

django django-models

When defining fields in a django model, there are two ways to say that the field is allowed to be empty. null means it can be empty in the database, and blank says the field can be empty in a form...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.