ArcGIS API for Javascript (七)

LeetCode——剑指 Offer 52. 两个链表的第一个公共节点

  返回  

Java使用HotSpotVirtualMachine和JMXServiceURL获取JVM进程的内存信息

2021/7/21 8:32:08 浏览:
package com.jvm;

import com.sun.tools.attach.VirtualMachine;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import sun.tools.attach.*;

import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.io.File;
import java.io.InputStream;
import java.lang.management.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

public class JvmUtils {
	/*  获取 jvm 信息 */
    public static String attachJmx(String pid){
        VirtualMachine virtualMachine = null ;
        try{
            virtualMachine = VirtualMachine.attach(pid) ;
            HotSpotVirtualMachine machine = (HotSpotVirtualMachine) virtualMachine ;
            String javaHome = virtualMachine.getSystemProperties().getProperty("java.home");

            String jmxAgent = javaHome + File.separator + "lib" + File.separator + "management-agent.jar";
            virtualMachine.loadAgent(jmxAgent, "com.sun.management.jmxremote");
            Properties properties = virtualMachine.getAgentProperties();
            String address = (String) properties.get("com.sun.management.jmxremote.localConnectorAddress");
            virtualMachine.detach();
            return address;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null ;
    }
	/* 获取 jstack 堆信息*/
    public static String getStack(String pid){
        VirtualMachine virtualMachine = null ;
        InputStream is = null ;
        try{
            virtualMachine = VirtualMachine.attach(pid) ;
            HotSpotVirtualMachine machine = (HotSpotVirtualMachine) virtualMachine ;
            is = machine.remoteDataDump(new String[]{});
            return IOUtils.toString(is , StandardCharsets.UTF_8.name());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            IOUtils.closeQuietly(is);
        }
        return null ;
    }
	/*  获取 heapHisto 信息 */
    public static String heapHisto(String pid){
        VirtualMachine virtualMachine = null ;
        InputStream is = null ;
        try{
            virtualMachine = VirtualMachine.attach(pid) ;
            HotSpotVirtualMachine machine = (HotSpotVirtualMachine) virtualMachine ;
            is = machine.heapHisto(new String[]{});
            return IOUtils.toString(is , StandardCharsets.UTF_8.name());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            IOUtils.closeQuietly(is);
        }
        return null ;
    }
	/* 获取各种内存信息,  jmap -heap pid */
    public static void getMemory(String pid ){
        String address = attachJmx(pid);
        try {
            JMXServiceURL target = new JMXServiceURL(address);
            final JMXConnector connector = JMXConnectorFactory.connect(target);
            final MBeanServerConnection mbsc = connector.getMBeanServerConnection();

            TimeUnit.SECONDS.sleep(1);

            List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getPlatformMXBeans(mbsc , MemoryPoolMXBean.class);
            for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
                String name = memoryPoolMXBean.getName();
                String[] memoryManagerNames = memoryPoolMXBean.getMemoryManagerNames();
                MemoryType type = memoryPoolMXBean.getType();
                MemoryUsage usage = memoryPoolMXBean.getUsage();
                MemoryUsage peakUsage = memoryPoolMXBean.getPeakUsage();

                System.out.println(name + ":");
                System.out.println("    managers: " + StringUtils.join(memoryManagerNames , " , "));
                System.out.println("    type: " + type.toString());
                System.out.print("    usage: " + usage.toString());
                System.out.println("    Percentage: " + ((double) usage.getUsed() / usage.getCommitted() ) );
                System.out.println("    peakUsage: "  +peakUsage.toString());

                System.out.println("");
            }

        }catch (Exception e){
            e.printStackTrace();
        }


    }

    public static void main(String[] args) {
       // 传递进程ID
        getMemory("3112" );
    }
}

联系我们

如果您对我们的服务有兴趣,请及时和我们联系!

服务热线:18288888888
座机:18288888888
传真:
邮箱:888888@qq.com
地址:郑州市文化路红专路93号