Java进程堆外内存(off heap)大小

时间:2022-02-16 17:00:11

一、使用ByteBuffer.allocateDirect分配的off heap内存大小

本机进程

  • 在Jvisualvm中安装 Mbeans插件。然后查看java.nio/BufferPool/direct

    Java进程堆外内存(off heap)大小

  • 进程内使用代码获取

MBeanServer mbs = ManagementFactory. getPlatformMBeanServer() ;
ObjectName objectName = new ObjectName("java.nio:type=BufferPool,name=direct" ) ;
MBeanInfo info = mbs.getMBeanInfo(objectName) ;
for(MBeanAttributeInfo i : info.getAttributes()) {
System.out .println(i.getName() + ":" + mbs.getAttribute(objectName , i.getName()));
}

远程进程

  • JMX获取

    假设目标机器没有启动JMX,那么加入jvm參数:
-Dcom.sun.management.jmxremote.port=9999
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremotAe.ssl=false

重新启动进程

然后本机通过JMX连接訪问:

String jmxURL = "service:jmx:rmi:///jndi/rmi://10.125.6.204:9999/jmxrmi" ;
JMXServiceURL serviceURL = new JMXServiceURL(jmxURL);
Map map = new HashMap() ;
String[] credentials = new String[] { "monitorRole" , "QED" } ;
map.put( "jmx.remote.credentials" , credentials) ;
JMXConnector connector = JMXConnectorFactory. connect(serviceURL , map);
MBeanServerConnection mbsc = connector.getMBeanServerConnection() ;
ObjectName objectName = new ObjectName("java.nio:type=BufferPool,name=direct" ) ;
MBeanInfo mbInfo = mbsc.getMBeanInfo(objectName) ;
for(MBeanAttributeInfo i : mbInfo.getAttributes()) {
System.out .println(i.getName() + ":" + mbsc.getAttribute(objectName , i.getName()));
}

二、使用UnSafe反射调用直接申请的堆外内存

java无法获取。须要相似gperftools之类的工具查看,下次总结