From ac23f2485e1d5eb8e8294ebb4b1682f3bd8a0350 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 c2b4270388..94f6294954 100644 --- a/common/src/main/java/io/netty/util/internal/PlatformDependent.java +++ b/common/src/main/java/io/netty/util/internal/PlatformDependent.java @@ -263,6 +263,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. */