Package hudson.util

Class DelegatingClassLoader

java.lang.Object
java.lang.ClassLoader
hudson.util.DelegatingClassLoader
Direct Known Subclasses:
ExistenceCheckingClassLoader, MaskingClassLoader, PluginManager.UberClassLoader

@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public abstract class DelegatingClassLoader extends ClassLoader
A ClassLoader that does not define any classes itself but delegates class loading to other class loaders to avoid the JDK's per-class-name locking and lock retention.

This class first attempts to load classes via its ClassLoader.getParent() class loader, then falls back to ClassLoader.findClass(java.lang.String) to allow for custom delegation logic.

In a parallel-capable ClassLoader, the JDK maintains a per-name lock object indefinitely. In Jenkins, many class loading misses across many loaders can accumulate hundreds of thousands of such locks, retaining significant memory. This loader never defines classes and bypasses ClassLoader's default loadClass locking; it delegates to the parent first and then to findClass for custom delegation.

The actual defining loader (parent or a delegate) still performs the necessary synchronization and class definition. A runtime guard (verify(java.lang.Class<?>)) throws if this loader ever becomes the defining loader.

Subclasses must not call defineClass; implement delegation in findClass if needed and do not mark subclasses as parallel-capable.

Author:
Dmytro Ukhlov
  • Constructor Details

    • DelegatingClassLoader

      protected DelegatingClassLoader(String name, ClassLoader parent)
    • DelegatingClassLoader

      public DelegatingClassLoader(ClassLoader parent)
  • Method Details

    • loadClass

      protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException
      Parent-first delegation without synchronizing on ClassLoader.getClassLoadingLock(String). This prevents creation/retention of per-name lock objects in a loader that does not define classes. The defining loader downstream still serializes class definition as required.
      Overrides:
      loadClass in class ClassLoader
      Parameters:
      name - The binary name of the class
      resolve - If true then resolve the class
      Returns:
      The resulting Class object
      Throws:
      ClassNotFoundException - If the class could not be found