advertisement

ClassLoader Leaks

60 %
40 %
advertisement
Information about ClassLoader Leaks
Technology

Published on February 19, 2014

Author: mjiderhamn

Source: slideshare.net

Description

Are you tired of java.lang.OutOfMemoryError: PermGen space? Then this talk is for you! We'll begin with a crash course in the Java memory model in order to understand what the error message means. Then we'll look at different causes of the error and how to avoid them. We may glance at a few interesting mistakes from the Open Source world. Last but not least you'll learn how you can get rid of java.lang.OutOfMemoryError: PermGen space once and for all.
advertisement

Classloader leaks Mattias Jiderhamn Mattias Jiderhamn – java.jiderhamn.se

:-( java.lang.OutOfMemoryError: PermGen space Mattias Jiderhamn – java.jiderhamn.se

Local dev env Mattias Jiderhamn – java.jiderhamn.se

Continuous Deploy Mattias Jiderhamn – java.jiderhamn.se

Java Memory Model Stack PermGen Heap Mattias Jiderhamn – java.jiderhamn.se

Java Memory Model Stack Per thread Local variables and method parameters Mattias Jiderhamn – java.jiderhamn.se

Java Memory Model Object instances Young generation Eden Survivor space space Heap Mattias Jiderhamn – java.jiderhamn.se Old generation / tenured space

Java Memory Model Permanent Generation java.lang.Class instances PermGen etc Named before JEE and class unloading Renamed Metaspace in Java 8 Mattias Jiderhamn – java.jiderhamn.se

Java Memory Model java.lang.OutOfMemoryError: PermGen space = Too many classes are loaded Mattias Jiderhamn – java.jiderhamn.se

Reason for OOME java.lang.OutOfMemoryError: PermGen space 1. Your application is too large -XX:MaxPermSize=256M Java 8: Metaspace auto increase by default 2. java.lang.Class instances could not be garbage collected after redeploy Mattias Jiderhamn – java.jiderhamn.se

Redeploy Application Server ClassLoader ClassLoader ClassLoader app.war app.war’ app.war’’ Mattias Jiderhamn – java.jiderhamn.se

Reference types • Strong (i.e. normal) reference – Never GC:ed if reachable • Soft reference – GC:ed before OutOfMemoryError • Weak reference (WeakHashMap) – GC:ed when no Strong or Soft refs • Phantom reference – … won’t prevent GC Mattias Jiderhamn – java.jiderhamn.se

Example WeakHashMap Map m = new WeakHashMap(); Foo myFoo = new Foo(); m.put(myFoo, ”bar”); myFoo = null; Foo Mattias Jiderhamn – java.jiderhamn.se

GC reachability GC roots Mattias Jiderhamn – java.jiderhamn.se

ClassLoader refs Application Server ClassLoader Class Instance Mattias Jiderhamn – java.jiderhamn.se Class Class

How leaks happen Application Server ClassLoader Class Instance Class GC root Mattias Jiderhamn – java.jiderhamn.se Class

Application Server • Application Server bugs • Logging frameworks – Apache Commons Logging Unless LogFactory.release() – Log4j - some configs Unless LogManager.shutdown() – java.util.logging custom Level • Bean Validation API (JSR 303) • Unified Expression Language (javax.el) Mattias Jiderhamn – java.jiderhamn.se

GC roots • Class loaded by system ClassLoader – static field in JDK classes (java.* etc) • Live thread – Stack – local vars, method params – java.lang.Thread instance • Object held as synchronization monitor • JNI references • JVM specials… Mattias Jiderhamn – java.jiderhamn.se

System classes • java.sql.DriverManager • Bean introspection cache, shutdown hooks, custom default Authenticator, custom security Provider, custom MBeans, custom ThreadGroup, custom property editor, … • Reference to contextClassLoader of first caller Mattias Jiderhamn – java.jiderhamn.se

DriverManager Mattias Jiderhamn – java.jiderhamn.se

DriverManager JRE Application Server DriverManager 1) … ClassLoader com.mysql.jdbc.Driver app.war mysql-jdbc.jar … or 2) DriverManager.deregisterDriver(…) Mattias Jiderhamn – java.jiderhamn.se

Context shutdown public class MyCleanupListener implements javax.servlet.ServletContextListener { ... /** Called when application is undeployed */ public void contextDestroyed( ServletContextEvent servletContextEvent) { DriverManager.deregisterDriver(…); } } Mattias Jiderhamn – java.jiderhamn.se

Threads • Thread stack – Local variables – Method parameters • MyThread extends Thread MyRunnable implements Runnable • contextClassLoader – Example HTTP 1.1 Keep-Alive-Timer Mattias Jiderhamn – java.jiderhamn.se

Context shutdown public class MyCleanupListener implements javax.servlet.ServletContextListener { ... /** Called when application is undeployed */ public void contextDestroyed( ServletContextEvent servletContextEvent) { DriverManager.deregisterDriver(…); // Stop threads here! } } Mattias Jiderhamn – java.jiderhamn.se

Stopping Threads public class MyThread extends Thread { public void run() { while(true) { // Bad idea! // Do something } } } Mattias Jiderhamn – java.jiderhamn.se

Stopping Threads public class MyThread extends Thread { private volatile boolean running = true; private boolean running = true; public void run() { while(running) { // Until stopped // Do something } } public void shutdown() { running = false; } Heinz Kabutz / Java Specialists’ - The Law of the Blind Spot } Mattias Jiderhamn – java.jiderhamn.se

Threads • Thread stack – Local variables – Method parameters • MyThread extends Thread MyRunnable implements Runnable • contextClassLoader – Example HTTP 1.1 Keep-Alive-Timer • ThreadLocal Mattias Jiderhamn – java.jiderhamn.se

ThreadLocal ThreadLocal WeakHashMap<Thread, ?> Thread Mattias Jiderhamn – java.jiderhamn.se Foo

ThreadLocal ThreadLocal JavaDoc ”Each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and the ThreadLocal instance is accessible; …” Mattias Jiderhamn – java.jiderhamn.se

ThreadLocal Thread put() ThreadLocalMap Entry ThreadLocal set() Mattias Jiderhamn – java.jiderhamn.se Foo

ThreadLocal(?) Pooled threads: • Threads may outlive ClassLoader • ThreadLocal → ThreadGlobal! Mattias Jiderhamn – java.jiderhamn.se

ThreadLocal Thread ThreadLocalMap Entry ThreadLocal Foo static Class ClassLoader Mattias Jiderhamn – java.jiderhamn.se Class

ThreadLocal Thread ThreadLocalMap Entry Stale entry ThreadLocal Foo ClassLoader Mattias Jiderhamn – java.jiderhamn.se Class

ThreadLocal ThreadLocalMap JavaDoc ”However, since reference queues are not used, stale entries are guaranteed to be removed only when the table starts running out of space” Mattias Jiderhamn – java.jiderhamn.se

ThreadLocal • Custom value (incl references) –static ThreadLocal = leak –otherwise = unpredicted GC:ing • Custom ThreadLocal = no leak Mattias Jiderhamn – java.jiderhamn.se

ThreadLocal Always clear your ThreadLocals! try { myThreadLocal.set(foo); … } finally { myThreadLocal.remove(); } Mattias Jiderhamn – java.jiderhamn.se

Known offenders Apache ActiveMQ Apache Axis Apache Batik Apache Commons Pool / DBCP Apache CXF AWT Toolkit Bean Validation API / JSR 303 CGLIB (Hibernate / Spring / JBoss / Apache Geronimo) dom4j EclipseLink GeoTools Google Guice Groovy GWT / javax.imageio Hessian iCal4J Infinispan IntrospectionUtils JarURLConnection Java Advanced Imaging (JAI) Javassist Java Cryptography Architecture (JCA) Java Server Faces 2 javax.security.auth.Policy/login.Configuration JGroups Logback JAXB Mozilla Rhino MVEL OpenOffice Java Uno RunTime (JURT) Oracle JDBC RMI Serialization (<= JDK 1.4.2) Spring framework Unified Expression Language / javax.el URLConnection + HTTP 1.1 XML parsing (DocumentBuilderFactory) Mattias Jiderhamn – java.jiderhamn.se

Leak prevention lib • Application server independent • Covers much more than Tomcat – – – – System class references avoided/cleared Threads are killed ThreadLocals are cleared Known offenders handled • Logs warnings • Apache 2 licensed – You may modify and redistribute Mattias Jiderhamn – java.jiderhamn.se

Leak prevention lib 0 runtime dependencies 1 class 2 XML snippets Mattias Jiderhamn – java.jiderhamn.se

Leak prevention lib pom.xml <dependency>   <groupId>se.jiderhamn</groupId>   <artifactId>classloader-leak-prevention</artifactId>   <version>1.9.2</version> </dependency> web.xml <listener>   <listener-class> se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventor   </listener-class> </listener> Mattias Jiderhamn – java.jiderhamn.se

Tomcat Cleanup Thread Thread remove() Unsafe! get() ThreadLocalMap set() remove() Removed in 6.0.27 Bugzilla #48895 Tomcat 7.0.6+ renews the thread pool whenever an application is redeployed. Disposable threads for lifecycle events. Bugzilla #49159 Mattias Jiderhamn – java.jiderhamn.se

Leak prevention lib Cleanup Thread Thread set(null) ThreadLocalMap Entry Stale ThreadLocal Mattias Jiderhamn – java.jiderhamn.se Foo

Bean Validation API Version 1.0.0.GA javax.validation.Validation Application Server validation-api-1.0.0.GA.jar app.war hibernate-validator.jar Mattias Jiderhamn – java.jiderhamn.se

javax.el API javax.el.BeanELResolver Mattias Jiderhamn – java.jiderhamn.se

OpenOffice JURT com.sun.star.lib.util.AsynchronousFinalizer Mattias Jiderhamn – java.jiderhamn.se

GC choice matters • Parallell GC seems to have problems collecting java.lang.Class instances • CMS needs extra parameter -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled • G1 -XX:+UseG1GC Mattias Jiderhamn – java.jiderhamn.se

Links http://java.jiderhamn.se • Leak prevention library incl sources (GitHub) • Step by step guide: acquire heap dump and analyze for leaks using Eclipse Memory Analyzer (MAT) • List of known offenders and links to bug reports • Submit your own reports and patches! Que? Join the fight! Mattias Jiderhamn – java.jiderhamn.se

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Classloader leaks: the dreaded "java.lang.OutOfMemoryError ...

Classloader leaks: the dreaded "java.lang.OutOfMemoryError: PermGen space" exception By fkieviet on Oct 16, 2006
Read more

Classloader leaks I – How to find classloader leaks with ...

I’m planning a series of posts around classloader leaks, also known as PermGen memory leaks. You have probably arrived at this page because your Java web ...
Read more

java - ClassLoader Leak - Are they worth solving? - Stack ...

ClassLoader leaks usually result in java.lang.OutOfMemoryError: PermGen. In the instance of working on application servers you may see this as a result of ...
Read more

Reloading Java Classes 201: How do ClassLoader leaks ...

Other Articles in the Reloading Java Classes Series. RJC101: Objects, Classes and ClassLoaders; RJC201: How do Classloader leaks happen? RJC301 ...
Read more

What is a PermGen leak? - Plumbr – Java Performance ...

What is a PermGen leak? ... Looking for an easy way to detect permgen leaks? ... Ensuring proper class loading process is the responsibility of a ClassLoader.
Read more

Classloader leaks and PermGen space · samaxes

« Back to home Classloader leaks and PermGen space Posted on 2007.10.02 · Tagged in java, jvm ...
Read more

Diagnosis of Java class loader memory leaks

We will learn basic concept of class and class load mechanism, generate class/classloader leaks and diagnose problems with a variety of artifacts such as ...
Read more

MemoryLeakProtection - Tomcat Wiki - FrontPage - General Wiki

ThreadLocal leaks. Classloader leaks because of uncleaned ThreadLocal variables are quite common. Depending on the use cases, they can be ...
Read more

classloader - Java Class Loader Leaks - Next Steps ...

I guess some reference might still be hanging on to some of the classes loaded by the class loader. I checked with the Memory Leak estimator provided by HP ...
Read more

ClassLoader leaks | new Blog("Mattias Jiderhamn")

Today I launch another weapon in the ongoing war on Classloader Leaks: The classloader-leak-test-framework. Admittedly, the framework itself is not new.
Read more