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

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

本机进程

  • 在Jvisualvm中安装 Mbeans插件,然后查看java.nio/BufferPool/direct
    Java进程堆外内存(off heap)大小_第1张图片

  • 进程内使用代码获取

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之类的工具查看,下次总结

你可能感兴趣的:(【原创】,编程语言,JVM)