ClassLoader Leak Prevention library

ServletContextListener that prevents ClassLoader leaks / java.lang.OutOfMemoryError: PermGen space

License

License

Categories

Categories

IDE Development Tools
GroupId

GroupId

se.jiderhamn
ArtifactId

ArtifactId

classloader-leak-prevention
Last Version

Last Version

1.15.4
Release Date

Release Date

Type

Type

jar
Description

Description

ClassLoader Leak Prevention library
ServletContextListener that prevents ClassLoader leaks / java.lang.OutOfMemoryError: PermGen space
Project URL

Project URL

https://github.com/mjiderhamn/classloader-leak-prevention
Source Code Management

Source Code Management

https://github.com/mjiderhamn/classloader-leak-prevention.git

Download classloader-leak-prevention

How to add to project

<!-- https://jarcasting.com/artifacts/se.jiderhamn/classloader-leak-prevention/ -->
<dependency>
    <groupId>se.jiderhamn</groupId>
    <artifactId>classloader-leak-prevention</artifactId>
    <version>1.15.4</version>
</dependency>
// https://jarcasting.com/artifacts/se.jiderhamn/classloader-leak-prevention/
implementation 'se.jiderhamn:classloader-leak-prevention:1.15.4'
// https://jarcasting.com/artifacts/se.jiderhamn/classloader-leak-prevention/
implementation ("se.jiderhamn:classloader-leak-prevention:1.15.4")
'se.jiderhamn:classloader-leak-prevention:jar:1.15.4'
<dependency org="se.jiderhamn" name="classloader-leak-prevention" rev="1.15.4">
  <artifact name="classloader-leak-prevention" type="jar" />
</dependency>
@Grapes(
@Grab(group='se.jiderhamn', module='classloader-leak-prevention', version='1.15.4')
)
libraryDependencies += "se.jiderhamn" % "classloader-leak-prevention" % "1.15.4"
[se.jiderhamn/classloader-leak-prevention "1.15.4"]

Dependencies

compile (1)

Group / Artifact Type Version
javax.servlet : servlet-api jar 2.5

test (14)

Group / Artifact Type Version
se.jiderhamn : classloader-leak-test-framework jar 1.0.2
junit : junit jar 4.12
commons-logging : commons-logging jar 1.1.3
javax.validation : validation-api jar 1.0.0.GA
org.hibernate : hibernate-validator jar 4.2.0.Final
org.apache.axis : axis jar 1.4
commons-discovery : commons-discovery jar 0.5
mysql : mysql-connector-java jar 5.1.18
javax.el : el-api jar 2.2.1-b04
org.apache.cxf : cxf-rt-transports-http jar 2.6.10
org.geotools » gt-metadata jar 2.6.2
org.apache.logging.log4j : log4j-api jar 2.3
org.apache.logging.log4j : log4j-core jar 2.3
com.lmax : disruptor jar 3.3.4

Project Modules

There are no modules declared in this project.

Classloader Leak Prevention library

Build Status Maven Central License

If you want to avoid the dreaded java.lang.OutOfMemoryError: Metaspace / PermGen space, just include this library into your Java EE application and it should take care of the rest!

To learn more about classloader leaks, their causes, types, ways to find them and known offenders, see blog series here: http://java.jiderhamn.se/category/classloader-leaks/

Servlet 3.0+

In a Servlet 3.0+ environment, all you need to do is include this Maven dependency in your .war:

<dependency>
  <groupId>se.jiderhamn.classloader-leak-prevention</groupId>
  <artifactId>classloader-leak-prevention-servlet3</artifactId>
  <version>2.7.0</version>
</dependency>

If you run into problems with the Servlet 3.0 module, try the Servlet 2.5 alternative below. Since the Servlet spec does not guarantee the order of ServletContainerInitializers, it means this library may not initialize first and clean up last in case you have other Servlet 3.0 dependencies, which could lead to unexpected behaviour.

Servlet 2.5 (and earlier)

For Servlet 2.5 (and earlier) environments, you need to use a different Maven dependency (notice the difference in artifactId):

<dependency>
  <groupId>se.jiderhamn.classloader-leak-prevention</groupId>
  <artifactId>classloader-leak-prevention-servlet</artifactId>
  <version>2.7.0</version>
</dependency>

You also have to add this to your web.xml:

<listener>
  <listener-class>se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventorListener</listener-class>
</listener>

Note that the name of the listener class has changed since 1.x!

It makes sense to keep this listener "outermost" (initializing first, destroying last), so you should normally declare it before any other listeners in web.xml.

Configuration

The context listener used in both cases has a number of settings that can be configured with context parameters in web.xml:

<context-param>
  <param-name>ClassLoaderLeakPreventor.stopThreads</param-name>
  <param-value>false</param-value>
</context-param>

The available settings are

Parameter name Default value Description
ClassLoaderLeakPreventor.stopThreads true Should threads tied to the web app classloader be forced to stop at application shutdown?
ClassLoaderLeakPreventor.stopTimerThreads true Should Timer threads tied to the web app classloader be forced to stop at application shutdown?
ClassLoaderLeakPreventor.executeShutdownHooks true Should shutdown hooks registered from the application be executed at application shutdown?
ClassLoaderLeakPreventor.startOracleTimeoutThread true Should the oracle.jdbc.driver.OracleTimeoutPollingThread thread be forced to start with system ClassLoader, in case Oracle JDBC driver is present? This is normally a good idea, but can be disabled in case the Oracle JDBC driver is not used even though it is on the classpath.
ClassLoaderLeakPreventor.threadWaitMs 5000
(5 seconds)
No of milliseconds to wait for threads to finish execution, before stopping them.
ClassLoaderLeakPreventor.shutdownHookWaitMs 10000
(10 seconds)
No of milliseconds to wait for shutdown hooks to finish execution, before stopping them. If set to -1 there will be no waiting at all, but Thread is allowed to run until finished.

Classloader leak detection / test framework

The test framework has its own Maven module and its own documentation, see classloader-leak-test-framework.

Integration

For non-servlet environments, please see the documentation for the core module.

License

This project is licensed under the Apache 2 license, which allows you to include modified versions of the code in your distributed software, without having to release your source code.

Versions

Version
1.15.4
1.15.3
1.15.2
1.15.1
1.15.0
1.14.1
1.14.0
1.13.0
1.12.0
1.11.0
1.10.0
1.9.3
1.9.2
1.9.1
1.9.0
1.8.1
1.8.0
1.7.1
1.7.0
1.6.0
1.5.2