聊聊jvm的-XX:MaxDirectMemorySize

本文详细探讨了JVM的-XX:MaxDirectMemorySize参数,介绍了其作用和默认值,以及如何通过System.initPhase1、VM.saveProperties等方法获取最大直接内存大小。同时,通过实例展示了如何使用API监控directBuffer,以及在出现OOM时的处理。文章还提到了Java 9以后模块化对VM和SharedSecrets的影响,并利用NMT查看directBuffer使用情况。

本文主要研究一下jvm的-XX:MaxDirectMemorySize

-XX:MaxDirectMemorySize

-XX:MaxDirectMemorySize=size用于设置New I/O(java.nio) direct-buffer allocations的最大大小,size的单位可以使用k/K、m/M、g/G;如果没有设置该参数则默认值为0,意味着JVM自己自动给NIO direct-buffer allocations选择最大大小

System.initPhase1

java.base/java/lang/System.java

public final class System {
    /* Register the natives via the static initializer.
     *
     * VM will invoke the initializeSystemClass method to complete
     * the initialization for this class separated from clinit.
     * Note that to use properties set by the VM, see the constraints
     * described in the initializeSystemClass method.
     */
    private static native void registerNatives();
    static {
        registerNatives();
    }

    /** Don't let anyone instantiate this class */
    private System() {
    }

    /**
     * Initialize the system class.  Called after thread initialization.
     */
    private static void initPhase1() {
        // VM might invoke JNU_NewStringPlatform() to set those encoding
        // sensitive properties (user.home, user.name, boot.class.path, etc.)
        // during "props" initialization.
        // The charset is initialized in System.c and does not depend on the Properties.
        Map<String, String> tempProps = SystemProps.initProperties();
        VersionProps.init(tempProps);

        // There are certain system configurations that may be controlled by
        // VM options such as the maximum amount of direct memory and
        // Integer cache size used to support the object identity semantics
        // of autoboxing.  Typically, the library will obtain these values
        // from the properties set by the VM.  If the properties are for
        // internal implementation use only, these properties should be
        // masked from the system properties.
        //
        // Save a private copy of the system properties object that
        // can only be accessed by the internal implementation.
        VM.saveProperties(tempProps);
        props = createProperties(tempProps);

        StaticProperty.javaHome();          // Load StaticProperty to cache the property values

        lineSeparator = props.getProperty("line.separator");

        FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
        FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
        FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
        setIn0(new BufferedInputStream(fdIn));
        setOut0(newPrintStream(fdOut, props.getProperty("sun.stdout.encoding")));
        setErr0(newPrintStream(fdErr, props.getProperty("sun.stderr.encoding")));

        // Setup Java signal handlers for HUP, TERM, and INT (where available).
        Terminator.setup();

        // Initialize any miscellaneous operating system settings that need to be
        // set for the class libraries. Currently this is no-op everywhere except
        // for Windows where the process-wide error mode is set before the java.io
        // classes are used.
        VM.initializeOSEnvironment();

        // The main thread is not added to its thread group in the same
        // way as other threads; we must do it ourselves here.
        Thread current = Thread.currentThread();
        current.getThreadGroup().add(current);

        // register shared secrets
        setJavaLangAccess();

        // Subsystems that are invoked during initialization can invoke
        // VM.isBooted() in order to avoid doing things that should
        // wait until the VM is fully initialized. The initialization level
        // is incremented from 0 to 1 here to indicate the first phase of
        // initialization has completed.
        // IMPORTANT: Ensure that this remains the last initialization action!
        VM.initLevel(1);
    }

    //......
}
复制代码

System的initPhase1方法会调用VM.saveProperties(tempProps)方法来保存一份系统配置供内部实现使用;其中tempProps为SystemProps.initProperties()

jvm.cpp

hotspot/share/prims/jvm.cpp

  // Convert the -XX:MaxDirectMemorySize= command line flag
  // to the sun.nio.MaxDirectMemorySize property.
  // Do this after setting user properties to prevent people
  // from setting the value with a -D option, as requested.
  // Leave empty if not supplied
  if (!FLAG_IS_DEFAULT(MaxDirectMemorySize)) {
    char as_chars[256];
    jio_snprintf(as_chars, sizeof(as_chars), JULONG_FORMAT, MaxDirectMemorySize);
    Handle key_str = java_lang_String::create_from_platform_dependent_str("sun.nio.MaxDirectMemorySize", CHECK_NULL);
    Handle value_str  = java_lang_String::create_from_platform_dependent_str(as_chars, CHECK_NULL);
    result_h->obj_at_put(ndx * 2,  key_str());
    result_h->obj_at_put(ndx * 2 + 1, value_str());
    ndx++;
  }
复制代码

jvm.cpp里头有一段代码用于把-XX:MaxDirectMemorySize命令参数转换为key为sun.nio.MaxDirectMemorySize的属性

VM.saveProperties

java.base/jdk/internal/misc/VM.java

public class VM {

    // the init level when the VM is fully initialized
    private static final int JAVA_LANG_SYSTEM_INITED     = 1;
    private static final int MODULE_SYSTEM_INITED        = 2;
    private static final int SYSTEM_LOADER_INITIALIZING  = 3;
    private static final int SYSTEM_BOOTED               = 4;
    private static final int SYSTEM_SHUTDOWN             = 5;


    // 0, 1, 2, ...
    private static volatile int initLevel;
    private static final Object lock = new Object();

    //......

    // A user-settable upper limit on the maximum amount of allocatable direct
    // buffer memory.  This value may be changed during VM initialization if
    // "java" is launched with "-XX:MaxDirectMemorySize=<size>".
    //
    // The initial value of this field is arbitrary; during JRE initialization
    // it will be reset to the value specified on the command line, if any,
    // otherwise to Runtime.getRuntime().maxMemory().
    //
    private static long directMemory = 64 * 1024 * 1024;

    // Returns the maximum amount of allocatable direct buffer memory.
    // The directMemory variable is initialized during system initialization
    // in the saveAndRemoveProperties method.
    //
    public static long maxDirectMemory() {
        return directMemory;
    }

    //......

    // Save a private copy of the system properties and remove
    // the system properties that are not intended for public access.
    //
    // This method can only be invoked during system initialization.
    public static void saveProperties(Map<String, String> props) {
        if (initLevel() != 0)
            throw new IllegalStateException("Wrong init level");

        // only main thread is running at this time, so savedProps and
        // its content will be correctly published to threads started later
        if (savedProps == null) {
            savedProps = props;
        }

        // Set the maximum amount of direct memory.  This value is controlled
        // by the vm option -XX:MaxDirectMemorySize=<size>.
        // The maximum amount of allocatable direct buffer memory (in bytes)
        // from the system property sun.nio.MaxDirectMemorySize set by the VM.
        // If not set or set to -1, the max memory will be used
        // The system property will be removed.
        String s = props.get("sun.nio.MaxDirectMemorySize");
        if (s == null || s.isEmpty() || s.equals("-1")) {
            // -XX:MaxDirectMemorySize not given, take default
            directMemory = Runtime.getRuntime().maxMemory();
        } else {
            long l = Long.parseLong(s);
            if (l > -1)
                directMemory = l;
        }

        // Check if direct buffers should be page aligned
        s = props.get("sun.nio.PageAlignDirectMemory");
        if ("true".equals(s))
            pageAlignDirectMemory = true;
    }

    //......
}
复制代码

VM的saveProperties方法读取sun.nio.MaxDirectMemorySize属性,如果为null或者是空或者是-1,那么则设置为Runtime.getRuntime().maxMemory();如果有设置MaxDirectMemorySize且值大于-1,那么使用该值作为directMemory的值;而VM的maxDirectMemory方法则返回的是directMemory的值

获取maxDirectMemory的值

实例

    public BufferPoolMXBean getDirectBufferPoolMBean(){
        return ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class)
                .stream()
                .filter(e -> e.getName().equals("direct"))
                .findFirst()
                .orElseThrow();
    }

    public JavaNioAccess.BufferPool getNioBufferPool(){
        return SharedSecrets.getJavaNioAccess().getDirectBufferPool();
    }

    /**
     * -XX:MaxDirectMemorySize=60M
     */
    @Test
    public void testGetMaxDirectMemory(){
        ByteBuffer.allocateDirect(25*1024*1024);
        System.out.println(Runtime.getRuntime().maxMemory() / 1024.0 / 1024.0);
        System.out.println(VM.maxDirectMemory() / 1024.0 / 1024.0);
        System.out.println(getDirectBufferPoolMBean().getTotalCapacity() / 1024.0 / 1024.0);
        System.out.println(getNioBufferPool().getTotalCapacity() / 1024.0 / 1024.0);
    }
复制代码

输出

输出结果如下:

4096.0
60.0
25.0
25.0
复制代码
  • 由于java9模块化之后,VM从原来的sun.misc.VM变更到java.base模块下的jdk.internal.misc.VM;上面代码默认是unamed module,要使用jdk.internal.misc.VM就需要使用--add-exports java.base/jdk.internal.misc=ALL-UNNAMED将其导出到UNNAMED,这样才可以运行
  • 同理java9模块化之后,SharedSecrets从原来的sun.misc.SharedSecrets变更到java.base模块下的jdk.internal.access.SharedSecrets;要使用--add-exports java.base/jdk.internal.access=ALL-UNNAMED将其导出到UNNAMED,这样才可以运行
  • 从输出结果可以看出,Runtime.getRuntime().maxMemory()输出的值是正确的,而BufferPoolMXBean及JavaNioAccess.BufferPool的getTotalCapacity返回的都是directBuffer大小,而非max值

使用API查看directBuffer使用情况

实例

    /**
     * -XX:MaxDirectMemorySize=60M
     */
    @Test
    public void testGetDirectMemoryUsage(){
        ByteBuffer.allocateDirect(30*1024*1024);
        System.out.println(getDirectBufferPoolMBean().getMemoryUsed() / 1024.0 / 1024.0);
        System.out.println(getNioBufferPool().getMemoryUsed() / 1024.0 / 1024.0);
    }
复制代码

输出

输出结果如下:

30.0
30.0
复制代码

可以看到BufferPoolMXBean及JavaNioAccess.BufferPool的getMemoryUsed可以返回directBuffer大小

OOM

java.lang.OutOfMemoryError: Direct buffer memory

	at java.base/java.nio.Bits.reserveMemory(Bits.java:175)
	at java.base/java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:118)
	at java.base/java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:317)
复制代码

如果上面的ByteBuffer.allocateDirect改为分配超过60M,则运行抛出OutOfMemoryError

使用NMT查看directBuffer使用情况

jcmd 3088 VM.native_memory scale=MB
3088:

Native Memory Tracking:

Total: reserved=5641MB, committed=399MB
-                 Java Heap (reserved=4096MB, committed=258MB)
                            (mmap: reserved=4096MB, committed=258MB)

-                     Class (reserved=1032MB, committed=6MB)
                            (classes #1609)
                            (  instance classes #1460, array classes #149)
                            (mmap: reserved=1032MB, committed=5MB)
                            (  Metadata:   )
                            (    reserved=8MB, committed=5MB)
                            (    used=3MB)
                            (    free=2MB)
                            (    waste=0MB =0.00%)
                            (  Class space:)
                            (    reserved=1024MB, committed=1MB)
                            (    used=0MB)
                            (    free=0MB)
                            (    waste=0MB =0.00%)

-                    Thread (reserved=18MB, committed=18MB)
                            (thread #18)
                            (stack: reserved=18MB, committed=18MB)

-                      Code (reserved=242MB, committed=7MB)
                            (mmap: reserved=242MB, committed=7MB)

-                        GC (reserved=203MB, committed=60MB)
                            (malloc=18MB #2443)
                            (mmap: reserved=185MB, committed=43MB)

-                  Internal (reserved=1MB, committed=1MB)
                            (malloc=1MB #1257)

-                     Other (reserved=30MB, committed=30MB)
                            (malloc=30MB #2)

-                    Symbol (reserved=1MB, committed=1MB)
                            (malloc=1MB #13745)

-        Shared class space (reserved=17MB, committed=17MB)
                            (mmap: reserved=17MB, committed=17MB)
复制代码

从Other部分可以看到其值跟ByteBuffer.allocateDirect使用的值一致,改变ByteBuffer.allocateDirect的值再重新查看,可以发现Other部分跟着改变;因而初步断定Other部分应该是可以反映direct memory的使用大小

小结

  • -XX:MaxDirectMemorySize=size用于设置New I/O(java.nio) direct-buffer allocations的最大大小,size的单位可以使用k/K、m/M、g/G;如果没有设置该参数则默认值为0,意味着JVM自己自动给NIO direct-buffer allocations选择最大大小;从代码java.base/jdk/internal/misc/VM.java中可以看到默认是取的Runtime.getRuntime().maxMemory()
  • 使用jdk.internal.misc.VM.maxDirectMemory()可以获取maxDirectMemory的值;由于java9模块化之后,VM从原来的sun.misc.VM变更到java.base模块下的jdk.internal.misc.VM;上面代码默认是unamed module,要使用jdk.internal.misc.VM就需要使用--add-exports java.base/jdk.internal.misc=ALL-UNNAMED将其导出到UNNAMED,这样才可以运行
  • BufferPoolMXBean及JavaNioAccess.BufferPool(通过SharedSecrets获取)的getMemoryUsed可以获取direct memory的大小;其中java9模块化之后,SharedSecrets从原来的sun.misc.SharedSecrets变更到java.base模块下的jdk.internal.access.SharedSecrets;要使用--add-exports java.base/jdk.internal.access=ALL-UNNAMED将其导出到UNNAMED,这样才可以运行

另外使用NMT也可以查看direct memory的使用情况,其包含在了Other部分

doc

转载于:https://juejin.im/post/5c9ced366fb9a070e344c614

<think>嗯,用户问的是关于JVM参数-XX:InitialHeapSize的作用和配置方法。这个问题涉及到JVM内存管理的核心知识,需要结合引用内容详细说明。 用户可能正在调优Java应用性能,或者遇到了内存相关的问题。从提问方式看,ta对JVM参数有一定了解但需要具体指导,属于中级开发者水平。需要特别注意引用[1]和[3]都提到了-Xms参数,这其实是关键点。 首先明确概念:InitialHeapSize就是堆内存的初始大小。引用[1]的示例输出显示了这个参数的实际效果,而引用[3]的调优示例用-Xms100m这种更常见的写法。这里要强调两种写法的等价性,因为很多开发者可能不知道-XX:InitialHeapSize和-Xms其实是同一个配置的两种写法。 配置方法需要分三部分说明:命令行配置、默认值规则、关联参数。特别要注意引用[4]提到的MaxDirectMemorySize关系,虽然用户没问但应该提醒避免内存总和超限。另外引用[2]提到的PrintFlagsInitial对验证默认值很有用。 注意事项部分要包含引用[1][2]都强调的版本差异问题,还有引用[4]隐含的生产环境风险。用户可能正在开发环境调试,但必须提前告知生产环境的注意事项。 最后生成的问题要覆盖延伸需求:性能分析(引用[3]的GC日志参数相关)、监控方法(引用[1]的打印参数技巧)、常见错误(结合引用[4]的内存溢出场景)。这样既解答当前问题,又预判了用户后续可能遇到的困难。</think>### JVM `-XX:InitialHeapSize` 参数详解 #### **作用** `-XX:InitialHeapSize` 用于设置 **JVM 堆内存的初始大小**。JVM 启动时,会立即分配此参数指定的内存量作为初始堆空间,避免运行时频繁调整堆大小,从而提升启动性能。若未显式配置,JVM 会根据系统内存自动计算默认值(通常为物理内存的 1/64)[^1][^3]。 #### **配置方法** 1. **命令行直接指定**(单位:字节/B,支持 `k`/`m`/`g` 后缀): ```bash java -XX:InitialHeapSize=512m -jar your_app.jar ``` 2. **等价简写参数 `-Xms`**(更常用): ```bash java -Xms512m -jar your_app.jar # 效果等同 -XX:InitialHeapSize=512m ``` #### **关键规则** - **与 `-Xmx` 的关系**: - 建议将初始堆大小(`-Xms`)和最大堆大小(`-Xmx`)设为相同值,避免运行时堆扩容引发性能波动[^3]。 - 示例:`-Xms2g -Xmx2g` 表示固定分配 2GB 堆内存。 - **默认值依赖**: 未设置时,JVM 基于系统内存自动计算,可通过 `-XX:+PrintFlagsFinal` 查看实际值[^1][^2]。 #### **验证配置生效** 添加 JVM 参数 `-XX:+PrintCommandLineFlags`,启动时输出生效值: ```bash java -XX:InitialHeapSize=512m -XX:+PrintCommandLineFlags -version # 输出示例: # -XX:InitialHeapSize=536870912 // 512MB 的字节值 # ...其他参数... ``` #### **注意事项** 1. **容器环境**: 在 Docker/K8s 中运行时,`-Xms` + `-XX:MaxDirectMemorySize`(堆外内存)的总和不可超过容器内存限制,否则可能触发 OOM Kill[^4]。 2. **生产建议**: - 避免依赖 JVM 默认值,显式配置 `-Xms` 和 `-Xmx` 以稳定性能[^3]。 - 结合 `-XX:+HeapDumpOnOutOfMemoryError` 在 OOM 时生成堆转储文件[^3]。 3. **参数兼容性**: 不同 JVM 版本(如 OpenJDK/OracleJDK)的默认值或行为可能差异,需测试验证[^2]。 --- ### 相关问题 1. **如何监控 JVM 堆内存的实际使用情况?** (涉及工具:`jstat`、GC 日志分析、`-Xlog:gc*` 参数等) 2. **`-Xms` 和 `-Xmx` 设置不一致会导致哪些性能问题?** (堆扩容/收缩的开销、GC 频率变化等) 3. **容器中如何为 JVM 分配合适的内存?** (堆内存、元空间、堆外内存的比例调整与风险规避[^4]) 4. **如何通过 GC 日志优化堆大小配置?** (分析 Full GC 频率、老年代占用等指标[^3]) > 引用来源: > [^1]: JVM 参数打印与实际应用场景说明 > [^2]: JVM 参数查询方法与兼容性注意事项 > [^3]: 生产环境内存调优参数示例与策略 > [^4]: 堆外内存配置与容器环境风险警告
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值