Get the size of object

jakarta-ee, java, opencv, tomcat

Solution

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

Problem

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?

Original source

Related problems