sizeof.agent.SizeOfAgent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sizeofag Show documentation
Show all versions of sizeofag Show documentation
sizeofag is a Java Agent that allows you to determine the size of Java objects from within the JVM at runtime. This makes it very useful for developing Java frameworks that take memory constraints into account.
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
package sizeof.agent;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Stack;
/**
* Instrumentation agent used.
*
* This version merely outputs an error message on the commandline if the agent
* is not present instead of throwing an exception.
*
* @author Maxim Zakharenkov
* @author FracPete (fracpete at waikato dot ac dot nz)
*/
public class SizeOfAgent {
static Instrumentation inst;
static Boolean messageDisplayed;
/**
* initializes agent.
*
* @param agentArgs the arguments (ignored)
* @param instP the instrumentation to use
*/
public static void premain(String agentArgs, Instrumentation instP) {
inst = instP;
}
/**
* Returns object size without member sub-objects.
* @param o object to get size of
* @return object size, 0 is agent not present
*/
public static long sizeOf(Object o) {
if(inst == null) {
if (messageDisplayed == null) {
messageDisplayed = true;
System.err.println("Can not access instrumentation environment.\n" +
"Please check if jar file containing SizeOfAgent class is \n" +
"specified in the java's \"-javaagent\" command line argument.");
}
return 0;
}
return inst.getObjectSize(o);
}
/**
* Calculates full size of object iterating over
* its hierarchy graph.
* @param obj object to calculate size of
* @return object size
*/
public static long fullSizeOf(Object obj) {
Map
© 2015 - 2024 Weber Informatics LLC | Privacy Policy