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;}}

--

--

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