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

46次阅读

共计 603 个字符,预计需要花费 2 分钟才能阅读完成。

一、获取堆外内存

    @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

正文完
 0