size of memory used by map incluing all the objects it is carrying

142 views Asked by At

Measure size of Map in memory including the size of all the objects it is holding. It doesn't seem to be straight forward. Size of the object itself might return shallow size as oppose to the actual size. Somebody suggested that we can make the object serializable to get the size. That might give the size. If the serializable object has only that Map, will it be then possible to get the count of the object in the map and the sizes of all the objects it is holding?

The profiler may be a way to go. Even if it may not show the size of objects while showing the size of object containing map. The size of the objects itself may be obtained separately and then added up. Any idea?

1

There are 1 answers

0
CMR On

If this is in Java, and if the object is Serializable, use java.io.ByteArrayOutputStream and java.io.ObjectOutputStream

public static int measure(Object m) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(m);
    return baos.size();
}