Skip to content

Commit

Permalink
Removed clearReferencesRmiTargets
Browse files Browse the repository at this point in the history
- This leak was already fixed in JDK9
- see https://bugs.openjdk.org/browse/JDK-8157570

Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Feb 27, 2023
1 parent 11b2a7d commit 93c91a0
Showing 1 changed file with 0 additions and 65 deletions.
Expand Up @@ -23,7 +23,6 @@
import java.lang.ref.Reference;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InaccessibleObjectException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.security.AccessController;
Expand All @@ -32,14 +31,12 @@
import java.sql.DriverManager;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.ResourceBundle;

import org.glassfish.web.util.IntrospectionUtils;

import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.ERROR;
import static java.lang.System.Logger.Level.INFO;
import static java.lang.System.Logger.Level.TRACE;
import static java.lang.System.Logger.Level.WARNING;
import static java.text.MessageFormat.format;
Expand Down Expand Up @@ -75,7 +72,6 @@ void clearReferences(Collection<ResourceEntry> resourceEntries) {

clearReferencesJdbc();
checkThreadLocalsForLeaks();
clearReferencesRmiTargets();

// Null out any static or final fields from loaded classes,
// as a workaround for apparent garbage collection bugs
Expand Down Expand Up @@ -217,67 +213,6 @@ private void checkThreadLocalMapForLeaks(Object threadLocalMap, Field internalTa
}


/**
* Clear RMI Targets loaded by this class loader.
* This depends on the internals of the JVM so it does everything by reflection.
*/
private void clearReferencesRmiTargets() {
LOG.log(TRACE, "clearReferencesRmiTargets()");
try {
// Need access to the ccl field of sun.rmi.transport.Target
Class<?> objectTargetClass = Class.forName("sun.rmi.transport.Target");
Field cclField = objectTargetClass.getDeclaredField("ccl");
setAccessible(cclField);

// Clear the objTable map
Class<?> objectTableClass = Class.forName("sun.rmi.transport.ObjectTable");
Field objTableField = objectTableClass.getDeclaredField("objTable");
setAccessible(objTableField);
Object objTable = objTableField.get(null);
if (objTable == null) {
return;
}

// Iterate over the values in the table
if (objTable instanceof Map<?, ?>) {
Iterator<?> iter = ((Map<?, ?>) objTable).values().iterator();
while (iter.hasNext()) {
Object obj = iter.next();
Object cclObject = cclField.get(obj);
if (loader == cclObject) {
iter.remove();
}
}
}

// Clear the implTable map
Field implTableField = objectTableClass.getDeclaredField("implTable");
setAccessible(implTableField);
Object implTable = implTableField.get(null);
if (implTable == null) {
return;
}

// Iterate over the values in the table
if (implTable instanceof Map<?, ?>) {
Iterator<?> iter = ((Map<?, ?>) implTable).values().iterator();
while (iter.hasNext()) {
Object obj = iter.next();
Object cclObject = cclField.get(obj);
if (loader == cclObject) {
iter.remove();
}
}
}
} catch (ClassNotFoundException e) {
LOG.log(INFO, getString(LogFacade.CLEAR_RMI_INFO, loader.getName()), e);
} catch (SecurityException | NoSuchFieldException | IllegalArgumentException | IllegalAccessException
| InaccessibleObjectException e) {
LOG.log(WARNING, getString(LogFacade.CLEAR_RMI_FAIL, loader.getName()), e);
}
}


private void clearReferencesStaticFinal(Collection<ResourceEntry> resourceEntries) {
LOG.log(TRACE, "clearReferencesStaticFinal(resourceEntries={0})", resourceEntries);
Iterator<ResourceEntry> loadedClasses = resourceEntries.iterator();
Expand Down

0 comments on commit 93c91a0

Please sign in to comment.