codecache.jar

Code出处:http://hellojava.info/?p=119


# vi codecache.java    #名称应尽量与public class后面的名称一样。

import java.io.File;

 

import javax.management.MBeanServerConnection;

import javax.management.ObjectName;

import javax.management.remote.JMXConnector;

import javax.management.remote.JMXConnectorFactory;

import javax.management.remote.JMXServiceURL;

 

import com.sun.tools.attach.VirtualMachine;

 

public class codecache {

 

    private static final String CONNECTOR_ADDRESS = 

            "com.sun.management.jmxremote.localConnectorAddress";

 

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

        if(args.length != 1){

            System.err.println("Must enter one arg: pid");

            System.exit(0);

        }

        VirtualMachine vm = VirtualMachine.attach(args[0]);

        JMXConnector connector = null;

        try {

            String connectorAddress = vm.getAgentProperties().getProperty(

                    CONNECTOR_ADDRESS);

 

            if (connectorAddress == null) {

                String agent = vm.getSystemProperties()

                        .getProperty("java.home")

                        + File.separator

                        + "lib"

                        + File.separator + "management-agent.jar";

                vm.loadAgent(agent);

 

                connectorAddress = vm.getAgentProperties().getProperty(

                        CONNECTOR_ADDRESS);

            }

 

            JMXServiceURL url = new JMXServiceURL(connectorAddress);

            connector = JMXConnectorFactory.connect(url);

            MBeanServerConnection mbeanConn = connector.getMBeanServerConnection();

            ObjectName name = new ObjectName(

                    "java.lang:type=MemoryPool,name=Code Cache");

            System.out.println(mbeanConn.getAttribute(name, "Usage"));

        } 

        finally {

            if(connector != null)

                connector.close();

            vm.detach();

        }

    }

 

}

# javac -verbose codecache.java 

  [search path for class files............... /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75-2.5.4.2.el7_0.x86_64/jre/lib/ext/dnsns.jar.......

  codecache.java:9: package com.sun.tools.attach does not exist    #如果报此错误,则说明tools.jar包没找到。

  import com.sun.tools.attach.VirtualMachine;   

# ln -sv /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/lib/tools.jar /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/ext/tools.jar

# javac -verbose codecache.java               #编译通过。  

# java codecache JavaPid                      #测试是否能正常运行。



# jar -cvf codecache.jar codecache.class    #下面步骤用于生成jar文件。

  added manifest

  adding: codecache.class(in = 2298) (out= 1219)(deflated 46%)

# java -jar codecache.jar

  no main manifest attribute, in codecache.jar

# jar -xf codecache.jar

# cp /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75-2.5.4.2.el7_0.x86_64/jre/lib/ext/tools.jar ./

# vim META-INF/MANIFEST.MF 

  Manifest-Version: 1.0

  Created-By: 1.7.0_75 (Oracle Corporation)

  Class-Path: tools.jar

  Main-class: codecache           #添加此行,并在后面添加一个空行。


# rm -rf codecache.jar

# jar -cvfm codecache.jar META-INF/MANIFEST.MF codecache.class

# java -jar codecache.jar 27696

javax.management.openmbean.CompositeDataSupport(compositeType=javax.management.openmbean.CompositeType(name=java.lang.management.MemoryUsage,items=((itemName=committed,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=init,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=max,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=used,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)))),contents={committed=22085632, init=2555904, max=67108864, used=20885568})

上面的信息显示Code Cache区域初始化的时候为2555904,最大为67108864,已占用了22085632,使用了20885568。


你可能感兴趣的:(jvm,tomcat,code,cache,jboss,heap,non)