in kotlin how to get the object's size

时间:2019-04-17 00:39:39

标签: kotlin objectsize

Is it possible to get the object's memory consumption, like in c++ with sizeOf()?

for object is Parcelable, seems this one could get the raw bytes, not sure if it is accurate for the size of that parcelable objec:

fun marshall(parceable: Parcelable): ByteArray {
    val parcel = Parcel.obtain()
    parceable.writeToParcel(parcel, 0)
    val bytes = parcel.marshall()
    parcel.recycle()
    return bytes
} 

Any suggestion?

Update:

Thanks @Gustavo Passini pints to a link with a lot approaches. Someone needs java.lang.instrument package and not able to use on Android, and found one but needs api level 26 above, converted into koltin and copied it here as ref (thanks Agnius Vasiliauskas ), is it accurate?:

import java.io.ByteArrayOutputStream
    import java.io.ObjectOutputStream
    import java.io.Serializable

    @TargetApi (26)
    object ObjectSizeCalculator {
        private fun getFirstObjectReference(o: Any): Any? {
            val objectType = o.javaClass.typeName

            if (objectType.substring(objectType.length - 2) == "[]") {
                try {
                    return if (objectType == "java.lang.Object[]")
                        (o as Array<Any>)[0]
                    else if (objectType == "int[]")
                        (o as IntArray)[0]
                    else
                        throw RuntimeException("Not Implemented !")
                } catch (e: IndexOutOfBoundsException) {
                    return null
                }

            }

            return o
        }

        fun getObjectSizeInBytes(o: Any?): Int {
            val STRING_JAVA_TYPE_NAME = "java.lang.String"

            if (o == null)
                return 0

            val objectType = o.javaClass.typeName
            val isArray = objectType.substring(objectType.length - 2) == "[]"

            val objRef = getFirstObjectReference(o)
            if (objRef != null && objRef !is Serializable)
                throw RuntimeException("Object must be serializable for measuring it's memory footprint using this method !")

            try {
                val baos = ByteArrayOutputStream()
                val oos = ObjectOutputStream(baos)
                oos.writeObject(o)
                oos.close()
                val bytes = baos.toByteArray()

                var i = bytes.size - 1
                var j = 0
                while (i != 0) {
                    if (objectType !== STRING_JAVA_TYPE_NAME) {
                        if (bytes[i].toInt() == 112)
                            return if (isArray)
                                j - 4
                            else
                                j
                    } else {
                        if (bytes[i].toInt() == 0)
                            return j - 1
                    }
                    i--
                    j++
                }
            } catch (e: Exception) {
                return -1
            }

            return -1
        }

    }

0 个答案:

没有答案