title: Instrumenting java string object size gives incorrect result
body: <p>I am using the <code>java.lang.instrument.Instrumentation</code> package to calculate the size of a java string. I follow the steps as listed <a href="https://stackoverflow.com/a/52682/537503">here</a> to create a java agent jar. Below is the application code that calls the java agent class </p>
  
  <pre><code>public static void main(String[] args) {
      String string = "A very long string which I have trimmed for this example";
  
      StringBuilder sb = new StringBuilder(string);
      for (int i=0; i&lt;16000; i++) {
          sb.append(string);
      }
  
      printObjectSize(sb.toString());
  
  
  }
  
  private static long printObjectSize(Object object) {
      final long objectSize = ObjectSizeFetcher.getObjectSize(object);
      System.out.println("Object type: " + object.getClass() + ", size: " + objectSize + " bytes");
      return objectSize;
  }
  </code></pre>
  
  <p>Here <code>ObjectSizeFetcher</code> is the class in the java agent that calls the instrumentation code internally. Irrespective of how big the string is, I always get the output as</p>
  
  <pre><code>Object type: class java.lang.String, size: 24 bytes
  </code></pre>
  
  <p>What could be going wrong?</p>