Class LeakDetector

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

public final class LeakDetector extends Object
Global singleton detector for ClassLoader memory leaks ClassLoader 内存泄漏的全局单例检测器

Uses PhantomReference and a ReferenceQueue to detect ClassLoader instances that are garbage-collected without being explicitly closed (untracked). A virtual-thread daemon continuously polls the reference queue and generates LeakReport entries for any leaked ClassLoaders.

使用 PhantomReferenceReferenceQueue 检测未显式关闭(取消追踪) 就被垃圾回收的 ClassLoader 实例。一个虚拟线程守护进程持续轮询引用队列, 并为任何泄漏的 ClassLoader 生成 LeakReport 条目。

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
Since:
JDK 25, opencode-base-classloader V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clear all leak reports 清除所有泄漏报告
    Returns the global singleton instance (lazy-initialized on first call) 返回全局单例实例(首次调用时延迟初始化)
    Returns the list of detected leak reports 返回已检测到的泄漏报告列表
    int
    Returns the number of currently tracked ClassLoaders 返回当前被追踪的 ClassLoader 数量
    void
    Shutdown the leak detector poll thread 关闭泄漏检测器轮询线程
    void
    track(ClassLoader classLoader, String name, LeakDetection level)
    Register a ClassLoader for leak tracking 注册一个 ClassLoader 进行泄漏追踪
    void
    untrack(ClassLoader classLoader)
    Unregister a ClassLoader from leak tracking (called on close) 取消注册 ClassLoader 的泄漏追踪(在 close 时调用)

    Methods inherited from class Object

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

    • shutdown

      public void shutdown()
      Shutdown the leak detector poll thread 关闭泄漏检测器轮询线程

      Interrupts the poll thread. Primarily useful for orderly shutdown in application servers or test environments.

      中断轮询线程。主要用于应用服务器或测试环境中的有序关闭。

    • getInstance

      public static LeakDetector getInstance()
      Returns the global singleton instance (lazy-initialized on first call) 返回全局单例实例(首次调用时延迟初始化)
      Returns:
      the singleton LeakDetector | 单例 LeakDetector
    • track

      public void track(ClassLoader classLoader, String name, LeakDetection level)
      Register a ClassLoader for leak tracking 注册一个 ClassLoader 进行泄漏追踪

      If the level is LeakDetection.DISABLED, this method is a no-op.

      如果级别为 LeakDetection.DISABLED,此方法不执行任何操作。

      Parameters:
      classLoader - the ClassLoader to track | 要追踪的 ClassLoader
      name - a descriptive name for the ClassLoader | ClassLoader 的描述性名称
      level - the detection level | 检测级别
      Throws:
      NullPointerException - if any argument is null | 当任何参数为 null 时
    • untrack

      public void untrack(ClassLoader classLoader)
      Unregister a ClassLoader from leak tracking (called on close) 取消注册 ClassLoader 的泄漏追踪(在 close 时调用)
      Parameters:
      classLoader - the ClassLoader to untrack | 要取消追踪的 ClassLoader
      Throws:
      NullPointerException - if classLoader is null | 当 classLoader 为 null 时
    • getLeakReports

      public List<LeakReport> getLeakReports()
      Returns the list of detected leak reports 返回已检测到的泄漏报告列表
      Returns:
      unmodifiable list of leak reports | 不可修改的泄漏报告列表
    • getTrackedCount

      public int getTrackedCount()
      Returns the number of currently tracked ClassLoaders 返回当前被追踪的 ClassLoader 数量
      Returns:
      tracked count | 追踪数量
    • clearReports

      public void clearReports()
      Clear all leak reports 清除所有泄漏报告