Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.