Skip to content

Instantly share code, notes, and snippets.

@TanyaGaleyev
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TanyaGaleyev/61cdc9b930e0cc1d6429 to your computer and use it in GitHub Desktop.
Save TanyaGaleyev/61cdc9b930e0cc1d6429 to your computer and use it in GitHub Desktop.
ThreadLocal leak
package org.ivan.threadlocal;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* Created by ivan on 17.08.2014.
*/
@WebListener
public class BadListener implements ServletContextListener {
private static ThreadLocal<HugeClass> local = new ThreadLocal<HugeClass>() {
@Override
protected HugeClass initialValue() {
return new HugeClass(Thread.currentThread().getName());
}
};
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("Context initialized: " + local.get());
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("Context destroyed: " + local.get());
// local.remove();
}
}
package org.ivan.threadlocal;
/**
* Created by ivan on 17.08.2014.
*/
public class HugeClass {
private static final byte[] poison = new byte[256 * 1024 * 1024];
private final String value;
public HugeClass(String value) {
this.value = value;
}
@Override
public String toString() {
return value;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment