JVM Classloader leak

Main.java-----------------------------------------public class Main {public static void main(String...args) throws Exception {List<Object> list = new ArrayList<>();loadClass(list);while (true) {System.gc();Thread.sleep(1000);}}private static void loadClass(List list) throws Exception {URL url = Main.class.getProtectionDomain().getCodeSource().getLocation();MyCustomClassLoader cl = new MyCustomClassLoader(url);Class<?> clazz = cl.loadClass("com.test.Foo");list.add(clazz.newInstance());cl = null;}}class MyCustomClassLoader extends URLClassLoader {public MyCustomClassLoader(URL... urls) {super(urls, null);}@Overrideprotected void finalize() {System.out.println("*** CustomClassLoader finalized!");}}Foo.java----------------------------------public class Foo {public Foo() {System.out.println("Test ClassLoader: " + this.getClass().getClassLoader());}@Overrideprotected void finalize() {System.out.println( this + " finalized!");   }}
public static void main(String...args) throws Exception {List<Object> list = new ArrayList<>();loadClass(list);while (true) {System.gc();Thread.sleep(1000);list = null;}}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Containers & Containerization

Make sure your data conversion SQL is easily identifiable/reversible

Comparing AWS and Google Cloud — An overview

Payrexx Release Notes April — 2022.04

dARCH 2021 — Event 0 — Round 1 Results

CS373 Spring 2021: Final Entry

Deny Launching EC2 Instances, If an instance type is not t2.medium

Announcing the FaunaDB Integration for ZEIT

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sumanta pakira

Sumanta pakira

More from Medium

zio-test and diffx integration

A required class was missing while executing org.wso2.maven:wso2-esb-api-plugin:2.1.0:pom-gen:

We release all apps together when the common lib is released. Don’t do it like us.

How to improve quality of the automated Java tests using Pitest