I have the following classes:
public class AppContext {
private static final ThreadLocal<AppContextData> contextHolder = new ThreadLocal<AppContextData>() {
@Override
protected AppContextData initialValue() {
return new AppContextData();
}
};
public static AppContextData get() {
return contextHolder.get();
}
public static void unset() {
contextHolder.remove();
}
}
public class AppContextData {
//many getter and setter methods
}
The website starts using the ThreadLocal object in an interceptor and the object is used through out a web request.
public class MyIntercepter extends HandlerInterceptorAdapter {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
AppContext.get();
//.....
}
}
I run this website on Tomcat 8.x and I find out that the log has the following error message.
20-May-2016 22:43:53.657 SEVERE [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [my.organization.data.AppContext$1] (value [my.organization.data.AppContext$1@31d4463a]) and a value of type [my.organization.data.AppContextData] (value [my.organization.data.AppContextData@e89d5f4]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
How can I fix this issue?