Home / Class/ LeakPresenceDetector Class — netty Architecture

LeakPresenceDetector Class — netty Architecture

Architecture documentation for the LeakPresenceDetector class in LeakPresenceDetector.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  c3f8e874_7121_5232_86be_a0f7230a975e["LeakPresenceDetector"]
  acb11df3_85c3_c25d_0290_bc4b45701d31["LeakPresenceDetector.java"]
  c3f8e874_7121_5232_86be_a0f7230a975e -->|defined in| acb11df3_85c3_c25d_0290_bc4b45701d31
  e98e1d57_b1eb_b61a_2695_0603cacaff79["inStaticInitializerSlow()"]
  c3f8e874_7121_5232_86be_a0f7230a975e -->|method| e98e1d57_b1eb_b61a_2695_0603cacaff79
  d42e92d8_0864_ea4c_70e8_1a947bd4e052["inStaticInitializerFast()"]
  c3f8e874_7121_5232_86be_a0f7230a975e -->|method| d42e92d8_0864_ea4c_70e8_1a947bd4e052
  6e240761_5949_e8d5_e183_dc6f994cf447["R()"]
  c3f8e874_7121_5232_86be_a0f7230a975e -->|method| 6e240761_5949_e8d5_e183_dc6f994cf447
  f840e332_5108_6066_f108_0e3a557992d0["LeakPresenceDetector()"]
  c3f8e874_7121_5232_86be_a0f7230a975e -->|method| f840e332_5108_6066_f108_0e3a557992d0
  439e99fa_6566_6273_0ac0_65b3a7b5d745["ResourceScope()"]
  c3f8e874_7121_5232_86be_a0f7230a975e -->|method| 439e99fa_6566_6273_0ac0_65b3a7b5d745
  444ac5fb_0a03_7d35_4d16_8c2ce1672a66["track()"]
  c3f8e874_7121_5232_86be_a0f7230a975e -->|method| 444ac5fb_0a03_7d35_4d16_8c2ce1672a66
  ccf86979_4a86_08f3_73c6_2aa92e40925b["trackForcibly()"]
  c3f8e874_7121_5232_86be_a0f7230a975e -->|method| ccf86979_4a86_08f3_73c6_2aa92e40925b
  23ad2a85_3e23_08b2_59d0_ac921a198897["isRecordEnabled()"]
  c3f8e874_7121_5232_86be_a0f7230a975e -->|method| 23ad2a85_3e23_08b2_59d0_ac921a198897
  9091c485_77ea_292e_4e8a_05c5d72a80c3["check()"]
  c3f8e874_7121_5232_86be_a0f7230a975e -->|method| 9091c485_77ea_292e_4e8a_05c5d72a80c3

Relationship Graph

Source Code

common/src/main/java/io/netty/util/LeakPresenceDetector.java lines 83–361

public class LeakPresenceDetector<T> extends ResourceLeakDetector<T> {
    private static final String TRACK_CREATION_STACK_PROPERTY = "io.netty.util.LeakPresenceDetector.trackCreationStack";
    private static final boolean TRACK_CREATION_STACK =
            SystemPropertyUtil.getBoolean(TRACK_CREATION_STACK_PROPERTY, false);
    private static final ResourceScope GLOBAL = new ResourceScope("global");

    private static int staticInitializerCount;

    private static boolean inStaticInitializerSlow(StackTraceElement[] stackTrace) {
        for (StackTraceElement element : stackTrace) {
            if (element.getMethodName().equals("<clinit>")) {
                return true;
            }
        }
        return false;
    }

    private static boolean inStaticInitializerFast() {
        // This plain field access is safe. The worst that can happen is that we see non-zero where we shouldn't.
        return staticInitializerCount != 0 && inStaticInitializerSlow(Thread.currentThread().getStackTrace());
    }

    /**
     * Wrap a static initializer so that any resources created inside the block will not be tracked. Example:
     * <pre>{@code
     * private static final ByteBuf CRLF_BUF = LeakPresenceDetector.staticInitializer(() -> unreleasableBuffer(
     *             directBuffer(2).writeByte(CR).writeByte(LF)).asReadOnly());
     * }</pre>
     * <p>
     * Note that technically, this method does not actually care what happens inside the block. Instead, it turns on
     * stack trace introspection at the start of the block, and turns it back off at the end. Any allocation in that
     * interval will be checked to see whether it is part of a static initializer, and if it is, it will not be
     * tracked.
     *
     * @param supplier A code block to run
     * @return The value returned by the {@code supplier}
     * @param <R> The supplier return type
     */
    public static <R> R staticInitializer(Supplier<R> supplier) {
        if (!inStaticInitializerSlow(Thread.currentThread().getStackTrace())) {
            throw new IllegalStateException("Not in static initializer.");
        }
        synchronized (LeakPresenceDetector.class) {
            staticInitializerCount++;
        }
        try {
            return supplier.get();
        } finally {
            synchronized (LeakPresenceDetector.class) {
                staticInitializerCount--;
            }
        }
    }

    /**
     * Create a new detector for the given resource type.
     *
     * @param resourceType The resource type
     */
    public LeakPresenceDetector(Class<?> resourceType) {
        super(resourceType, 0);
    }

    /**
     * This constructor should not be used directly, it is called reflectively by {@link ResourceLeakDetectorFactory}.
     *
     * @param resourceType The resource type
     * @param samplingInterval Ignored
     */
    @Deprecated
    @SuppressWarnings("unused")
    public LeakPresenceDetector(Class<?> resourceType, int samplingInterval) {
        this(resourceType);
    }

    /**
     * This constructor should not be used directly, it is called reflectively by {@link ResourceLeakDetectorFactory}.
     *
     * @param resourceType The resource type
     * @param samplingInterval Ignored
     * @param maxActive Ignored

Frequently Asked Questions

What is the LeakPresenceDetector class?
LeakPresenceDetector is a class in the netty codebase, defined in common/src/main/java/io/netty/util/LeakPresenceDetector.java.
Where is LeakPresenceDetector defined?
LeakPresenceDetector is defined in common/src/main/java/io/netty/util/LeakPresenceDetector.java at line 83.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free