Home / Class/ ResourceScope Class — netty Architecture

ResourceScope Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  3dc7780b_2502_84f5_0a0e_ccc5113b7e2d["ResourceScope"]
  acb11df3_85c3_c25d_0290_bc4b45701d31["LeakPresenceDetector.java"]
  3dc7780b_2502_84f5_0a0e_ccc5113b7e2d -->|defined in| acb11df3_85c3_c25d_0290_bc4b45701d31
  bbdfe024_db5f_ae56_b952_c0cfc052f68f["ResourceScope()"]
  3dc7780b_2502_84f5_0a0e_ccc5113b7e2d -->|method| bbdfe024_db5f_ae56_b952_c0cfc052f68f
  b2910c2a_467a_024b_eca1_d065f2238602["checkOpen()"]
  3dc7780b_2502_84f5_0a0e_ccc5113b7e2d -->|method| b2910c2a_467a_024b_eca1_d065f2238602
  95093f73_e143_2bf9_5d95_2adba188c280["check()"]
  3dc7780b_2502_84f5_0a0e_ccc5113b7e2d -->|method| 95093f73_e143_2bf9_5d95_2adba188c280
  1f25563b_06bc_840e_0a19_d5c8e5bf2d1f["hasOpenResources()"]
  3dc7780b_2502_84f5_0a0e_ccc5113b7e2d -->|method| 1f25563b_06bc_840e_0a19_d5c8e5bf2d1f
  38dcc791_90be_c5aa_7d59_e5d118926a5a["close()"]
  3dc7780b_2502_84f5_0a0e_ccc5113b7e2d -->|method| 38dcc791_90be_c5aa_7d59_e5d118926a5a

Relationship Graph

Source Code

common/src/main/java/io/netty/util/LeakPresenceDetector.java lines 260–329

    public static final class ResourceScope implements Closeable {
        final String name;
        final LongAdder openResourceCounter = new LongAdder();
        final Map<PresenceTracker<?>, Throwable> creationStacks =
                TRACK_CREATION_STACK ? new ConcurrentHashMap<>() : null;
        boolean closed;

        /**
         * Create a new scope.
         *
         * @param name The scope name, used for error reporting
         */
        public ResourceScope(String name) {
            this.name = name;
        }

        void checkOpen() {
            if (closed) {
                throw new AllocationProhibitedException("Resource scope '" + name + "' already closed");
            }
        }

        void check() {
            long n = openResourceCounter.sumThenReset();
            if (n != 0) {
                StringBuilder msg = new StringBuilder("Possible memory leak detected for resource scope '")
                        .append(name).append("'. ");
                if (n < 0) {
                    msg.append("Resource count was negative: A resource previously reported as a leak was released " +
                            "after all. Please ensure that that resource is released before its test finishes.");
                    throw new IllegalStateException(msg.toString());
                }
                if (TRACK_CREATION_STACK) {
                    msg.append("Creation stack traces:");
                    IllegalStateException ise = new IllegalStateException(msg.toString());
                    int i = 0;
                    for (Throwable t : creationStacks.values()) {
                        ise.addSuppressed(t);
                        if (i++ > 5) {
                            break;
                        }
                    }
                    creationStacks.clear();
                    throw ise;
                }
                msg.append("Please use paranoid leak detection to get more information, or set " +
                        "-D" + TRACK_CREATION_STACK_PROPERTY + "=true");
                throw new IllegalStateException(msg.toString());
            }
        }

        /**
         * Check whether there are any open resources left, and {@link #close()} would throw.
         *
         * @return {@code true} if there are open resources
         */
        public boolean hasOpenResources() {
            return openResourceCounter.sum() > 0;
        }

        /**
         * Close this scope. Closing a scope will prevent new resources from being allocated (or released) in this
         * scope. The call also throws an exception if there are any resources left open.
         */
        @Override
        public void close() {
            closed = true;
            check();
        }
    }

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free