As we all know Java 5 introduced the ability for Instrumentation to get the size of an object with ease. Is there such a method on Android and Dalvik?
The java
My idea to get the size of a ArrayList
on the heap was to serialize the object into a Byte-Array, where the lengths is the size.
I've used the SerializationUtils
from Apache Commons Lang.
To serialize:
byte[] data = SerializationUtils.serialize((Serializable) arrayList);
The lenght of the data array should be approximately the size in bytes on the heap.
As mentioned, this requires Commons Lang library. It can be imported using Gradle:
api 'org.apache.commons:commons-lang3:3.5'
more ways mentioned here