for (VirtualMachineDescriptor vmd : VirtualMachine.list()) {
VirtualMachine vm = VirtualMachine.attach(vmd);
String javaHome = vm.getSystemProperties().getProperty("java.home");
File managementAgentJarFile = new File(javaHome + File.separator + "lib" + File.separator + "management-agent.jar");
vm.loadAgent(managementAgentJarFile.getAbsolutePath());
String address = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress", null);
JMXServiceURL jmxUrl = new JMXServiceURL(address);
MBeanServerConnection conn = JMXConnectorFactory.connect(jmxUrl).getMBeanServerConnection();
MemoryMXBean memBean = ManagementFactory.newPlatformMXBeanProxy(conn,
ManagementFactory.MEMORY_MXBEAN_NAME, MemoryMXBean.class);
RuntimeMXBean rtBean = ManagementFactory.newPlatformMXBeanProxy(conn,
ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
int memUsage = (int)(100.0 * memBean.getHeapMemoryUsage().getUsed() / memBean.getHeapMemoryUsage().getMax());
long memMB = memBean.getHeapMemoryUsage().getMax() / 1024 / 1024;
System.out.println(rtBean.getSystemProperties().get("user.dir")+ " => " + memUsage +"% mem, max="+memMB+"MB");
}
piątek, maja 23, 2014
Subskrybuj:
Komentarze do posta (Atom)
0 komentarze:
Prześlij komentarz