Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the size of object

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?

like image 251
Andrea Catania Avatar asked Dec 08 '13 09:12

Andrea Catania


People also ask

What is the size of the object?

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.

How do you find the size of the object of the class?

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.

How do I get the size of a Python object?

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.


1 Answers

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

like image 69
alexgirao Avatar answered Oct 10 '22 00:10

alexgirao