Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To get the object size using instrumentation, it is necessary to load an agent into the jvm, here is agent code and manifest</p> <p>Agent-MANIFEST.MF</p> <pre><code>Premain-Class: mypackage.Agent Agent-Class: mypackage.Agent Can-Retransform-Classes: true </code></pre> <p>Agent.java</p> <pre><code>/* Agent.java javac -cp ".:$JAVA_HOME/lib/tools.jar" -d . Agent.java Test.java &amp;&amp; \ 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; } } </code></pre> <p>the agent above allows you this</p> <p><code>GetObjectSizeTest.java</code></p> <pre><code>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("&lt;html&gt;"); writer.println("&lt;body bgcolor=white&gt;"); writer.println("&lt;p&gt;The size of System.in is " + Agent.inst.getObjectSize(System.in) + "&lt;/p&gt;"); writer.println("&lt;/body&gt;"); writer.println("&lt;/html&gt;"); } } </code></pre> <p>for this to work with tomcat and eclipse you may refer to <a href="https://stackoverflow.com/questions/6697063/adding-javaagent-to-tomcat-6-server-where-do-i-put-it-and-in-what-format">Adding -javaagent to Tomcat 6 server, where do I put it and in what format?</a> and <a href="https://stackoverflow.com/questions/13870225/how-to-set-jvm-arguments-in-tomcat-that-work-both-in-eclipse-and-using-the-start">How to set JVM arguments in tomcat that work both in eclipse and using the startup.bat</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload