package org.apache.markt.leaks.rmi;

import java.lang.ref.WeakReference;
import java.net.URL;
import java.net.URLClassLoader;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class RegistryLeak {

    public static void main(String[] args) {

        RegistryLeak registryLeak = new RegistryLeak();

        // Switch TCCL
        registryLeak.start();

        // Create RMI registry
        registryLeak.register();

        // Clean-up registry
        registryLeak.deregister();

        // Restore TCCL
        registryLeak.stop();

        // Trigger GC
        System.gc();

        // Check for leak
        registryLeak.leakCheck();
    }

    private static final ClassLoader ORIGINAL_CLASS_LOADER =
            Thread.currentThread().getContextClassLoader();

    private WeakReference<ClassLoader> moduleClassLoaderRef;


    private void start() {
        ClassLoader moduleClassLoader = new URLClassLoader(new URL[] {}, ORIGINAL_CLASS_LOADER);

        Thread.currentThread().setContextClassLoader(moduleClassLoader);
        moduleClassLoaderRef = new WeakReference<>(moduleClassLoader);
    }


    private void register() {
        try {
            LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void deregister() {
        // NO-OP. NO API available.
    }


    private void stop() {
        Thread.currentThread().setContextClassLoader(ORIGINAL_CLASS_LOADER);
    }


    private void leakCheck() {
        if (moduleClassLoaderRef.get() == null) {
            System.out.println("No leak");
        } else {
            System.out.println("Leak");
        }
    }
}
@@ -562,24 +562,135 @@
 ak.stop();%0A%0A
+        // Check for leaks%0A        int count = 0;%0A        while (count %3C 10 && registryLeak.leakCheck()) %7B%0A    
         // T
@@ -699,16 +699,20 @@
 gger GC%0A
+    
         
@@ -728,34 +728,258 @@
 ();%0A
-%0A        // Check for leak
+            try %7B%0A                Thread.sleep(200);%0A            %7D catch (InterruptedException e) %7B%0A                e.printStackTrace();%0A            %7D%0A            count++;%0A        %7D%0A        System.out.println(%22There were %22 + count + %22 calls to GC%22);%0A
 %0A   
@@ -975,32 +975,36 @@
  GC%22);%0A%0A        
+if (
 registryLeak.lea
@@ -1011,17 +1011,129 @@
 kCheck()
-;
+) %7B%0A            System.out.println(%22Leak%22);%0A        %7D else %7B%0A            System.out.println(%22No leak%22);%0A        %7D
 %0A    %7D%0A%0A
@@ -1987,20 +1987,23 @@
 private 
-void
+boolean
  leakChe
@@ -2017,20 +2017,23 @@
         
-if (
+return 
 moduleCl
@@ -2055,128 +2055,16 @@
 t() 
-=
+!
 = null
-) %7B%0A            System.out.println(%22No leak%22);%0A        %7D else %7B%0A            System.out.println(%22Leak%22);%0A        %7D
+;
 %0A   

Make the leak check more robust.