GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add option to ignore MaxDirectMemorySize JVM argument (#17177) · netty/netty@8f60926

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • common/src
    • main/java/io/netty/util/internal
      • PlatformDependent.java
      • RuntimeJvmArgs.java
    • test/java/io/netty/util/internal
      • RuntimeJvmArgsTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎common/src/main/java/io/netty/util/internal/PlatformDependent.java‎

Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/internal/PlatformDependent.java
+8-58Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import java.io.File;
4444
import java.io.IOException;
4545
import java.io.InputStreamReader;
46-
import java.lang.invoke.MethodHandle;
4746
import java.lang.invoke.MethodHandles;
4847
import java.lang.invoke.VarHandle;
4948
import java.lang.reflect.Field;
@@ -59,7 +58,6 @@
5958
import java.util.Collections;
6059
import java.util.Deque;
6160
import java.util.LinkedHashSet;
62-
import java.util.List;
6361
import java.util.Locale;
6462
import java.util.Map;
6563
import java.util.Queue;
@@ -81,7 +79,6 @@
8179
import static io.netty.util.internal.PlatformDependent0.unalignedAccess;
8280
import static java.lang.Math.max;
8381
import static java.lang.Math.min;
84-
import static java.lang.invoke.MethodType.methodType;
8582

8683
/**
8784
* Utility that detects various properties specific to the current runtime
@@ -95,12 +92,13 @@ public final class PlatformDependent {
9592

9693
private static final InternalLogger logger = InternalLoggerFactory.getInstance(PlatformDependent.class);
9794

98-
private static Pattern MAX_DIRECT_MEMORY_SIZE_ARG_PATTERN;
9995
private static final boolean MAYBE_SUPER_USER;
10096

10197
private static final boolean CAN_ENABLE_TCP_NODELAY_BY_DEFAULT = !isAndroid();
10298

10399
private static final Throwable UNSAFE_UNAVAILABILITY_CAUSE = unsafeUnavailabilityCause0();
100+
private static final boolean IS_J9_JVM = isJ9Jvm0();
101+
private static final boolean IS_IVKVM_DOT_NET = isIkvmDotNet0();
104102
private static final boolean DIRECT_BUFFER_PREFERRED;
105103
private static final boolean EXPLICIT_NO_PREFER_DIRECT;
106104
private static final long MAX_DIRECT_MEMORY = estimateMaxDirectMemory();
@@ -121,8 +119,6 @@ public final class PlatformDependent {
121119

122120
private static final boolean IS_WINDOWS = isWindows0();
123121
private static final boolean IS_OSX = isOsx0();
124-
private static final boolean IS_J9_JVM = isJ9Jvm0();
125-
private static final boolean IS_IVKVM_DOT_NET = isIkvmDotNet0();
126122

127123
private static final int ADDRESS_SIZE = addressSize0();
128124
private static final AtomicLong DIRECT_MEMORY_COUNTER;
@@ -1515,16 +1511,6 @@ private static boolean isIkvmDotNet0() {
15151511
return vmName.equals("IKVM.NET");
15161512
}
15171513

1518-
private static Pattern getMaxDirectMemorySizeArgPattern() {
1519-
// Pattern's is immutable so it's always safe published
1520-
Pattern pattern = MAX_DIRECT_MEMORY_SIZE_ARG_PATTERN;
1521-
if (pattern == null) {
1522-
pattern = Pattern.compile("\\s*-XX:MaxDirectMemorySize\\s*=\\s*([0-9]+)\\s*([kKmMgG]?)\\s*$");
1523-
MAX_DIRECT_MEMORY_SIZE_ARG_PATTERN = pattern;
1524-
}
1525-
return pattern;
1526-
}
1527-
15281514
/**
15291515
* Compute an estimate of the maximum amount of direct memory available to this JVM.
15301516
* <p>
@@ -1541,48 +1527,8 @@ public static long estimateMaxDirectMemory() {
15411527
return maxDirectMemory;
15421528
}
15431529

1544-
try {
1545-
// Now try to get the JVM option (-XX:MaxDirectMemorySize) and parse it.
1546-
// Note that we are using reflection because Android doesn't have these classes.
1547-
ClassLoader systemClassLoader = getSystemClassLoader();
1548-
Class<?> mgmtFactoryClass = Class.forName(
1549-
"java.lang.management.ManagementFactory", true, systemClassLoader);
1550-
Class<?> runtimeClass = Class.forName(
1551-
"java.lang.management.RuntimeMXBean", true, systemClassLoader);
1552-
1553-
MethodHandles.Lookup lookup = MethodHandles.publicLookup();
1554-
MethodHandle getRuntime = lookup.findStatic(
1555-
mgmtFactoryClass, "getRuntimeMXBean", methodType(runtimeClass));
1556-
MethodHandle getInputArguments = lookup.findVirtual(
1557-
runtimeClass, "getInputArguments", methodType(List.class));
1558-
List<String> vmArgs = (List<String>) getInputArguments.invoke(getRuntime.invoke());
1559-
1560-
Pattern maxDirectMemorySizeArgPattern = getMaxDirectMemorySizeArgPattern();
1561-
1562-
for (int i = vmArgs.size() - 1; i >= 0; i --) {
1563-
Matcher m = maxDirectMemorySizeArgPattern.matcher(vmArgs.get(i));
1564-
if (!m.matches()) {
1565-
continue;
1566-
}
1567-
1568-
maxDirectMemory = Long.parseLong(m.group(1));
1569-
switch (m.group(2).charAt(0)) {
1570-
case 'k': case 'K':
1571-
maxDirectMemory *= 1024;
1572-
break;
1573-
case 'm': case 'M':
1574-
maxDirectMemory *= 1024 * 1024;
1575-
break;
1576-
case 'g': case 'G':
1577-
maxDirectMemory *= 1024 * 1024 * 1024;
1578-
break;
1579-
default:
1580-
break;
1581-
}
1582-
break;
1583-
}
1584-
} catch (Throwable ignored) {
1585-
// Ignore
1530+
if (!isAndroid() && !ignoreMaxDirectMemorySize()) {
1531+
maxDirectMemory = RuntimeJvmArgs.parseMaxDirectMemorySize(maxDirectMemory);
15861532
}
15871533

15881534
if (maxDirectMemory <= 0) {
@@ -1595,6 +1541,10 @@ public static long estimateMaxDirectMemory() {
15951541
return maxDirectMemory;
15961542
}
15971543

1544+
private static boolean ignoreMaxDirectMemorySize() {
1545+
return SystemPropertyUtil.getBoolean("io.netty.ignoreMaxDirectMemorySize", false);
1546+
}
1547+
15981548
private static File tmpdir0() {
15991549
File f;
16001550
try {
Collapse file

‎common/src/main/java/io/netty/util/internal/RuntimeJvmArgs.java‎

Copy file name to clipboard
+79Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2026 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in
6+
* compliance with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13+
* implied. See the License for the specific language governing
14+
* permissions and limitations under the License.
15+
*/
16+
package io.netty.util.internal;
17+
18+
import java.lang.management.ManagementFactory;
19+
import java.util.List;
20+
21+
/**
22+
* Avoid class verification failures on Android.
23+
* Android does not have {@link ManagementFactory},
24+
* so loading this class on Android will fail with
25+
* {@link NoClassDefFoundError}.
26+
*/
27+
final class RuntimeJvmArgs {
28+
29+
private static final String MAX_DIRECT_MEMORY_SIZE_ARG =
30+
"-XX:MaxDirectMemorySize=";
31+
32+
private RuntimeJvmArgs() {
33+
}
34+
35+
static long parseMaxDirectMemorySize(final long maxDirectMemory) {
36+
try {
37+
List<String> vmArgs = ManagementFactory
38+
.getRuntimeMXBean()
39+
.getInputArguments();
40+
for (int i = vmArgs.size() - 1; i >= 0; i--) {
41+
String arg = vmArgs.get(i);
42+
if (!arg.startsWith(MAX_DIRECT_MEMORY_SIZE_ARG)) {
43+
continue;
44+
}
45+
return parseSize(arg, MAX_DIRECT_MEMORY_SIZE_ARG.length());
46+
}
47+
} catch (Throwable ignored) {
48+
// Ignore
49+
}
50+
return maxDirectMemory;
51+
}
52+
53+
static long parseSize(final String arg, final int offset) {
54+
String val = arg.substring(offset).trim();
55+
if (val.isEmpty()) {
56+
return -1;
57+
}
58+
char lastChar = val.charAt(val.length() - 1);
59+
long multiplier;
60+
switch (lastChar) {
61+
case 'k':
62+
case 'K':
63+
multiplier = 1024;
64+
break;
65+
case 'm':
66+
case 'M':
67+
multiplier = 1024 * 1024;
68+
break;
69+
case 'g':
70+
case 'G':
71+
multiplier = 1024L * 1024 * 1024;
72+
break;
73+
default:
74+
return Long.parseLong(val);
75+
}
76+
String numStr = val.substring(0, val.length() - 1);
77+
return Long.parseLong(numStr) * multiplier;
78+
}
79+
}
Collapse file

‎common/src/test/java/io/netty/util/internal/RuntimeJvmArgsTest.java‎

Copy file name to clipboard
+86Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2026 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in
6+
* compliance with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13+
* implied. See the License for the specific language governing
14+
* permissions and limitations under the License.
15+
*/
16+
package io.netty.util.internal;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import java.lang.management.ManagementFactory;
21+
import java.util.List;
22+
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
import static org.junit.jupiter.api.Assertions.assertThrows;
25+
26+
public class RuntimeJvmArgsTest {
27+
28+
private static final String ARG_PREFIX = "-XX:MaxDirectMemorySize=";
29+
30+
@Test
31+
public void testParseSizeWithoutSuffix() {
32+
assertEquals(12345L, RuntimeJvmArgs.parseSize(ARG_PREFIX + "12345", ARG_PREFIX.length()));
33+
}
34+
35+
@Test
36+
public void testParseSizeWithKiloSuffix() {
37+
assertEquals(2048L, RuntimeJvmArgs.parseSize(ARG_PREFIX + "2k", ARG_PREFIX.length()));
38+
assertEquals(2048L, RuntimeJvmArgs.parseSize(ARG_PREFIX + "2K", ARG_PREFIX.length()));
39+
}
40+
41+
@Test
42+
public void testParseSizeWithMegaSuffix() {
43+
assertEquals(8L * 1024 * 1024, RuntimeJvmArgs.parseSize(ARG_PREFIX + "8m", ARG_PREFIX.length()));
44+
assertEquals(8L * 1024 * 1024, RuntimeJvmArgs.parseSize(ARG_PREFIX + "8M", ARG_PREFIX.length()));
45+
}
46+
47+
@Test
48+
public void testParseSizeWithGigaSuffix() {
49+
assertEquals(1024L * 1024 * 1024, RuntimeJvmArgs.parseSize(ARG_PREFIX + "1g", ARG_PREFIX.length()));
50+
assertEquals(1024L * 1024 * 1024, RuntimeJvmArgs.parseSize(ARG_PREFIX + "1G", ARG_PREFIX.length()));
51+
}
52+
53+
@Test
54+
public void testParseSizeWithEmptyValueReturnsNegativeOne() {
55+
assertEquals(-1L, RuntimeJvmArgs.parseSize(ARG_PREFIX, ARG_PREFIX.length()));
56+
}
57+
58+
@Test
59+
public void testParseSizeWithWhitespaceOnlyValueReturnsNegativeOne() {
60+
assertEquals(-1L, RuntimeJvmArgs.parseSize(ARG_PREFIX + " ", ARG_PREFIX.length()));
61+
}
62+
63+
@Test
64+
public void testParseSizeWithInvalidNumberThrows() {
65+
assertThrows(NumberFormatException.class,
66+
() -> RuntimeJvmArgs.parseSize(ARG_PREFIX + "abc", ARG_PREFIX.length()));
67+
}
68+
69+
@Test
70+
public void testParseSizeWithUnsupportedSuffixThrows() {
71+
assertThrows(NumberFormatException.class,
72+
() -> RuntimeJvmArgs.parseSize(ARG_PREFIX + "5t", ARG_PREFIX.length()));
73+
}
74+
75+
@Test
76+
public void testParseMaxDirectMemorySizeFallsBackWhenArgAbsent() {
77+
List<String> vmArgs = ManagementFactory.getRuntimeMXBean().getInputArguments();
78+
for (String arg : vmArgs) {
79+
if (arg.startsWith(ARG_PREFIX)) {
80+
// The running JVM was launched with the flag; skip as the outcome is environment-dependent.
81+
return;
82+
}
83+
}
84+
assertEquals(1234L, RuntimeJvmArgs.parseMaxDirectMemorySize(1234L));
85+
}
86+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.