Class LeakCleaner

java.lang.Object
cloud.opencode.base.classloader.leak.LeakCleaner

public final class LeakCleaner extends Object
Utility class for cleaning up resources tied to a ClassLoader to prevent memory leaks 用于清理绑定到 ClassLoader 的资源以防止内存泄漏的工具类

When a web application or plugin is unloaded, its ClassLoader should become eligible for garbage collection. However, certain JDK and library resources (JDBC drivers, ThreadLocals, shutdown hooks, timers) can pin the ClassLoader in memory. This utility provides best-effort cleanup of these resources.

当 Web 应用程序或插件被卸载时,其 ClassLoader 应该变得可被垃圾回收。 但是,某些 JDK 和库资源(JDBC 驱动、ThreadLocal、关闭钩子、计时器) 可能会将 ClassLoader 钉在内存中。此工具提供这些资源的尽力清理。

Features | 主要功能:

  • JDBC driver deregistration (public API) - JDBC 驱动注销(公共 API)
  • ThreadLocal cleanup (reflection-based, best-effort) - ThreadLocal 清理(基于反射,尽力而为)
  • Shutdown hook removal (reflection-based, best-effort) - 关闭钩子移除(基于反射,尽力而为)
  • Timer thread cancellation (best-effort) - 计时器线程取消(尽力而为)
  • Combined cleanup with aggregated report - 组合清理与汇总报告

Usage Examples | 使用示例:

// Clean all resources tied to a ClassLoader
CleanupReport report = LeakCleaner.cleanAll(myClassLoader);
System.out.println("Drivers removed: " + report.jdbcDriversRemoved());

// Clean only JDBC drivers
int count = LeakCleaner.cleanJdbcDrivers(myClassLoader);

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是 (无状态工具类)
  • Uses reflection where necessary; gracefully degrades under module restrictions - 必要时使用反射;在模块限制下优雅降级
Since:
JDK 25, opencode-base-classloader V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    cleanAll(ClassLoader classLoader)
    Run all cleanup operations for the given ClassLoader and return an aggregated report 对给定的 ClassLoader 运行所有清理操作并返回汇总报告
    static int
    Deregister JDBC drivers loaded by the given ClassLoader 注销由给定 ClassLoader 加载的 JDBC 驱动
    static int
    Remove shutdown hooks registered by the given ClassLoader 移除由给定 ClassLoader 注册的关闭钩子
    static int
    Clean ThreadLocal entries that reference classes loaded by the given ClassLoader 清理引用由给定 ClassLoader 加载的类的 ThreadLocal 条目
    static int
    cleanTimers(ClassLoader classLoader)
    Cancel timer threads loaded by the given ClassLoader 取消由给定 ClassLoader 加载的计时器线程

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • cleanAll

      public static CleanupReport cleanAll(ClassLoader classLoader)
      Run all cleanup operations for the given ClassLoader and return an aggregated report 对给定的 ClassLoader 运行所有清理操作并返回汇总报告

      Each cleanup operation is independent — a failure in one does not prevent the others from executing. All errors are collected in the returned report.

      每个清理操作都是独立的 — 一个操作的失败不会阻止其他操作的执行。 所有错误都收集在返回的报告中。

      Parameters:
      classLoader - the ClassLoader whose resources should be cleaned | 应清理其资源的 ClassLoader
      Returns:
      aggregated cleanup report | 汇总的清理报告
      Throws:
      NullPointerException - if classLoader is null | 当 classLoader 为 null 时
    • cleanJdbcDrivers

      public static int cleanJdbcDrivers(ClassLoader classLoader)
      Deregister JDBC drivers loaded by the given ClassLoader 注销由给定 ClassLoader 加载的 JDBC 驱动

      Uses the public DriverManager.drivers() API (JDK 9+) to enumerate registered drivers and deregisters those whose class was loaded by the specified ClassLoader.

      使用公共 DriverManager.drivers() API(JDK 9+)枚举已注册的驱动, 并注销由指定 ClassLoader 加载的驱动。

      Parameters:
      classLoader - the ClassLoader whose drivers should be removed | 应移除其驱动的 ClassLoader
      Returns:
      the number of drivers deregistered | 已注销的驱动数
      Throws:
      NullPointerException - if classLoader is null | 当 classLoader 为 null 时
    • cleanThreadLocals

      public static int cleanThreadLocals(ClassLoader classLoader)
      Clean ThreadLocal entries that reference classes loaded by the given ClassLoader 清理引用由给定 ClassLoader 加载的类的 ThreadLocal 条目

      Iterates all live threads and uses reflection to access each thread's threadLocals field. If the module system prevents access, this method logs a warning and returns 0.

      遍历所有活动线程,使用反射访问每个线程的 threadLocals 字段。 如果模块系统阻止访问,此方法记录警告并返回 0。

      Parameters:
      classLoader - the ClassLoader whose ThreadLocal entries should be cleared | 应清除其 ThreadLocal 条目的 ClassLoader
      Returns:
      the number of ThreadLocal entries cleared | 已清除的 ThreadLocal 条目数
      Throws:
      NullPointerException - if classLoader is null | 当 classLoader 为 null 时
    • cleanShutdownHooks

      public static int cleanShutdownHooks(ClassLoader classLoader)
      Remove shutdown hooks registered by the given ClassLoader 移除由给定 ClassLoader 注册的关闭钩子

      Uses reflection to access the shutdown hooks registry in the Runtime. If the module system prevents access, this method returns 0 with errors logged.

      使用反射访问 Runtime 中的关闭钩子注册表。 如果模块系统阻止访问,此方法返回 0 并记录错误。

      Parameters:
      classLoader - the ClassLoader whose shutdown hooks should be removed | 应移除其关闭钩子的 ClassLoader
      Returns:
      the number of shutdown hooks removed | 已移除的关闭钩子数
      Throws:
      NullPointerException - if classLoader is null | 当 classLoader 为 null 时
    • cleanTimers

      public static int cleanTimers(ClassLoader classLoader)
      Cancel timer threads loaded by the given ClassLoader 取消由给定 ClassLoader 加载的计时器线程

      Scans all live threads for those whose class was loaded by the given ClassLoader and whose name or class suggests they are timer threads. Interrupts matching threads.

      扫描所有活动线程,查找其类由给定 ClassLoader 加载且其名称或类表明是计时器线程的线程。 中断匹配的线程。

      Parameters:
      classLoader - the ClassLoader whose timer threads should be cancelled | 应取消其计时器线程的 ClassLoader
      Returns:
      the number of timer threads cancelled | 已取消的计时器线程数
      Throws:
      NullPointerException - if classLoader is null | 当 classLoader 为 null 时