乐趣区

关于java:java代码获取内存信息

一、获取堆外内存

    @GetMapping("/panama")
    public Map<String, Object> panama() {ByteBuffer buffer = ByteBuffer.allocateDirect(1 * 1024 * 1024);
        Map<String, Object> map = new HashMap<>();
        try {MBeanServer mbs = ManagementFactory. getPlatformMBeanServer() ;
        ObjectName objectName = new ObjectName("java.nio:type=BufferPool,name=direct") ;
        MBeanInfo info = mbs.getMBeanInfo(objectName) ;
       
        for(MBeanAttributeInfo i : info.getAttributes()) {map.put(i.getName(), mbs.getAttribute(objectName , i.getName()));
        }
        } catch (Exception e) {map.put("error", e);
        }
        return map;
        
    }

输入后果

Name:direct
Count:1
TotalCapacity:1048576
MemoryUsed:1048576
ObjectName:java.nio:type=BufferPool,name=direct
退出移动版