I would like to get the size of an object. I tried to use this method:
import java.lang.instrument.Instrumentation;
public class ObjectSizeFetcher {
private static Instrumentation instrumentation;
public static void premain(String args, Instrumentation inst) {
instrumentation = inst;
}
public static long getObjectSize(Object o) {
return instrumentation.getObjectSize(o);
}
}
But it thrown this error:
java.lang.NullPointerException
test.ObjectSizeFetcher.getObjectSize(ObjectSizeFetcher.java:13)
servlet.testObj.doGet(cms.java:55)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:394)
However I tryed jprofiler and MAT but I'm not able to find this object alive...
what can I do?
Size in general is the magnitude or dimensions of a thing. More specifically, geometrical size (or spatial size) can refer to linear dimensions (length, width, height, diameter, perimeter), area, or volume. Size can also be measured in terms of mass, especially when assuming a density range.
One way to get an estimate of an object's size in Java is to use getObjectSize(Object) method of the Instrumentation interface introduced in Java 5. As we could see in Javadoc documentation, the method provides “implementation-specific approximation” of the specified object's size.
In python, the usage of sys. getsizeof() can be done to find the storage size of a particular object that occupies some space in the memory. This function returns the size of the object in bytes.
To get the object size using instrumentation, it is necessary to load an agent into the jvm, here is agent code and manifest
Agent-MANIFEST.MF
Premain-Class: mypackage.Agent
Agent-Class: mypackage.Agent
Can-Retransform-Classes: true
Agent.java
/* Agent.java
javac -cp ".:$JAVA_HOME/lib/tools.jar" -d . Agent.java Test.java && \
jar cfm Agent.jar Agent-MANIFEST.MF mypackage/Agent.class
*/
package mypackage;
import java.lang.instrument.Instrumentation;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
public class Agent implements ClassFileTransformer {
public static Instrumentation inst;
public static void premain(String agentArgs, Instrumentation inst) {
Agent.inst = inst;
}
public static void agentmain(String agentArgs, Instrumentation inst) {
Agent.inst = inst;
}
public byte[] transform(ClassLoader loader, String className, Class redefiningClass, ProtectionDomain domain, byte[] bytes) throws IllegalClassFormatException {
/* returning null means we don't want to change a thing
*/
return null;
}
}
the agent above allows you this
GetObjectSizeTest.java
package mypackage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public final class GetObjectSizeTest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("<html>");
writer.println("<body bgcolor=white>");
writer.println("<p>The size of System.in is " + Agent.inst.getObjectSize(System.in) + "</p>");
writer.println("</body>");
writer.println("</html>");
}
}
for this to work with tomcat and eclipse you may refer to Adding -javaagent to Tomcat 6 server, where do I put it and in what format? and How to set JVM arguments in tomcat that work both in eclipse and using the startup.bat
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With