SlideShare a Scribd company logo
1 of 55
Mattias Jiderhamn – java.jiderhamn.se
Join the war on
Mattias Jiderhamn
ClassLoader leaks
Mattias Jiderhamn – java.jiderhamn.se
java.lang.OutOfMemoryError:
PermGen space
:-(
Metaspace
Mattias Jiderhamn – java.jiderhamn.se
Local dev env
Mattias Jiderhamn – java.jiderhamn.se
Continuous Deploy
.war
Crashing
Mattias Jiderhamn – java.jiderhamn.se
Agenda
What does it mean?
Why does it happen?
Where does it leak?
How to avoid?
OSS examples
Training!
Mattias Jiderhamn – java.jiderhamn.se
What?
Mattias Jiderhamn – java.jiderhamn.se
JVM memory
Stack
Heap
PermGen /
Metaspace
Mattias Jiderhamn – java.jiderhamn.se
JVM memory
Per thread
Local variables and
method parameters
Stack
Mattias Jiderhamn – java.jiderhamn.se
JVM memory
Heap
Young generation
Old generation /
tenured space
Eden
space
Survivor
space
Object instances
Mattias Jiderhamn – java.jiderhamn.se
JVM memory
Permanent Generation
java.lang.Class instances
etc
Named before JEE and
class unloading
Renamed Metaspace in Java 8
Aka Method area
PermGen /
Metaspace
Mattias Jiderhamn – java.jiderhamn.se
java.lang.OutOfMemoryError:
PermGen space / Metaspace
= Too many classes are loaded
What?
Mattias Jiderhamn – java.jiderhamn.se
Why?
Mattias Jiderhamn – java.jiderhamn.se
Reason for OOME
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
java.lang.OutOfMemoryError:
PermGen space / Metaspace
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
Foo
WeakHashMap
Map m = new WeakHashMap();
Foo myFoo = new Foo();
m.put(myFoo, ”bar”);
myFoo = null;
Mattias Jiderhamn – java.jiderhamn.se
GC reachability
GC roots
Mattias Jiderhamn – java.jiderhamn.se
Redeploy
Application Server
ClassLoader
app.war
ClassLoader
app.war’
ClassLoader
app.war’’
Mattias Jiderhamn – java.jiderhamn.se
ClassLoader refs
ClassLoader
Class ClassClass
Instance
Mattias Jiderhamn – java.jiderhamn.se
How leaks happen
Application Server
ClassLoader
Class ClassClass
Instance
GC root
Mattias Jiderhamn – java.jiderhamn.se
Where?
Mattias Jiderhamn – java.jiderhamn.se
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
Application Server
app.war
mysql-jdbc.jar
JRE
DriverManager
ClassLoader
com.mysql.jdbc.Driver
1) …
… 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 +
AccessControlContext inherited
– 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 boolean running = true;
public void run() {
while(running) { // Until stopped
// Do something
}
}
public void shutdown() {
running = false;
}
}
private volatile boolean running = true;
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 +
AccessControlContext inherited
– Example HTTP 1.1 Keep-Alive-Timer
• ThreadLocal
Mattias Jiderhamn – java.jiderhamn.se
ThreadLocal
WeakHashMap<Thread, ?>
ThreadLocal
Thread Foo
Mattias Jiderhamn – java.jiderhamn.se
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
ThreadLocalMap
Entry
Thread
ThreadLocal Foo
put()
set()
Mattias Jiderhamn – java.jiderhamn.se
ThreadLocal
Pooled threads:
•Threads may outlive ClassLoader
•ThreadLocal → ThreadGlobal!
(?)
Mattias Jiderhamn – java.jiderhamn.se
ThreadLocal
ThreadLocalMap
Entry
Thread
ThreadLocal Foo
ClassClassLoader
static
Class
Mattias Jiderhamn – java.jiderhamn.se
ThreadLocal
ThreadLocalMap
Entry
Thread
ThreadLocal Foo
ClassClassLoader
Stale entry
Mattias Jiderhamn – java.jiderhamn.se
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
•Custom ThreadLocal = no leak
Mattias Jiderhamn – java.jiderhamn.se
ThreadLocal
try {
myThreadLocal.set(foo);
…
}
finally {
myThreadLocal.remove();
}
Always clear your ThreadLocals!
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
How?
Mattias Jiderhamn – java.jiderhamn.se
Leak Prevention Lib
• Application server independent
– Version 2 core module is pure Java SE!
• Covers a lot more than Tomcat
– System class references avoided/cleared
– Threads are stopped
– ThreadLocals are cleared
– Known offenders handled
• Logs warnings
• Apache 2 licensed
– You may modify and redistribute
Mattias Jiderhamn – java.jiderhamn.se
Leak Prevention Lib
Zero runtime dependencies
No required config
Maven pom.xml (Servlet 3.0+):
<dependency>
<groupId>se.jiderhamn.classloader-leak-prevention</groupId>
<artifactId>classloader-leak-prevention-servlet3</artifactId>
<version>2.0.2</version>
</dependency>
Mattias Jiderhamn – java.jiderhamn.se
Tomcat
Bugzilla #48895
ThreadLocalMap
Thread
Cleanup
Thread
remove()
Unsafe!
get()
set()
remove()
Removed in 6.0.27
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
set(null) ThreadLocalMap
Entry
Thread
ThreadLocal Foo
Stale
Cleanup
Thread
Mattias Jiderhamn – java.jiderhamn.se
Open Source
examples
Mattias Jiderhamn – java.jiderhamn.se
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
Apache CXF
org.apache.cxf.transport.http.CXFAuthenticator
CXF-5442
Mattias Jiderhamn – java.jiderhamn.se
OpenOffice JURT
com.sun.star.lib.util.AsynchronousFinalizer
Mattias Jiderhamn – java.jiderhamn.se
Training!
Gear up!
Mattias Jiderhamn – java.jiderhamn.se
Free tool
Eclipse Memory Analyzer
aka MAT
eclipse.org/mat
Mattias Jiderhamn – java.jiderhamn.se
Heap dump
Run application server with
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/temp
Mattias Jiderhamn – java.jiderhamn.se
Links
Join the fight!
java.jiderhamn.se @mjiderhamn
github.com/mjiderhamn
• ClassLoader Leak Prevention Library
• 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 reports and pull requests!
???

More Related Content

What's hot

Clean Pragmatic Architecture - Avoiding a Monolith
Clean Pragmatic Architecture - Avoiding a MonolithClean Pragmatic Architecture - Avoiding a Monolith
Clean Pragmatic Architecture - Avoiding a MonolithVictor Rentea
 
クラスローダーについて
クラスローダーについてクラスローダーについて
クラスローダーについてSuguru ARAKAWA
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooksMaulik Shah
 
EventListener使いこなし術 - Symfony勉強会#10
EventListener使いこなし術 - Symfony勉強会#10EventListener使いこなし術 - Symfony勉強会#10
EventListener使いこなし術 - Symfony勉強会#10Yuichi Okada
 
Laravel × レイヤードアーキテクチャを実践して得られた知見と反省 / Practice of Laravel with layered archi...
Laravel × レイヤードアーキテクチャを実践して得られた知見と反省 / Practice of Laravel with layered archi...Laravel × レイヤードアーキテクチャを実践して得られた知見と反省 / Practice of Laravel with layered archi...
Laravel × レイヤードアーキテクチャを実践して得られた知見と反省 / Practice of Laravel with layered archi...Shohei Okada
 
知らないと損するアプリ開発におけるStateMachineの活用法(full版)
知らないと損するアプリ開発におけるStateMachineの活用法(full版)知らないと損するアプリ開発におけるStateMachineの活用法(full版)
知らないと損するアプリ開発におけるStateMachineの活用法(full版)Ken Morishita
 
Hexagonal architecture - message-oriented software design
Hexagonal architecture  - message-oriented software designHexagonal architecture  - message-oriented software design
Hexagonal architecture - message-oriented software designMatthias Noback
 
並行処理初心者のためのAkka入門
並行処理初心者のためのAkka入門並行処理初心者のためのAkka入門
並行処理初心者のためのAkka入門Yoshimura Soichiro
 
Clean Architecture Essentials - Stockholm Software Craftsmanship
Clean Architecture Essentials - Stockholm Software CraftsmanshipClean Architecture Essentials - Stockholm Software Craftsmanship
Clean Architecture Essentials - Stockholm Software CraftsmanshipIvan Paulovich
 
Reactive programming
Reactive programmingReactive programming
Reactive programmingSUDIP GHOSH
 
はじめての Go 言語のプロジェクトを AWS Lambda + API Gateway でやったのでパッケージ構成を晒すよ
はじめての Go 言語のプロジェクトを AWS Lambda + API Gateway でやったのでパッケージ構成を晒すよはじめての Go 言語のプロジェクトを AWS Lambda + API Gateway でやったのでパッケージ構成を晒すよ
はじめての Go 言語のプロジェクトを AWS Lambda + API Gateway でやったのでパッケージ構成を晒すよShohei Okada
 
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023Steve Pember
 
CQRS + Event Sourcing
CQRS + Event SourcingCQRS + Event Sourcing
CQRS + Event SourcingMike Bild
 
Microservices Interview Questions and Answers | Microservices Architecture Tr...
Microservices Interview Questions and Answers | Microservices Architecture Tr...Microservices Interview Questions and Answers | Microservices Architecture Tr...
Microservices Interview Questions and Answers | Microservices Architecture Tr...Edureka!
 
RxJava - introduction & design
RxJava - introduction & designRxJava - introduction & design
RxJava - introduction & designallegro.tech
 
Introduction to Resilience4j
Introduction to Resilience4jIntroduction to Resilience4j
Introduction to Resilience4jKnoldus Inc.
 

What's hot (20)

Clean Pragmatic Architecture - Avoiding a Monolith
Clean Pragmatic Architecture - Avoiding a MonolithClean Pragmatic Architecture - Avoiding a Monolith
Clean Pragmatic Architecture - Avoiding a Monolith
 
クラスローダーについて
クラスローダーについてクラスローダーについて
クラスローダーについて
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
EventListener使いこなし術 - Symfony勉強会#10
EventListener使いこなし術 - Symfony勉強会#10EventListener使いこなし術 - Symfony勉強会#10
EventListener使いこなし術 - Symfony勉強会#10
 
Laravel × レイヤードアーキテクチャを実践して得られた知見と反省 / Practice of Laravel with layered archi...
Laravel × レイヤードアーキテクチャを実践して得られた知見と反省 / Practice of Laravel with layered archi...Laravel × レイヤードアーキテクチャを実践して得られた知見と反省 / Practice of Laravel with layered archi...
Laravel × レイヤードアーキテクチャを実践して得られた知見と反省 / Practice of Laravel with layered archi...
 
知らないと損するアプリ開発におけるStateMachineの活用法(full版)
知らないと損するアプリ開発におけるStateMachineの活用法(full版)知らないと損するアプリ開発におけるStateMachineの活用法(full版)
知らないと損するアプリ開発におけるStateMachineの活用法(full版)
 
Hexagonal architecture - message-oriented software design
Hexagonal architecture  - message-oriented software designHexagonal architecture  - message-oriented software design
Hexagonal architecture - message-oriented software design
 
並行処理初心者のためのAkka入門
並行処理初心者のためのAkka入門並行処理初心者のためのAkka入門
並行処理初心者のためのAkka入門
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
 
Clean Architecture Essentials - Stockholm Software Craftsmanship
Clean Architecture Essentials - Stockholm Software CraftsmanshipClean Architecture Essentials - Stockholm Software Craftsmanship
Clean Architecture Essentials - Stockholm Software Craftsmanship
 
Reactive programming
Reactive programmingReactive programming
Reactive programming
 
はじめての Go 言語のプロジェクトを AWS Lambda + API Gateway でやったのでパッケージ構成を晒すよ
はじめての Go 言語のプロジェクトを AWS Lambda + API Gateway でやったのでパッケージ構成を晒すよはじめての Go 言語のプロジェクトを AWS Lambda + API Gateway でやったのでパッケージ構成を晒すよ
はじめての Go 言語のプロジェクトを AWS Lambda + API Gateway でやったのでパッケージ構成を晒すよ
 
Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
 
Design Patterns (Examples in .NET)
Design Patterns (Examples in .NET)Design Patterns (Examples in .NET)
Design Patterns (Examples in .NET)
 
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
Anatomy of a Spring Boot App with Clean Architecture - Spring I/O 2023
 
CQRS + Event Sourcing
CQRS + Event SourcingCQRS + Event Sourcing
CQRS + Event Sourcing
 
Microservices Interview Questions and Answers | Microservices Architecture Tr...
Microservices Interview Questions and Answers | Microservices Architecture Tr...Microservices Interview Questions and Answers | Microservices Architecture Tr...
Microservices Interview Questions and Answers | Microservices Architecture Tr...
 
RxJava - introduction & design
RxJava - introduction & designRxJava - introduction & design
RxJava - introduction & design
 
Introduction to Resilience4j
Introduction to Resilience4jIntroduction to Resilience4j
Introduction to Resilience4j
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 

Viewers also liked

Memory Management: What You Need to Know When Moving to Java 8
Memory Management: What You Need to Know When Moving to Java 8Memory Management: What You Need to Know When Moving to Java 8
Memory Management: What You Need to Know When Moving to Java 8AppDynamics
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVMRafael Winterhalter
 
Introduktion till Aspekt-orienterad programmering (AOP) med AspectJ
Introduktion till Aspekt-orienterad programmering (AOP) med AspectJIntroduktion till Aspekt-orienterad programmering (AOP) med AspectJ
Introduktion till Aspekt-orienterad programmering (AOP) med AspectJMattias Jiderhamn
 
Java 8 Launch - MetaSpaces
Java 8 Launch - MetaSpacesJava 8 Launch - MetaSpaces
Java 8 Launch - MetaSpacesHaim Yadid
 
mjprof: Monadic approach for JVM profiling
mjprof: Monadic approach for JVM profilingmjprof: Monadic approach for JVM profiling
mjprof: Monadic approach for JVM profilingHaim Yadid
 
GIT out of Dependency Hell - git submodules
GIT out of Dependency Hell - git submodulesGIT out of Dependency Hell - git submodules
GIT out of Dependency Hell - git submodulesCarsten Nielsen
 
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia "What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia Vladimir Ivanov
 
Coding for Android on steroids with Kotlin
Coding for Android on steroids with KotlinCoding for Android on steroids with Kotlin
Coding for Android on steroids with KotlinKai Koenig
 
Taking Kotlin to production, Seriously
Taking Kotlin to production, SeriouslyTaking Kotlin to production, Seriously
Taking Kotlin to production, SeriouslyHaim Yadid
 
The Future of Futures - A Talk About Java 8 CompletableFutures
The Future of Futures - A Talk About Java 8 CompletableFuturesThe Future of Futures - A Talk About Java 8 CompletableFutures
The Future of Futures - A Talk About Java 8 CompletableFuturesHaim Yadid
 
Classloader leak detection in websphere application server
Classloader leak detection in websphere application serverClassloader leak detection in websphere application server
Classloader leak detection in websphere application serverRohit Kelapure
 
No one puts java in the container
No one puts java in the containerNo one puts java in the container
No one puts java in the containerkensipe
 
Deploy and Destroy Complete Test Environments
Deploy and Destroy Complete Test EnvironmentsDeploy and Destroy Complete Test Environments
Deploy and Destroy Complete Test EnvironmentsBas Dijkstra
 
Testing RESTful web services with REST Assured
Testing RESTful web services with REST AssuredTesting RESTful web services with REST Assured
Testing RESTful web services with REST AssuredBas Dijkstra
 
Ruby CI with Jenkins
Ruby CI with JenkinsRuby CI with Jenkins
Ruby CI with Jenkinscowboyd
 
新版 OutOfMemoryErrorを知る
新版 OutOfMemoryErrorを知る新版 OutOfMemoryErrorを知る
新版 OutOfMemoryErrorを知るMasahiro Hidaka
 

Viewers also liked (20)

Memory Management: What You Need to Know When Moving to Java 8
Memory Management: What You Need to Know When Moving to Java 8Memory Management: What You Need to Know When Moving to Java 8
Memory Management: What You Need to Know When Moving to Java 8
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVM
 
Introduktion till Aspekt-orienterad programmering (AOP) med AspectJ
Introduktion till Aspekt-orienterad programmering (AOP) med AspectJIntroduktion till Aspekt-orienterad programmering (AOP) med AspectJ
Introduktion till Aspekt-orienterad programmering (AOP) med AspectJ
 
Java 8 Launch - MetaSpaces
Java 8 Launch - MetaSpacesJava 8 Launch - MetaSpaces
Java 8 Launch - MetaSpaces
 
Tools for Metaspace
Tools for MetaspaceTools for Metaspace
Tools for Metaspace
 
The Java Memory Model
The Java Memory ModelThe Java Memory Model
The Java Memory Model
 
mjprof: Monadic approach for JVM profiling
mjprof: Monadic approach for JVM profilingmjprof: Monadic approach for JVM profiling
mjprof: Monadic approach for JVM profiling
 
GIT out of Dependency Hell - git submodules
GIT out of Dependency Hell - git submodulesGIT out of Dependency Hell - git submodules
GIT out of Dependency Hell - git submodules
 
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia "What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
 
Coding for Android on steroids with Kotlin
Coding for Android on steroids with KotlinCoding for Android on steroids with Kotlin
Coding for Android on steroids with Kotlin
 
Taking Kotlin to production, Seriously
Taking Kotlin to production, SeriouslyTaking Kotlin to production, Seriously
Taking Kotlin to production, Seriously
 
The Future of Futures - A Talk About Java 8 CompletableFutures
The Future of Futures - A Talk About Java 8 CompletableFuturesThe Future of Futures - A Talk About Java 8 CompletableFutures
The Future of Futures - A Talk About Java 8 CompletableFutures
 
Classloader leak detection in websphere application server
Classloader leak detection in websphere application serverClassloader leak detection in websphere application server
Classloader leak detection in websphere application server
 
No one puts java in the container
No one puts java in the containerNo one puts java in the container
No one puts java in the container
 
Java GC, Off-heap workshop
Java GC, Off-heap workshopJava GC, Off-heap workshop
Java GC, Off-heap workshop
 
Deploy and Destroy Complete Test Environments
Deploy and Destroy Complete Test EnvironmentsDeploy and Destroy Complete Test Environments
Deploy and Destroy Complete Test Environments
 
Testing RESTful web services with REST Assured
Testing RESTful web services with REST AssuredTesting RESTful web services with REST Assured
Testing RESTful web services with REST Assured
 
JVM Internals (2015)
JVM Internals (2015)JVM Internals (2015)
JVM Internals (2015)
 
Ruby CI with Jenkins
Ruby CI with JenkinsRuby CI with Jenkins
Ruby CI with Jenkins
 
新版 OutOfMemoryErrorを知る
新版 OutOfMemoryErrorを知る新版 OutOfMemoryErrorを知る
新版 OutOfMemoryErrorを知る
 

Similar to ClassLoader Leaks

Владимир Иванов. Java 8 и JVM: что нового в HotSpot
Владимир Иванов. Java 8 и JVM: что нового в HotSpotВладимир Иванов. Java 8 и JVM: что нового в HotSpot
Владимир Иванов. Java 8 и JVM: что нового в HotSpotVolha Banadyseva
 
Don't dump thread dumps
Don't dump thread dumpsDon't dump thread dumps
Don't dump thread dumpsTier1 App
 
Web Sphere Problem Determination Ext
Web Sphere Problem Determination ExtWeb Sphere Problem Determination Ext
Web Sphere Problem Determination ExtRohit Kelapure
 
An Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and RuntimeAn Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and RuntimeOmar Bashir
 
Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)
Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)
Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)Ontico
 
java-monitoring-troubleshooting
java-monitoring-troubleshootingjava-monitoring-troubleshooting
java-monitoring-troubleshootingWilliam Au
 
Don't dump thread dumps
Don't dump thread dumpsDon't dump thread dumps
Don't dump thread dumpsTier1app
 
Java on Linux for devs and ops
Java on Linux for devs and opsJava on Linux for devs and ops
Java on Linux for devs and opsaragozin
 
White and Black Magic on the JVM
White and Black Magic on the JVMWhite and Black Magic on the JVM
White and Black Magic on the JVMIvaylo Pashov
 
JDK not so hidden treasures
JDK not so hidden treasuresJDK not so hidden treasures
JDK not so hidden treasuresAndrzej Grzesik
 
Extending Spark With Java Agent (handout)
Extending Spark With Java Agent (handout)Extending Spark With Java Agent (handout)
Extending Spark With Java Agent (handout)Jaroslav Bachorik
 
Spark Summit EU talk by Jaroslav Bachorik and Adrian Popescu
Spark Summit EU talk by Jaroslav Bachorik and Adrian PopescuSpark Summit EU talk by Jaroslav Bachorik and Adrian Popescu
Spark Summit EU talk by Jaroslav Bachorik and Adrian PopescuSpark Summit
 
Object Oriented Programming-JAVA
Object Oriented Programming-JAVAObject Oriented Programming-JAVA
Object Oriented Programming-JAVAHome
 

Similar to ClassLoader Leaks (20)

Владимир Иванов. Java 8 и JVM: что нового в HotSpot
Владимир Иванов. Java 8 и JVM: что нового в HotSpotВладимир Иванов. Java 8 и JVM: что нового в HotSpot
Владимир Иванов. Java 8 и JVM: что нового в HotSpot
 
Don't dump thread dumps
Don't dump thread dumpsDon't dump thread dumps
Don't dump thread dumps
 
Web Sphere Problem Determination Ext
Web Sphere Problem Determination ExtWeb Sphere Problem Determination Ext
Web Sphere Problem Determination Ext
 
An Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and RuntimeAn Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and Runtime
 
Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)
Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)
Java и Linux — особенности эксплуатации / Алексей Рагозин (Дойче Банк)
 
Java JVM
Java JVMJava JVM
Java JVM
 
java-monitoring-troubleshooting
java-monitoring-troubleshootingjava-monitoring-troubleshooting
java-monitoring-troubleshooting
 
Diving into Java Class Loader
Diving into Java Class LoaderDiving into Java Class Loader
Diving into Java Class Loader
 
Eureka moment
Eureka momentEureka moment
Eureka moment
 
Don't dump thread dumps
Don't dump thread dumpsDon't dump thread dumps
Don't dump thread dumps
 
Java on Linux for devs and ops
Java on Linux for devs and opsJava on Linux for devs and ops
Java on Linux for devs and ops
 
Eureka moment
Eureka momentEureka moment
Eureka moment
 
White and Black Magic on the JVM
White and Black Magic on the JVMWhite and Black Magic on the JVM
White and Black Magic on the JVM
 
Leak, lock and a long pause
Leak, lock and a long pauseLeak, lock and a long pause
Leak, lock and a long pause
 
JDK not so hidden treasures
JDK not so hidden treasuresJDK not so hidden treasures
JDK not so hidden treasures
 
Extending Spark With Java Agent (handout)
Extending Spark With Java Agent (handout)Extending Spark With Java Agent (handout)
Extending Spark With Java Agent (handout)
 
Spark Summit EU talk by Jaroslav Bachorik and Adrian Popescu
Spark Summit EU talk by Jaroslav Bachorik and Adrian PopescuSpark Summit EU talk by Jaroslav Bachorik and Adrian Popescu
Spark Summit EU talk by Jaroslav Bachorik and Adrian Popescu
 
Object Oriented Programming-JAVA
Object Oriented Programming-JAVAObject Oriented Programming-JAVA
Object Oriented Programming-JAVA
 
Temadag om-java-jamaica vm-2013-09
Temadag om-java-jamaica vm-2013-09Temadag om-java-jamaica vm-2013-09
Temadag om-java-jamaica vm-2013-09
 
Spring data requery
Spring data requerySpring data requery
Spring data requery
 

Recently uploaded

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 

Recently uploaded (20)

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 

ClassLoader Leaks

  • 1. Mattias Jiderhamn – java.jiderhamn.se Join the war on Mattias Jiderhamn ClassLoader leaks
  • 2. Mattias Jiderhamn – java.jiderhamn.se java.lang.OutOfMemoryError: PermGen space :-( Metaspace
  • 3. Mattias Jiderhamn – java.jiderhamn.se Local dev env
  • 4. Mattias Jiderhamn – java.jiderhamn.se Continuous Deploy .war Crashing
  • 5. Mattias Jiderhamn – java.jiderhamn.se Agenda What does it mean? Why does it happen? Where does it leak? How to avoid? OSS examples Training!
  • 6. Mattias Jiderhamn – java.jiderhamn.se What?
  • 7. Mattias Jiderhamn – java.jiderhamn.se JVM memory Stack Heap PermGen / Metaspace
  • 8. Mattias Jiderhamn – java.jiderhamn.se JVM memory Per thread Local variables and method parameters Stack
  • 9. Mattias Jiderhamn – java.jiderhamn.se JVM memory Heap Young generation Old generation / tenured space Eden space Survivor space Object instances
  • 10. Mattias Jiderhamn – java.jiderhamn.se JVM memory Permanent Generation java.lang.Class instances etc Named before JEE and class unloading Renamed Metaspace in Java 8 Aka Method area PermGen / Metaspace
  • 11. Mattias Jiderhamn – java.jiderhamn.se java.lang.OutOfMemoryError: PermGen space / Metaspace = Too many classes are loaded What?
  • 12. Mattias Jiderhamn – java.jiderhamn.se Why?
  • 13. Mattias Jiderhamn – java.jiderhamn.se Reason for OOME 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 java.lang.OutOfMemoryError: PermGen space / Metaspace
  • 14. 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
  • 15. Mattias Jiderhamn – java.jiderhamn.se Example Foo WeakHashMap Map m = new WeakHashMap(); Foo myFoo = new Foo(); m.put(myFoo, ”bar”); myFoo = null;
  • 16. Mattias Jiderhamn – java.jiderhamn.se GC reachability GC roots
  • 17. Mattias Jiderhamn – java.jiderhamn.se Redeploy Application Server ClassLoader app.war ClassLoader app.war’ ClassLoader app.war’’
  • 18. Mattias Jiderhamn – java.jiderhamn.se ClassLoader refs ClassLoader Class ClassClass Instance
  • 19. Mattias Jiderhamn – java.jiderhamn.se How leaks happen Application Server ClassLoader Class ClassClass Instance GC root
  • 20. Mattias Jiderhamn – java.jiderhamn.se Where?
  • 21. Mattias Jiderhamn – java.jiderhamn.se 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) ?
  • 22. 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…
  • 23. 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
  • 24. Mattias Jiderhamn – java.jiderhamn.se DriverManager
  • 25. Mattias Jiderhamn – java.jiderhamn.se DriverManager Application Server app.war mysql-jdbc.jar JRE DriverManager ClassLoader com.mysql.jdbc.Driver 1) … … or 2) DriverManager.deregisterDriver(…)
  • 26. 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(…); } }
  • 27. Mattias Jiderhamn – java.jiderhamn.se Threads • Thread stack – Local variables – Method parameters • MyThread extends Thread MyRunnable implements Runnable • contextClassLoader + AccessControlContext inherited – Example HTTP 1.1 Keep-Alive-Timer
  • 28. 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! } }
  • 29. Mattias Jiderhamn – java.jiderhamn.se Stopping threads public class MyThread extends Thread { public void run() { while(true) { // Bad idea! // Do something } } }
  • 30. Mattias Jiderhamn – java.jiderhamn.se Stopping threads public class MyThread extends Thread { private boolean running = true; public void run() { while(running) { // Until stopped // Do something } } public void shutdown() { running = false; } } private volatile boolean running = true; Heinz Kabutz / Java Specialists’ - The Law of the Blind Spot
  • 31. Mattias Jiderhamn – java.jiderhamn.se Threads • Thread stack – Local variables – Method parameters • MyThread extends Thread MyRunnable implements Runnable • contextClassLoader + AccessControlContext inherited – Example HTTP 1.1 Keep-Alive-Timer • ThreadLocal
  • 32. Mattias Jiderhamn – java.jiderhamn.se ThreadLocal WeakHashMap<Thread, ?> ThreadLocal Thread Foo
  • 33. Mattias Jiderhamn – java.jiderhamn.se 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; …”
  • 34. Mattias Jiderhamn – java.jiderhamn.se ThreadLocal ThreadLocalMap Entry Thread ThreadLocal Foo put() set()
  • 35. Mattias Jiderhamn – java.jiderhamn.se ThreadLocal Pooled threads: •Threads may outlive ClassLoader •ThreadLocal → ThreadGlobal! (?)
  • 36. Mattias Jiderhamn – java.jiderhamn.se ThreadLocal ThreadLocalMap Entry Thread ThreadLocal Foo ClassClassLoader static Class
  • 37. Mattias Jiderhamn – java.jiderhamn.se ThreadLocal ThreadLocalMap Entry Thread ThreadLocal Foo ClassClassLoader Stale entry
  • 38. Mattias Jiderhamn – java.jiderhamn.se 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”
  • 39. Mattias Jiderhamn – java.jiderhamn.se ThreadLocal •Custom value (incl references) –static ThreadLocal = leak –otherwise = unpredicted GC •Custom ThreadLocal = no leak
  • 40. Mattias Jiderhamn – java.jiderhamn.se ThreadLocal try { myThreadLocal.set(foo); … } finally { myThreadLocal.remove(); } Always clear your ThreadLocals!
  • 41. 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)
  • 42. Mattias Jiderhamn – java.jiderhamn.se How?
  • 43. Mattias Jiderhamn – java.jiderhamn.se Leak Prevention Lib • Application server independent – Version 2 core module is pure Java SE! • Covers a lot more than Tomcat – System class references avoided/cleared – Threads are stopped – ThreadLocals are cleared – Known offenders handled • Logs warnings • Apache 2 licensed – You may modify and redistribute
  • 44. Mattias Jiderhamn – java.jiderhamn.se Leak Prevention Lib Zero runtime dependencies No required config Maven pom.xml (Servlet 3.0+): <dependency> <groupId>se.jiderhamn.classloader-leak-prevention</groupId> <artifactId>classloader-leak-prevention-servlet3</artifactId> <version>2.0.2</version> </dependency>
  • 45. Mattias Jiderhamn – java.jiderhamn.se Tomcat Bugzilla #48895 ThreadLocalMap Thread Cleanup Thread remove() Unsafe! get() set() remove() Removed in 6.0.27 Tomcat 7.0.6+ renews the thread pool whenever an application is redeployed. Disposable threads for lifecycle events. Bugzilla #49159
  • 46. Mattias Jiderhamn – java.jiderhamn.se Leak Prevention Lib set(null) ThreadLocalMap Entry Thread ThreadLocal Foo Stale Cleanup Thread
  • 47. Mattias Jiderhamn – java.jiderhamn.se Open Source examples
  • 48. Mattias Jiderhamn – java.jiderhamn.se 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
  • 49. Mattias Jiderhamn – java.jiderhamn.se javax.el API javax.el.BeanELResolver
  • 50. Mattias Jiderhamn – java.jiderhamn.se Apache CXF org.apache.cxf.transport.http.CXFAuthenticator CXF-5442
  • 51. Mattias Jiderhamn – java.jiderhamn.se OpenOffice JURT com.sun.star.lib.util.AsynchronousFinalizer
  • 52. Mattias Jiderhamn – java.jiderhamn.se Training! Gear up!
  • 53. Mattias Jiderhamn – java.jiderhamn.se Free tool Eclipse Memory Analyzer aka MAT eclipse.org/mat
  • 54. Mattias Jiderhamn – java.jiderhamn.se Heap dump Run application server with -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/temp
  • 55. Mattias Jiderhamn – java.jiderhamn.se Links Join the fight! java.jiderhamn.se @mjiderhamn github.com/mjiderhamn • ClassLoader Leak Prevention Library • 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 reports and pull requests! ???

Editor's Notes

  1. No serious amount of web development Tomcat + luck
  2. Waste of time restarting AS
  3. http://commons.wikimedia.org/wiki/File:JBoss_logo.svg obstacle
  4. https://blogs.oracle.com/jonthecollector/entry/presenting_the_permanent_generation http://docs.oracle.com/javase/6/docs/technotes/guides/management/jconsole.html
  5. https://blogs.oracle.com/jonthecollector/entry/presenting_the_permanent_generation http://docs.oracle.com/javase/6/docs/technotes/guides/management/jconsole.html
  6. https://blogs.oracle.com/jonthecollector/entry/presenting_the_permanent_generation http://docs.oracle.com/javase/6/docs/technotes/guides/management/jconsole.html http://www.slideshare.net/waterfox1/an-introduction-to-jvm-internals-and-garbage-collection-in-java good picuture at slide 12
  7. https://blogs.oracle.com/jonthecollector/entry/presenting_the_permanent_generation http://docs.oracle.com/javase/6/docs/technotes/guides/management/jconsole.html http://www.slideshare.net/waterfox1/an-introduction-to-jvm-internals-and-garbage-collection-in-java
  8. Default 64 MB http://java.dzone.com/articles/java-8-permgen-metaspace http://www.infoq.com/news/2013/03/java-8-permgen-metaspace
  9. ”All soft references to softly-reachable objects are guaranteed to have been cleared before the virtual machine throws an OutOfMemoryError.”
  10. Inspired by http://www.slideshare.net/waterfox1/an-introduction-to-jvm-internals-and-garbage-collection-in-java slide 12
  11. http://www.slideshare.net/waterfox1/an-introduction-to-jvm-internals-and-garbage-collection-in-java slide 12
  12. http://www.slideshare.net/waterfox1/an-introduction-to-jvm-internals-and-garbage-collection-in-java slide 12
  13. Tomcat, Jetty, Resin, possibly GlashFish Bean Validation API
  14. GC roots: http://www.yourkit.com/docs/java/help/gc_roots.jsp http://javabook.compuware.com/content/memory/how-garbage-collection-works.aspx ÄNDRA: THREAD + underrubriker
  15. java.lang.Runtime.getRuntime().addShutdownHook()
  16. Runnable
  17. ”The Java Memory Model allows each thread to keep a local copy of fields.”
  18. 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; after a thread goes away, all of its copies of thread-local instances are subject to garbage collection (unless other references to these copies exist).
  19. http://wiki.apache.org/tomcat/MemoryLeakProtection#customThreadLocal
  20. http://wiki.apache.org/tomcat/MemoryLeakProtection#customThreadLocal
  21. http://wiki.apache.org/tomcat/MemoryLeakProtection#customThreadLocal
  22. http://wiki.apache.org/tomcat/MemoryLeakProtection#customThreadLocal
  23. ThreadLocalMap JavaDoc
  24. http://wiki.apache.org/tomcat/MemoryLeakProtection#customThreadLocal
  25. http://wiki.apache.org/tomcat/MemoryLeakProtection#customThreadLocal
  26. (except Servlet API…) (to simplify override by inheritance)
  27. Unsafe: get(), set(), remove() leads to inconsistent state https://issues.apache.org/bugzilla/show_bug.cgi?id=48895 https://issues.apache.org/bugzilla/show_bug.cgi?id=49159
  28. Tried filter first Tomcat 7 has ”filter”(?)
  29. Fixed in 1.1
  30. Independent of EL implementation Fixed in SVN, still wrong in 2.2.1-b04
  31. GC roots: http://www.yourkit.com/docs/java/help/gc_roots.jsp http://javabook.compuware.com/content/memory/how-garbage-collection-works.aspx
  32. http://commons.wikimedia.org/wiki/File:Duke_Wave.png