From eb1d12c75736c06525d42dab9c7b2e7b7a33eb6d Mon Sep 17 00:00:00 2001 From: Enrico Olivelli Date: Tue, 19 Mar 2019 08:34:35 +0100 Subject: [PATCH] Expose the global direct memory counter. (#8945) Motivation: This counter is very useful in order to monitor Netty without having every ByteBufAllocator in the JVM Modification: Expose the value of DIRECT_MEMORY_COUNTER as we are already doing for DIRECT_MEMORY_LIMIT. We are returning -1 in case that DIRECT_MEMORY_COUNTER is not available. Result: Be able to get the amount of direct memory used. --- .../java/io/netty/util/internal/PlatformDependent.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/common/src/main/java/io/netty/util/internal/PlatformDependent.java b/common/src/main/java/io/netty/util/internal/PlatformDependent.java index 8696996fcb..7614ff9bd9 100644 --- a/common/src/main/java/io/netty/util/internal/PlatformDependent.java +++ b/common/src/main/java/io/netty/util/internal/PlatformDependent.java @@ -288,6 +288,16 @@ public final class PlatformDependent { return DIRECT_MEMORY_LIMIT; } + /** + * Returns the current memory reserved for direct buffer allocation. + * This method returns -1 in case that a value is not available. + * + * @see #maxDirectMemory() + */ + public static long usedDirectMemory() { + return DIRECT_MEMORY_COUNTER != null ? DIRECT_MEMORY_COUNTER.get() : -1; + } + /** * Returns the temporary directory. */