博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android object size in Dalvik
阅读量:6860 次
发布时间:2019-06-26

本文共 3382 字,大约阅读时间需要 11 分钟。

hot3.png

Size of data

  • Size of reference

    In HotSpot, an object reference is 4 bytes in 32 bit JVM, 8 bytes in 64 bit JVM with -UseCompressedOops and 4 bytes with+UseCompressedOops. In Dalvik, reference is always 4 bytes.

  • Size of primitive data type

    The size of the primitive data type is fixd as follows:

    Data type 32 bit JVM 64 bit +UseCompressedOops 64bit -UseCompressedOops
    Object reference 4 4 8
    boolean 1 1 1
    byte 1 1 1
    char 2 2 2
    short 2 2 2
    int 4 4 4
    float 4 4 4
    long 8 8 8
    double 8 8 8

    But the size of the primitive type data is very diffrent in Dalvik.

    The size of a primitive data type is not the same when it is a field of object or a variable from when it is an element in Array.

    Data type Size as field / variable Size in Array 32 bit JVM 64 bit + 64bit -
    Object reference 4 4 4 4 8
    boolean 4 1 1 1 1
    byte 4 1 1 1 1
    char 4 2 2 2 2
    short 4 2 2 2 2
    int 4 4 4 4 4
    float 4 4 4 4 4
    long 8 8 8 8 8
    double 8 8 8 8 8

Size of object

  • Alignment

    In Dalvik, the boundary alignment of an object is also 8 bytes.

  • Overhead of Object

    In HotSpot, as we know, the overhead of object is 8 bytes in 32 bit JVM, and 16 bytes in 64 bit JVM withoutUseCompressedOops and 12 bytes with +UseCompressedOops.

    In Dalvik, this is diffrent. The memory of an object looks like:

    +---------------------+----------------------+----------+|overheade of Object  | overhead of dlmalloc |   data   |+---------------------+----------------------+----------+|   8 bytes           |  4 or 8 bytes        |          |+---------------------+----------------------+----------+

    There is another overhead for dlmalloc, which will take 4 or 8 bytes.

    So an empty object will take 16bytes, 12 bytes for overhead, 4 bytes for padding.

    Here are some examples:

    class EmptyClass {
    }

    Total size: 8 (Object overhead) + 4 (dlmalloc) = 12 bytes. For 8 bytes alignment, the final total size is 16 bytes.

    class Integer {
    int value; // 4 bytes }

    The total size is: 8 + 4 + 4 (int) = 16 bytes.

    static class HashMapEntry
    {
    final K key; // 4 bytes final int hash; // 4 bytes V value; // 4 bytes HashMapEntry
    next; // 4 bytes }

    The total size: 8 + 4 + 4 * 4 = 28 bytes. Total aligned is 32 bytes.

Size of Array

The memory layout of Array looks like:

+---------------------+----------------------+----------+---------+------+|overheade of Object  | overhead of dlmalloc |   size   | padding | data |+---------------------+----------------------+----------+---------+------+|   8 bytes           |  4 or 8 bytes        |  4 bytes | 4 bytes |      |+---------------------+----------------------+----------+---------+------+

The alignment is also 8 bytes.

So byte[0] will take: 8 + 4 + 4 + 4 = 20 bytes. The final size after alignment is 24 bytes.

byte[0] ~ byte[4] are all 24 bytes.

char[0] will also take 24 bytes. And fromchar[0]tochar[2]`, they are all 24 bytes.

Size of String

String is defined as follows:

class String {
private final char[] value; // 4 bytes private final int offset; // 4 bytes private final int count; // 4 bytes private int hashCode; // 4 bytes }

Total size: 8 + 4 + 4 * 4 = 28 bytes. Total aligned is 32 bytes, which excludes the retained memory of char array(at least 24 bytes).

So, even an empty String will still take at least 32 bytes of shadow heap and 24 bytes of retained heap.

References

转载于:https://my.oschina.net/weichou/blog/547999

你可能感兴趣的文章
Android关于TextView 宽度过大导致Drawable无法居中问题
查看>>
innodb 的预读
查看>>
redis slaveof自己会发生什么
查看>>
php实现基本数据结构之链表
查看>>
真正理解"CSS选择器的优先级"
查看>>
mysql技巧
查看>>
Linux系统安装mysql,jdk,tomcat等软件
查看>>
Vue-cli3 项目在安卓低版本系统和 IE 上白屏问题解决
查看>>
[译] 你正在阅读的用户体验文章是不是在向你进行推销?
查看>>
RQPro 公募FOF策略实例——晨星基金筛选和风险平价配置
查看>>
我的前端2019面试指引
查看>>
iOS热更新实现方式
查看>>
创建型模式 工厂模式
查看>>
最新安装CocoaPods教程
查看>>
Swizzling Method
查看>>
React同构踩坑记录
查看>>
教你用Python如何实现微信自动回复功能,机器人自动对话!
查看>>
使用var定义变量和不使用的区别
查看>>
React两个bug踩坑
查看>>
vue引入mxGrpah
查看>>