Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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?
–
–
–
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
The manifest of the agent JAR file must contain the attribute Premain-Class
. The value of this attribute is the name of the agent class. The agent class must implement a public static premain
method similar in principle to the main application entry point.
Java Agents cannot be contributed to an already running JVM; the premain
method is called before the main
method, again as clearly documented:
After the Java Virtual Machine (JVM) has initialized, each premain
method will be called in the order the agents were specified, then the real application main
method will be called.
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.