on
Italy
- Get link
- X
- Other Apps
Returns an implementation-specific approximation of the amount of storage consumed by the specified object. The result may include some or all of the object's overhead, and thus is useful for comparison within an implementation but not between implementations. The estimate may change during a single invocation of the JVM.This description tells us what the method does (provides an "implementation-specific approximation" of the specified object's size), its potential inclusion of overhead in the approximated size, and its potentially different values during a single JVM invocation.
Premain-Class
for the command-line approach and Agent-Class
for the post-JVM startup approach. The agent class requires a specific method be implemented for either case: premain
for command-line startup or agentmain
forpost JVM startup.premain
(command-line agent) method and a agentmain
(post JVM startup agent) method, though only the premain
will be demonstrated in this post.package dustin.examples;
import static java.lang.System.out;
import java.lang.instrument.Instrumentation;
/**
* Simple example of an Instrumentation Agent adapted from blog post
* "Instrumentation: querying the memory usage of a Java object"
* (http://www.javamex.com/tutorials/memory/instrumentation.shtml).
*/
public class InstrumentationAgent
{
/** Handle to instance of Instrumentation interface. */
private static volatile Instrumentation globalInstrumentation;
/**
* Implementation of the overloaded premain method that is first invoked by
* the JVM during use of instrumentation.
*
* @param agentArgs Agent options provided as a single String.
* @param inst Handle to instance of Instrumentation provided on command-line.
*/
public static void premain(final String agentArgs, final Instrumentation inst)
{
out.println("premain...");
globalInstrumentation = inst;
}
/**
* Implementation of the overloaded agentmain method that is invoked for
* accessing instrumentation of an already running JVM.
*
* @param agentArgs Agent options provided as a single String.
* @param inst Handle to instance of Instrumentation provided on command-line.
*/
public static void agentmain(String agentArgs, Instrumentation inst)
{
out.println("agentmain...");
globalInstrumentation = inst;
}
/**
* Provide the memory size of the provided object (but not it's components).
*
* @param object Object whose memory size is desired.
* @return The size of the provided object, not counting its components
* (described in Instrumentation.getObjectSize(Object)'s Javadoc as "an
* implementation-specific approximation of the amount of storage consumed
* by the specified object").
* @throws IllegalStateException Thrown if my Instrumentation is null.
*/
public static long getObjectSize(final Object object)
{
if (globalInstrumentation == null)
{
throw new IllegalStateException("Agent not initialized.");
}
return globalInstrumentation.getObjectSize(object);
}
}
The agent class above exposes a statically available method for accessing Instrumentation.getObjectSize(Object)
. The next code listing demonstrates a simple 'application' that makes use of it.package dustin.examples;
import static java.lang.System.out;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
/**
* Build up some sample objects and throw them at the Instrumentation example.
*
* Might run this class as shown next:
* java -javaagent:dist\agent.jar -cp dist\agent.jar dustin.examples.InstrumentSampleObjects
*
* @author Dustin
*/
public class InstrumentSampleObjects
{
public enum Color
{
RED,
WHITE,
YELLOW
}
/**
* Print basic details including size of provided object to standard output.
*
* @param object Object whose value and size are to be printed to standard
* output.
*/
public static void printInstrumentationSize(final Object object)
{
out.println(
"Object of type '" + object.getClass() + "' has size of "
+ InstrumentationAgent.getObjectSize(object) + " bytes.");
}
/**
* Main executable function.
*
* @param arguments Command-line arguments; none expected.
*/
public static void main(final String[] arguments)
{
final StringBuilder sb = new StringBuilder(1000);
final boolean falseBoolean = false;
final int zeroInt = 0;
final double zeroDouble = 0.0;
final Long zeroLong = 0L;
final long zeroLongP = 0L;
final Long maxLong = Long.MAX_VALUE;
final Long minLong = Long.MIN_VALUE;
final long maxLongP = Long.MAX_VALUE;
final long minLongP = Long.MIN_VALUE;
final String emptyString = "";
final String string = "ToBeOrNotToBeThatIsTheQuestion";
final String[] strings = {emptyString, string, "Dustin"};
final String[] moreStrings = new String[1000];
final List someStrings = new ArrayList();
final EmptyClass empty = new EmptyClass();
final BigDecimal bd = new BigDecimal("999999999999999999.99999999");
final Calendar calendar = Calendar.getInstance();
printInstrumentationSize(sb);
printInstrumentationSize(falseBoolean);
printInstrumentationSize(zeroInt);
printInstrumentationSize(zeroDouble);
printInstrumentationSize(zeroLong);
printInstrumentationSize(zeroLongP);
printInstrumentationSize(maxLong);
printInstrumentationSize(maxLongP);
printInstrumentationSize(minLong);
printInstrumentationSize(minLongP);
printInstrumentationSize(maxLong);
printInstrumentationSize(maxLongP);
printInstrumentationSize(emptyString);
printInstrumentationSize(string);
printInstrumentationSize(strings);
printInstrumentationSize(moreStrings);
printInstrumentationSize(someStrings);
printInstrumentationSize(empty);
printInstrumentationSize(bd);
printInstrumentationSize(calendar);
printInstrumentationSize(Color.WHITE);
}
}
dustin.examples.InstrumentationAgent
). Although I only need the Premain-class
entry for the command-line startup of the agent, I have included Agent-class
as an example of how to use the post JVM startup agent. It doesn't hurt
anything to have both present just as it did not hurt anything to have
both premain
and agentmain
methods defined in
the object class. There are prescribed rules for which of these is first
attempted based on the type of agent being used.Premain-class: dustin.examples.InstrumentationAgent Agent-class: dustin.examples.InstrumentationAgent
-javaagent
):java -javaagent:dist\Instrumentation.jar -cp Instrumentation.jar dustin.examples.InstrumentSampleObjects
Comments
Post a Comment