通过JMX取本地JVM内存使用信息

阅读更多
import java.io.BufferedWriter;
import java.io.FileWriter;

import javax.management.openmbean.CompositeData;
import javax.management.remote.JMXServiceURL;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;

import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
import com.sun.tools.attach.spi.AttachProvider;

public class JVMRuntimeClient {
	public static void main(String[] args) throws Exception {

		try {
			final AttachProvider attachProvider = AttachProvider.providers()
					.get(0);

			VirtualMachineDescriptor descriptor = null;
			for (VirtualMachineDescriptor virtualMachineDescriptor : attachProvider
					.listVirtualMachines()) {
				if (pickThisOne(virtualMachineDescriptor)) {
					descriptor = virtualMachineDescriptor;
					break;
				}
			}

			if (descriptor == null)
				throw new RuntimeException("You didn't pick one");

			final VirtualMachine virtualMachine = attachProvider
					.attachVirtualMachine(descriptor);
			virtualMachine
					.loadAgent(
							"/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/lib/management-agent.jar",
							"com.sun.management.jmxremote");
			final Object portObject = virtualMachine.getAgentProperties().get(
					"com.sun.management.jmxremote.localConnectorAddress");

			final JMXServiceURL target = new JMXServiceURL(portObject + "");
			JMXConnector connector = JMXConnectorFactory.connect(target);
			MBeanServerConnection remote = connector.getMBeanServerConnection();

			int c = remote.getMBeanCount();

			System.out.println(c);

			
			BufferedWriter bfw = new BufferedWriter(new FileWriter("/home/ec2-user/jmx/heap.csv"));
			while (true) {
				CompositeData composite = (CompositeData) remote.getAttribute(
						new ObjectName("java.lang:type=Memory"),
						"HeapMemoryUsage");

				StringBuffer sb = new StringBuffer(composite.get("committed")
						.toString());

				sb.append(",").append(composite.get("init")).append(",")
						.append(composite.get("max"))
						.append(composite.get("used"));
				
				
				
				bfw.write(sb.toString());
				bfw.newLine();
				System.out.println(composite.get("committed"));
				System.out.println(composite.get("init"));
				System.out.println(composite.get("max"));
				System.out.println(composite.get("used"));

			}

			// connector.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}

	private static boolean pickThisOne(
			VirtualMachineDescriptor virtualMachineDescriptor) {
		System.out.println(virtualMachineDescriptor.displayName());
		if (virtualMachineDescriptor.displayName().equals(
				"org.codehaus.plexus.classworlds.launcher.Launcher test")
				|| virtualMachineDescriptor
						.displayName()
						.equals("org.codehaus.plexus.classworlds.launcher.Launcher -B test"))
			return true;
		// TODO
		return false;
	}
}

 

你可能感兴趣的:(通过JMX取本地JVM内存使用信息)