Switch branches/tags
Find file History
Mattias Jiderhamn
Latest commit 590ab0b May 29, 2018

README.md

Classloader Leak Prevention library integration

This document is about using the Classloader Leak Prevention library in a non-servlet environment. For general information and use in servlet environments, please see the root README.md

Version 2.x of the Classloader Leak Prevention library has been refactored to allow for use outside a servlet environment, or by all means in a servlet container (Java EE application server).

Setting up

What you will want to do is first create a ClassLoaderLeakPreventorFactory instance, either by using the default constructor that will configure the system ClassLoader (ClassLoader.getSystemClassLoader()) to be used for pre-inits, or provide your own leak safe ClassLoader to the constructor.

Make any configurations on the factory instance, i.e. add or remove any cleanup or pre-init plugins, or change parameters of any of the default plugins.

Protect ClassLoader

Then for every ClassLoader that needs leak protection, create a new ClassLoaderLeakPreventor using

classLoaderLeakPreventor = classLoaderLeakPreventorFactory.newLeakPreventor(classLoader);

Before letting any code execute inside the ClassLoader (or at least as soon as possible), invoke

classLoaderLeakPreventor.runPreClassLoaderInitiators();

You can reuse the same ClassLoaderLeakPreventorFactory for multiple ClassLoaders, but please be aware that any configuration changes made to plugins of the factory will affect all ClassLoaderLeakPreventors created by the factory - both future and existing. If however you add or remove plugins, that will only affect new ClassLoaderLeakPreventors.

Shutting down

When you believe the ClassLoader should no longer be used, but be ready for Garbage Collection, invoke

classLoaderLeakPreventor.runCleanUps();

on the ClassLoaderLeakPreventor that corresponds to the ClassLoader.

Example

For an example how to use the framework, feel free to study the ClassLoaderLeakPreventorListener in the classloader-leak-prevention-servlet module.

Maven

The module is available in Maven as

<dependency>
  <groupId>se.jiderhamn.classloader-leak-prevention</groupId>
  <artifactId>classloader-leak-prevention-core</artifactId>
  <version>2.6.1</version>
</dependency>