Skip to content

Commit

Permalink
HHH-16911 MapBackedClassValue ClassLoader leak
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanne committed Aug 1, 2023
1 parent 3b05c27 commit 1642119
Showing 1 changed file with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected V computeValue(final Class<?> type) {
public MapBackedClassValue(final Map<Class<?>, V> map) {
//Defensive copy, and implicit null check.
//Choose the Map.copyOf implementation as it has a compact layout;
//it doesn't have great get() performance but it's acceptable since we're performing that at most
//it doesn't have great get() performance, but it's acceptable since we're performing that at most
//once per key before caching it via the ClassValue.
this.map = Map.copyOf( map );
}
Expand All @@ -50,17 +50,17 @@ public V get(Class<?> key) {
}

/**
* Use this to wipe the backing map, but N.B.
* we won't be clearing the ClassValue: this is useful
* only to avoid classloader leaks since the Map
* may hold references to user classes.
* Since ClassValue is also possibly caching state,
* it might be possible to retrieve some values after this
* but shouldn't be relied on.
* ClassValue doesn't leak references to classes.
* Use this to wipe the backing map, important
* to avoid classloader leaks.
*/
public void dispose() {
Map<Class<?>, V> existing = this.map;
this.map = null;
if ( existing != null ) {
for ( Map.Entry<Class<?>, V> entry : existing.entrySet() ) {
this.classValue.remove( entry.getKey() );
}
}
}

}

0 comments on commit 1642119

Please sign in to comment.