Instrumentation:querying the memory usage of a Java object

Source: Internet
Author: User

Copy from:http://www.javamex.com/tutorials/memory/instrumentation.shtml

Instrumentation:querying the memory usage of a Java object

The most reliable-but isn't necessarily the easiest-way to estimate the usage of a Java object are to ask the JVM. Querying the JVM for the memory usage of a object requires the instrumentation Framework introduced in Java 5. Hotspot supports the instrumentation framework; If you use a VM from a different vendor, it'll need to provide similar support, and there could be some variation in the P Rocedures described here. The general idea is:

    • We create a special agent class with a special Premain method, generally compiling it against the rest O F Our code so, it can see the definitions of the classes whose memory usage we ' re interested in measuring;
    • The JVM passes a special instrumentation object to our Premain method, which we can use to query the Siz E of an object;
    • The compiled agent class into a jar with a special manifest file;
    • We run the passing in the agent Jar to the VM command line arguments.

On the rest of this page, we'll go through the above procedure step by step.

1. Creating the Instrumentation AgentClass

An instrumentation agent was a class with a special method, with a predefined signature, that the JVM would invoke Before the rest of the application for you and set up any instrumentation code. Generally, instrumentation is the act of transforming classes for profiling Purposes:for example, we could manip Ulate the definition of the string class to increment a counter every time a string is created, and thus measure e.g. how many strings per second our application creates. But a interesting additional feature provided by the instrumentation framework are the ability to measure the memory usage of an object.

Our agents are simply a class with the following method defined:

public static void Premain (String args, Instrumentation inst) {  ...}

The JVM would pass to our method a implementation of the instrumentation interface, defined in java.lang.inst Rument. In turn, this interface defines the method getobjectsize (). So for example, if we want to measure the memory usage of a instance of SomeClass, our agents code would look as follows:

Import Java.lang.instrument.*;import Com.somepackage.someclass;public class Myagent {public  static void Premain ( String args, Instrumentation inst) {    SomeClass obj = new SomeClass ();    Long size = inst.getobjectsize (obj);    System.out.println ("Bytes used by object:" + size);}  }

Note that there ' s no interface, our agent needs to define:we just need to make sure, that we get the method signature Correct so, the JVM would find it.

2. Package the agent into a jar

Once we have compiled our agents class, we need to the package it into a jar. This step is slightly fiddly, because we also need to create a manifest file. The latter is simple a text file containing a single line that specifies the agent class. For example, you can create a file called Manifest.txt with the following line:

Premain-class:mypackage. Myagent

Then, to create the jar, we execute the following command (it's usually worth creating a batch file or shell script with T Your need to re-build the agent Jar several times):

JAR-CMF manifest.txt Agent.jar Mypackage/myagent.class
3. Run the application with the agent

Now, we execute the application as usual, but use the javaagent command line argument to specify that we want to Attach our instrumentation agent. For example, assuming the classpath are the current directory and that the application ' s main method are in com . MyPackage. Main:

-javaagent:agent.jar -CP. Com.mypackage.Main

Now, before our application are run, our agent's Premain method would be run. The size of an instance of SomeClass would be created.

Accessing theInstrumentationObject from within our application

In our simple example above, we measured the size of an object created during the execution of the  Premain method. If we want to measure the size of an object  during the lifetime of the application ? Well, we can still does so; We just need to arrange for the rest of our application to see the instrumentation  object passed to the  premain  method. This works because the single agent class (and the  instrumentation  instance) created at startup remains Valid throughout the application. So we can simple store the  instrumentation  instance in a static variable and then provide a static method To access it. For example:

public class Myagent {  private static volatile instrumentation globalinstr;  public static void Premain (String args, Instrumentation inst) {    globalinstr = inst;  }  Public static Long getobjectsize (Object obj) {    if (globalinstr = = null)      throw new IllegalStateException ("Agent not initted");    globalinstr.getobjectsize (obj);}  }

Now, provided the agent was included in the JVM command line parameters as above and then from anywhere in our application we Can callmyagent.getobjectsize () to query the memory size of a object created by our Java application.

Calculating "Deep" memory usage of a object

Note that the getobjectsize () method does does include the memory used by other objects referenced by the object passed in. For example, if object A had a reference to object B and then object a ' s reported memory usage would include only the bytes NE Eded for the reference to Object B (usually 4 bytes), not the actual object.

To get a ' deep ' count of the memory usage of a object (i.e. which includes "subobjects" or objects referred to by the "Ma In "Object" and then your can use the Classmexer agent available for beta download from this site.

Instrumentation:querying the memory usage of a Java object

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.