Android & Dalvik - Get the size of an object

前端 未结 3 1750
一整个雨季
一整个雨季 2020-12-28 16:12

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

3条回答
  •  别那么骄傲
    2020-12-28 16:38

    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

提交回复
热议问题