It is easy to use intrumentation.

Source
import java.lang.instrument.Instrumentation;
import java.util.Calendar;
import java.util.HashMap;
public class Test {
 private static Instrumentation inst;
 public static void premain(String options, Instrumentation inst) {
  Test.inst = inst;
 }
 public static long sizeOf(Object o) {
  assert inst != null;
  return inst.getObjectSize(o);
 }
 public static void main(String[] args) {
  System.out.println("Size of Object: " + sizeOf(new Object()));
  System.out.println("Size of String: " + sizeOf(new String()));
  System.out.println("Size of String111: " + sizeOf(new String("111.111.111.111")));
  System.out.println("Size of StringBuffer: " + sizeOf(new String()));
  System.out.println("Size of HashMap: " + sizeOf(new HashMap()));
  System.out.println("Size of direct subclass: " + sizeOf(new Test()));
  System.out.println("Size of Calendar: " + sizeOf(Calendar.getInstance()));
 }
}


manifest.mf

Manifest-Version: 1.0
Premain-Class: com.google.laputa.sms.sample.Test
Main-Class: com.google.laputa.sms.sample.Test

jaring
$ jar cvfm test.jar test-classes/MANIFEST.MF -C test-classes .

execution
$ java -ea -javaagent:test.jar -jar test.jar

Result
Size of Object: 8
Size of String: 24
Size of String111: 24
Size of StringBuffer: 24
Size of HashMap: 40
Size of direct subclass: 8
Size of Calendar: 112


'java core' 카테고리의 다른 글

URLConnection vs HTTPURLConnection  (0) 2009.07.20
Finally 범위  (0) 2009.07.15
How to get object size in java.  (0) 2009.05.29
How to get List comparator using Comparator class.  (0) 2009.05.13
JMX reference  (0) 2009.04.29
Effective Java Reload  (0) 2009.04.29
Posted by 김용환 '김용환'

댓글을 달아 주세요