Last active
August 29, 2015 14:05
-
-
Save TanyaGaleyev/61cdc9b930e0cc1d6429 to your computer and use it in GitHub Desktop.
ThreadLocal leak
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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