When running
java -javaagent:ObjectSizeFetcherAgent.jar PersistentTime
I get
24
when ObjectSizeFetcherAgent does this
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);
}
}
and when PersistentTime looks as follows
public class PersistentTime {
List<String> list = new ArrayList<String>();
public static void main(String[] args) {
PersistentTime p = new PersistentTime();
p.list.add("a"); // The number is the same with or without this
p.list.add("b"); // The number is the same with or without this
p.list.add("c"); // The number is the same with or without this
System.out.println(ObjectSizeFetcher.getObjectSize(p));
}
}
Why is adding elements to the list have no affect?
Your
PersistentTimeobject consists solely of one reference (to an array list).24 bytes is typical for objects containing a single reference.
Note: referenced objects are not included in the computation.
getObjectSizeis not recursively collecting the combined object sizes. This is not generally possible: there could be infinite reference loops and such; I don’t think there is a “deep size” computation easily available.