[JVM]了断局: 堆外内存无法 [ -XX:MaxDirectMemorySize ] 限制

一. 前言

今天看到一句话 , 有点懵, 所以验证一下.

使用sun.misc.Unsafe的allocateMemory方法分配堆外内存。不受-XX:MaxDirectMemorySize这个JVM参数的限制

二. 结论

上面的结论有点不完善:

  1. 如果通过反射的方式拿到Unsafe的实例,然后用Unsafe的allocateMemory方法分配堆外内存. 确实不受-XX:MaxDirectMemorySize这个JVM参数的限制 . 所以限制的内存大小为操作系统的内存.

  2. 如果使用Java自带的 ByteBuffer.allocateDirect(size) 或者直接 new DirectByteBuffer(capacity) , 这样受-XX:MaxDirectMemorySize 这个JVM参数的限制. 其实底层都是用的Unsafe#allocateMemory,区别是对大小做了限制. 如果超出限制直接OOM.

  3. 如果不设置-XX:MaxDirectMemorySize 默认的话,是跟堆内存大小保持一致. [堆内存大小如果不设置的话,默认为操作系统的 1/4, 所以 DirectMemory的大小限制JVM的Runtime.getRuntime().maxMemory()内存大小 . ], 代码入下:

sun.misc.VM

public static void saveAndRemoveProperties(Properties var0) {
        if (booted) {
            throw new IllegalStateException("System initialization has completed");
        } else {
            savedProps.putAll(var0);
            String var1 = (String)var0.remove("sun.nio.MaxDirectMemorySize");
            if (var1 != null) {
            
                if (var1.equals("-1")) {
                	// 默认值 -1 , 代表是JVM的最大内存 (注意,不是操作系统内存!!!!!!!!!). 
                    directMemory = Runtime.getRuntime().maxMemory();
                } else {
                    long var2 = Long.parseLong(var1);
                    if (var2 > -1L) {
                        directMemory = var2;
                    }
                }
            }

            var1 = (String)var0.remove("sun.nio.PageAlignDirectMemory");
            if ("true".equals(var1)) {
                pageAlignDirectMemory = true;
            }

            var1 = var0.getProperty("sun.lang.ClassLoader.allowArraySyntax");
            allowArraySyntax = var1 == null ? defaultAllowArraySyntax : Boolean.parseBoolean(var1);
            var0.remove("java.lang.Integer.IntegerCache.high");
            var0.remove("sun.zip.disableMemoryMapping");
            var0.remove("sun.java.launcher.diag");
            var0.remove("sun.cds.enableSharedLookupCache");
        }
    }

三. 代码验证: 反射获取Unsafe的allocateMemory方法

3.1. 测试代码


import com.sun.management.OperatingSystemMXBean;
import sun.misc.Unsafe;

import java.lang.management.ManagementFactory;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

public class DirectMemoryOOM_01 {
    // 内存限制失效
    // -Xms10m -Xmx10m -XX:MaxDirectMemorySize=10m
    public static void main(String[] args) throws Exception {

        Runtime rt = Runtime.getRuntime();
        long vmFree = 0;
        long vmUse = 0;
        long vmTotal = 0;
        long vmMax = 0;
        int byteToMb = 1024 * 1024;
        long size = 1024 * 1024 * 1024;


        Field f = Unsafe.class.getDeclaredFields()[0];
        f.setAccessible(true);
        Unsafe us = (Unsafe) f.get(null);

        List<Long> list = new ArrayList<Long>();



        while (true) {

            System.out.println("************ start ***************************");
            System.out.println("JVM maxDirectMemory : " + sun.misc.VM.maxDirectMemory() / byteToMb);


            vmTotal = rt.totalMemory() / byteToMb;
            vmFree = rt.freeMemory() / byteToMb;
            vmMax = rt.maxMemory() / byteToMb;
            vmUse = vmTotal - vmFree;
            System.out.println("JVM内存已用的空间为:" + vmUse + " MB");
            System.out.println("JVM内存的空闲空间为:" + vmFree + " MB");
            System.out.println("JVM总内存空间为:" + vmTotal + " MB");
            System.out.println("JVM总内存空间为:" + vmMax + " MB");

            System.out.println("======================================");
            // 操作系统级内存情况查询
            OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
            String os = System.getProperty("os.name");
            long physicalFree = osmxb.getFreePhysicalMemorySize() / byteToMb;
            long physicalTotal = osmxb.getTotalPhysicalMemorySize() / byteToMb;
            long physicalUse = physicalTotal - physicalFree;
            System.out.println("操作系统的版本:" + os);
            System.out.println("操作系统物理内存已用的空间为:" + physicalUse + " MB");
            System.out.println("操作系统物理内存的空闲空间为:" + physicalFree + " MB");
            System.out.println("操作系统总物理内存:" + physicalTotal + " MB");
            System.out.println("======================================");

            // 获得线程总数
            ThreadGroup parentThread;
            int totalThread = 0;
            for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
                    .getParent() != null; parentThread = parentThread.getParent()) {
                totalThread = parentThread.activeCount();
            }
            System.out.println("获得线程总数:" + totalThread);

            System.out.println("======================================");

            long p = us.allocateMemory(size);
            for (int i = 0; i < size; i++) {
                us.putByte(p + i, Byte.MAX_VALUE);
            }
            list.add(p);

            System.out.println("************ end ***************************");


            Thread.sleep(1000);
        }
    }
}

3.2.设置JVM参数

-Xms10m -Xmx10m -XX:MaxDirectMemorySize=10m

在这里插入图片描述

3.2.输出结果: 不报错,无法限制内存.


************ start ***************************
JVM maxDirectMemory : 10
JVM内存已用的空间为:2 MB
JVM内存的空闲空间为:7 MB
JVM总内存空间为:9 MB
JVM总内存空间为:9 MB
======================================
操作系统的版本:Mac OS X
操作系统物理内存已用的空间为:16368 MB
操作系统物理内存的空闲空间为:16 MB
操作系统总物理内存:16384 MB
======================================
获得线程总数:2
======================================

无限死循环中.....................
无限死循环中.....................
无限死循环中.....................
无限死循环中.....................
无限死循环中.....................
无限死循环中.....................
无限死循环中.....................


四. 代码验证: ByteBuffer.allocateDirect方式获取内存

4.1. 测试代码

package com.unsafe;

import sun.misc.Unsafe;

import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

public class DirectMemoryOOM_02 {
    // 内存限制失效
    // -Xms10m -Xmx10m -XX:MaxDirectMemorySize=10m
    public static void main(String[] args) throws Exception {

        Runtime rt = Runtime.getRuntime();
        long vmFree = 0;
        long vmUse = 0;
        long vmTotal = 0;
        long vmMax = 0;
        int byteToMb = 1024 * 1024;

        int size = 1024 * 1024 * 1024;

        List<ByteBuffer> list = new ArrayList<ByteBuffer>();


        while (true) {

            System.out.println("************ start ***************************");

            System.out.println("JVM maxDirectMemory : " + sun.misc.VM.maxDirectMemory() / byteToMb);

            vmTotal = rt.totalMemory() / byteToMb;
            vmFree = rt.freeMemory() / byteToMb;
            vmMax = rt.maxMemory() / byteToMb;
            vmUse = vmTotal - vmFree;
            System.out.println("JVM内存已用的空间为:" + vmUse + " MB");
            System.out.println("JVM内存的空闲空间为:" + vmFree + " MB");
            System.out.println("JVM总内存空间为:" + vmTotal + " MB");
            System.out.println("JVM总内存空间为:" + vmMax + " MB");

            System.out.println("======================================");
            // 操作系统级内存情况查询
            OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
            String os = System.getProperty("os.name");
            long physicalFree = osmxb.getFreePhysicalMemorySize() / byteToMb;
            long physicalTotal = osmxb.getTotalPhysicalMemorySize() / byteToMb;
            long physicalUse = physicalTotal - physicalFree;
            System.out.println("操作系统的版本:" + os);
            System.out.println("操作系统物理内存已用的空间为:" + physicalUse + " MB");
            System.out.println("操作系统物理内存的空闲空间为:" + physicalFree + " MB");
            System.out.println("操作系统总物理内存:" + physicalTotal + " MB");
            System.out.println("======================================");

            // 获得线程总数
            ThreadGroup parentThread;
            int totalThread = 0;
            for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
                    .getParent() != null; parentThread = parentThread.getParent()) {
                totalThread = parentThread.activeCount();
            }
            System.out.println("获得线程总数:" + totalThread);

            System.out.println("======================================");

            // 分配 Direct Memory
            ByteBuffer by =  ByteBuffer.allocateDirect(size);
            list.add(by);

            System.out.println("************ end ***************************");


            Thread.sleep(1000);
        }
    }
}

4.2. 设置JVM参数

-Xms10m -Xmx10m -XX:MaxDirectMemorySize=10m

在这里插入图片描述

4.3. 输出结果: OutOfMemoryError: Direct buffer memory

************ start ***************************
JVM maxDirectMemory : 10
JVM内存已用的空间为:2 MB
JVM内存的空闲空间为:7 MB
JVM总内存空间为:9 MB
JVM总内存空间为:9 MB
======================================
操作系统的版本:Mac OS X
操作系统物理内存已用的空间为:13986 MB
操作系统物理内存的空闲空间为:2398 MB
操作系统总物理内存:16384 MB
======================================
获得线程总数:1
======================================
Exception in thread "main" java.lang.OutOfMemoryError: Direct buffer memory
	at java.nio.Bits.reserveMemory(Bits.java:695)
	at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:123)
	at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:311)
	at com.unsafe.DirectMemoryOOM_02.main(DirectMemoryOOM_02.java:68)
Disconnected from the target VM, address: '127.0.0.1:58987', transport: 'socket'

  • 2
    点赞
  • 5
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:鲸 设计师:meimeiellie 返回首页
评论

打赏作者

张伯毅

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值