From eaf177133638b528a8d644b7d0bf3665cd90a18c Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Wed, 25 Apr 2018 07:33:07 +0200 Subject: [PATCH] Don't use VM.maxDirectMemory() on z/OS to retrieve direct memory limit. (#7886) Motivation: On z/OS netty initializes this value with 64M, even the direct accessible memory is actually unbounded. Modifications: Skip usage of VM.maxDirectMemory() on z/OS. Result: More correct direct memory limit detection. Fixes https://github.com/netty/netty/issues/7654. --- .../io/netty/util/internal/PlatformDependent.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) 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 177f16bfdb..4623459c5d 100644 --- a/common/src/main/java/io/netty/util/internal/PlatformDependent.java +++ b/common/src/main/java/io/netty/util/internal/PlatformDependent.java @@ -988,13 +988,20 @@ public final class PlatformDependent { private static long maxDirectMemory0() { long maxDirectMemory = 0; + ClassLoader systemClassLoader = null; try { - // Try to get from sun.misc.VM.maxDirectMemory() which should be most accurate. systemClassLoader = getSystemClassLoader(); - Class vmClass = Class.forName("sun.misc.VM", true, systemClassLoader); - Method m = vmClass.getDeclaredMethod("maxDirectMemory"); - maxDirectMemory = ((Number) m.invoke(null)).longValue(); + + // On z/OS we should not use VM.maxDirectMemory() as it not reflects the correct value. + // See: + // - https://github.com/netty/netty/issues/7654 + if (!SystemPropertyUtil.get("os.name", "").toLowerCase().contains("z/os")) { + // Try to get from sun.misc.VM.maxDirectMemory() which should be most accurate. + Class vmClass = Class.forName("sun.misc.VM", true, systemClassLoader); + Method m = vmClass.getDeclaredMethod("maxDirectMemory"); + maxDirectMemory = ((Number) m.invoke(null)).longValue(); + } } catch (Throwable ignored) { // Ignore }