Example usage for java.lang.management ManagementFactory MEMORY_MXBEAN_NAME

List of usage examples for java.lang.management ManagementFactory MEMORY_MXBEAN_NAME

Introduction

In this page you can find the example usage for java.lang.management ManagementFactory MEMORY_MXBEAN_NAME.

Prototype

String MEMORY_MXBEAN_NAME

To view the source code for java.lang.management ManagementFactory MEMORY_MXBEAN_NAME.

Click Source Link

Document

String representation of the ObjectName for the MemoryMXBean .

Usage

From source file:de.unisb.cs.st.javalanche.mutation.runtime.jmx.MutationMxClient.java

public static boolean connect(int i) {
    JMXConnector jmxc = null;//w  w  w .ja v  a 2s  . c  o m
    JMXServiceURL url = null;

    try {
        url = new JMXServiceURL(MXBeanRegisterer.ADDRESS + i);
        jmxc = JMXConnectorFactory.connect(url, null);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        return false;
        // System.out.println("Could not connect to address: " + url);
        // e.printStackTrace();
    }
    if (jmxc != null) {
        try {
            MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
            ObjectName objectName = new ObjectName(MXBeanRegisterer.OBJECT_NAME);
            Object numberOfMutations = mbsc.getAttribute(objectName, "NumberOfMutations");
            Object currentTest = mbsc.getAttribute(objectName, "CurrentTest");
            Object currentMutation = mbsc.getAttribute(objectName, "CurrentMutation");
            Object allMutations = mbsc.getAttribute(objectName, "Mutations");
            Object mutationsDuration = mbsc.getAttribute(objectName, "MutationDuration");
            Object testDuration = mbsc.getAttribute(objectName, "TestDuration");
            //            Object mutationSummary = mbsc.getAttribute(objectName,
            //                  "MutationSummary");

            final RuntimeMXBean remoteRuntime = ManagementFactory.newPlatformMXBeanProxy(mbsc,
                    ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);

            final MemoryMXBean remoteMemory = ManagementFactory.newPlatformMXBeanProxy(mbsc,
                    ManagementFactory.MEMORY_MXBEAN_NAME, MemoryMXBean.class);
            System.out.print("Connection: " + i + "  ");
            System.out.println("Target VM: " + remoteRuntime.getName() + " - " + remoteRuntime.getVmVendor()
                    + " - " + remoteRuntime.getSpecVersion() + " - " + remoteRuntime.getVmVersion());
            System.out.println(
                    "Running for: " + DurationFormatUtils.formatDurationHMS(remoteRuntime.getUptime()));
            System.out
                    .println("Memory usage: Heap - " + formatMemory(remoteMemory.getHeapMemoryUsage().getUsed())
                            + "  Non Heap - " + formatMemory(remoteMemory.getNonHeapMemoryUsage().getUsed()));

            String mutationDurationFormatted = DurationFormatUtils
                    .formatDurationHMS(Long.parseLong(mutationsDuration.toString()));
            String testDurationFormatted = DurationFormatUtils
                    .formatDurationHMS(Long.parseLong(testDuration.toString()));
            if (DEBUG_ADD) {
                System.out.println("Classpath: " + remoteRuntime.getClassPath());
                System.out.println("Args: " + remoteRuntime.getInputArguments());
                System.out.println("All Mutations: " + allMutations);
            }
            //            System.out.println(mutationSummary);
            System.out.println(
                    "Current mutation (Running for: " + mutationDurationFormatted + "): " + currentMutation);
            System.out.println("Mutations tested: " + numberOfMutations);
            System.out.println("Current test:   (Running for: " + testDurationFormatted + "): " + currentTest);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MalformedObjectNameException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (AttributeNotFoundException e) {
            e.printStackTrace();
        } catch (InstanceNotFoundException e) {
            e.printStackTrace();
        } catch (MBeanException e) {
            e.printStackTrace();
        } catch (ReflectionException e) {
            e.printStackTrace();
        } finally {
            try {
                jmxc.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return true;
}

From source file:org.apache.cassandra.tools.NodeProbe.java

/**
 * Create a connection to the JMX agent and setup the M[X]Bean proxies.
 *
 * @throws IOException on connection failures
 *//*from   w w  w  .j ava2s .  c om*/
private void connect() throws IOException {
    JMXServiceURL jmxUrl = new JMXServiceURL(String.format(fmtUrl, host, port));
    Map<String, Object> env = new HashMap<String, Object>();
    if (username != null) {
        String[] creds = { username, password };
        env.put(JMXConnector.CREDENTIALS, creds);
    }

    env.put("com.sun.jndi.rmi.factory.socket", getRMIClientSocketFactory());

    jmxc = JMXConnectorFactory.connect(jmxUrl, env);
    mbeanServerConn = jmxc.getMBeanServerConnection();

    try {
        ObjectName name = new ObjectName(ssObjName);
        ssProxy = JMX.newMBeanProxy(mbeanServerConn, name, StorageServiceMBean.class);
        name = new ObjectName(MessagingService.MBEAN_NAME);
        msProxy = JMX.newMBeanProxy(mbeanServerConn, name, MessagingServiceMBean.class);
        name = new ObjectName(StreamManagerMBean.OBJECT_NAME);
        streamProxy = JMX.newMBeanProxy(mbeanServerConn, name, StreamManagerMBean.class);
        name = new ObjectName(CompactionManager.MBEAN_OBJECT_NAME);
        compactionProxy = JMX.newMBeanProxy(mbeanServerConn, name, CompactionManagerMBean.class);
        name = new ObjectName(FailureDetector.MBEAN_NAME);
        fdProxy = JMX.newMBeanProxy(mbeanServerConn, name, FailureDetectorMBean.class);
        name = new ObjectName(CacheService.MBEAN_NAME);
        cacheService = JMX.newMBeanProxy(mbeanServerConn, name, CacheServiceMBean.class);
        name = new ObjectName(StorageProxy.MBEAN_NAME);
        spProxy = JMX.newMBeanProxy(mbeanServerConn, name, StorageProxyMBean.class);
        name = new ObjectName(HintedHandOffManager.MBEAN_NAME);
        hhProxy = JMX.newMBeanProxy(mbeanServerConn, name, HintedHandOffManagerMBean.class);
        name = new ObjectName(GCInspector.MBEAN_NAME);
        gcProxy = JMX.newMBeanProxy(mbeanServerConn, name, GCInspectorMXBean.class);
    } catch (MalformedObjectNameException e) {
        throw new RuntimeException("Invalid ObjectName? Please report this as a bug.", e);
    }

    memProxy = ManagementFactory.newPlatformMXBeanProxy(mbeanServerConn, ManagementFactory.MEMORY_MXBEAN_NAME,
            MemoryMXBean.class);
    runtimeProxy = ManagementFactory.newPlatformMXBeanProxy(mbeanServerConn,
            ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
}

From source file:org.kuali.test.runner.execution.TestExecutionContext.java

private void writePerformanceData(TestOperation op, long operationElaspedTime) {
    if (isPerformanceDataRequired()) {

        if (performanceData == null) {
            performanceData = new ArrayList<String[]>();
        }//from w ww  .  j  a va 2 s  .c om

        String submitElementName = getLastHttpSubmitElementName();
        ;

        if (op.getOperationType().equals(TestOperationType.HTTP_REQUEST)) {
            String[] rec = getInitializedPerformanceDataRecord();
            rec[8] = Constants.PERFORMANCE_ATTRIBUTE_TYPE_CLIENT;
            rec[9] = Constants.CLIENT_PERFORMANCE_ATTRIBUTE_HTTP_RESPONSE_TIME;
            rec[10] = Constants.PRIMITIVE_LONG_TYPE;
            rec[11] = "" + operationElaspedTime;
            rec[12] = submitElementName;
            rec[13] = (op.getOperation().getHtmlRequestOperation().getMethod() + ": "
                    + op.getOperation().getHtmlRequestOperation().getUrl());
            performanceData.add(rec);
        }

        JmxConnection jmxconn = Utils.findJmxConnection(configuration, platform.getJmxConnectionName());

        if ((jmxconn != null) && (jmxconn.getPerformanceMonitoringAttributes() != null)) {
            JMXConnector conn = null;
            try {
                conn = Utils.getJMXConnector(configuration, jmxconn);
                if (conn != null) {
                    MBeanServerConnection mbeanconn = conn.getMBeanServerConnection();
                    for (PerformanceMonitoringAttribute att : jmxconn.getPerformanceMonitoringAttributes()
                            .getPerformanceMonitoringAttributeArray()) {

                        Object value = "";
                        if (ManagementFactory.MEMORY_MXBEAN_NAME.equals(att.getType())) {
                            MemoryMXBean mbean = ManagementFactory.newPlatformMXBeanProxy(mbeanconn,
                                    ManagementFactory.MEMORY_MXBEAN_NAME, MemoryMXBean.class);
                            value = getValueFromMXBeanObject(att.getName(), mbean);
                        } else if (ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME.equals(att.getType())) {
                            OperatingSystemMXBean mbean = ManagementFactory.newPlatformMXBeanProxy(mbeanconn,
                                    att.getType(), OperatingSystemMXBean.class);
                            value = getValueFromMXBeanObject(att.getName(), mbean);
                        } else if (ManagementFactory.THREAD_MXBEAN_NAME.equals(att.getType())) {
                            ThreadMXBean mbean = ManagementFactory.newPlatformMXBeanProxy(mbeanconn,
                                    att.getType(), ThreadMXBean.class);
                            value = getValueFromMXBeanObject(att.getName(), mbean);
                        }

                        if ((value != null) && StringUtils.isNotBlank(value.toString())) {
                            String[] rec = getInitializedPerformanceDataRecord();

                            int pos = att.getType().indexOf(Constants.SEPARATOR_EQUALS);

                            if (pos > -1) {
                                rec[8] = att.getType().substring(pos + 1);
                            } else {
                                rec[8] = att.getType();
                            }
                            rec[9] = att.getName();
                            rec[10] = att.getType();
                            rec[11] = value.toString();
                            rec[12] = submitElementName;
                            rec[13] = att.getDescription();

                            performanceData.add(rec);
                        }
                    }
                }
            }

            catch (Exception ex) {
                LOG.error(ex.toString(), ex);
            }

            finally {
                if (conn != null) {
                    try {
                        conn.close();
                    }

                    catch (Exception ex) {
                    }
                    ;
                }
            }
        }
    }
}

From source file:org.talend.designer.runtime.visualization.views.RuntimeGraphcsComposite.java

private void loadOverviewChartSet(IActiveJvm activeJvm) throws JvmCoreException {
    final int[] blue = new int[] { 0, 255, 255 };
    final int[] red = new int[] { 255, 0, 0 };
    final int[] green = new int[] { 0, 255, 0 };
    final int[] lightgeen = new int[] { 128, 255, 0 };

    final int[] darkRed = new int[] { 255, 0, 128 };

    final int[] orange = new int[] { 255, 255, 0 };
    IMBeanServer server = activeJvm.getMBeanServer();
    server.getMonitoredAttributeGroups().clear();

    IMonitoredMXBeanGroup group = server.addMonitoredAttributeGroup(MonitorAttributeName.HEAP_MEMORY,
            AxisUnit.MBytes);//from w ww. j  a  v a2  s  .co m

    group.addAttribute(ManagementFactory.MEMORY_MXBEAN_NAME, MonitorAttributeName.HEAP_MEMORY_USE, green);

    group.addAttribute(ManagementFactory.MEMORY_MXBEAN_NAME, MonitorAttributeName.HEAP_MEMORY_SIZE, darkRed);

    group.addAttribute(ManagementFactory.MEMORY_MXBEAN_NAME, MonitorAttributeName.HEAP_MEMORY_NINTY, blue);

    group.addAttribute(ManagementFactory.MEMORY_MXBEAN_NAME, MonitorAttributeName.HEAP_MEMORY_THREE_QUARTER,
            orange);

    group = server.addMonitoredAttributeGroup(MonitorAttributeName.CPU_USE, AxisUnit.Percent);
    group.addAttribute(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME, MonitorAttributeName.CPU_TIME,
            lightgeen);
}

From source file:org.wso2.carbon.cassandra.cluster.ClusterDataAccessMBeanImplementation.java

/**
 * Access the <code>MemoryMXBean</code>
 *
 * @return <code>MemoryMXBean</code> instance
 *//*  w w w  .j  av  a 2 s. c om*/
public MemoryMXBean locateMemoryMBean() {
    if (memoryMXBean == null) {
        memoryMXBean = locateManagementFactoryMBean(ManagementFactory.MEMORY_MXBEAN_NAME, MemoryMXBean.class);

    }
    return memoryMXBean;
}

From source file:test.integ.be.fedict.hsm.MonitoringTest.java

@Test
public void testJMXConnection() throws Exception {
    JMXServiceURL jmxServiceURL = new JMXServiceURL("service:jmx:remoting-jmx://localhost:9999");
    JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxServiceURL);
    MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();
    MemoryMXBean memoryMXBean = ManagementFactory.newPlatformMXBeanProxy(connection,
            ManagementFactory.MEMORY_MXBEAN_NAME, MemoryMXBean.class);
    MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
    LOG.debug("used heap memory: " + heapMemoryUsage.getUsed());
}