Android 上的 Java 对象在内存中是如何布局的?

2024-03-02

我对 HotSpot 中堆上对象的布局相当熟悉,但对 Android 不太熟悉。

例如,在 32 位 HotSpot JVM 中,堆上的对象被实现为 8 字节标头,后面是对象的字段(一个字节表示boolean,四个字节用于引用,其他所有内容均按预期),以某种特定顺序布局(对超类中的字段有一些特殊规则),并填充为 8 字节的倍数。

我做了一些研究,但找不到任何 Android 特定的信息。

(我有兴趣优化一些极其广泛使用的数据结构,以最大限度地减少 Android 上的内存消耗。)


dalvik/vm/oo/Object.h是你的朋友吗?评论为struct Object says:

/*
 * There are three types of objects:
 *  Class objects - an instance of java.lang.Class
 *  Array objects - an object created with a "new array" instruction
 *  Data objects - an object that is neither of the above
 *
 * We also define String objects.  At present they're equivalent to
 * DataObject, but that may change.  (Either way, they make some of the
 * code more obvious.)
 *
 * All objects have an Object header followed by type-specific data.
 */

java.lang.Class物体是特殊的;它们的布局是由ClassObject结构体Object.h。数组对象很简单:

struct ArrayObject : Object {
    /* number of elements; immutable after init */
    u4              length;

    /*
     * Array contents; actual size is (length * sizeof(type)).  This is
     * declared as u8 so that the compiler inserts any necessary padding
     * (e.g. for EABI); the actual allocation may be smaller than 8 bytes.
     */
    u8              contents[1];
};

对于数组,宽度为vm/oo/Array.cpp。布尔值的宽度为 1,对象的宽度为sizeof(Object*)长度(通常为 4),所有其他基本类型都有其预期(压缩)长度。

数据对象非常简单:

/*
 * Data objects have an Object header followed by their instance data.
 */
struct DataObject : Object {
    /* variable #of u4 slots; u8 uses 2 slots */
    u4              instanceData[1];
};

的布局DataObject(所有非 Class 类实例)受以下约束computeFieldOffsets in vm/oo/Class.cpp。根据那里的评论:

/*
 * Assign instance fields to u4 slots.
 *
 * The top portion of the instance field area is occupied by the superclass
 * fields, the bottom by the fields for this class.
 *
 * "long" and "double" fields occupy two adjacent slots.  On some
 * architectures, 64-bit quantities must be 64-bit aligned, so we need to
 * arrange fields (or introduce padding) to ensure this.  We assume the
 * fields of the topmost superclass (i.e. Object) are 64-bit aligned, so
 * we can just ensure that the offset is "even".  To avoid wasting space,
 * we want to move non-reference 32-bit fields into gaps rather than
 * creating pad words.
 *
 * In the worst case we will waste 4 bytes, but because objects are
 * allocated on >= 64-bit boundaries, those bytes may well be wasted anyway
 * (assuming this is the most-derived class).
 *
 * Pad words are not represented in the field table, so the field table
 * itself does not change size.
 *
 * The number of field slots determines the size of the object, so we
 * set that here too.
 *
 * This function feels a little more complicated than I'd like, but it
 * has the property of moving the smallest possible set of fields, which
 * should reduce the time required to load a class.
 *
 * NOTE: reference fields *must* come first, or precacheReferenceOffsets()
 * will break.
 */

因此,超类字段首先出现(像往常一样),然后是引用类型字段,最后是单个 32 位字段(如果可用,并且如果需要填充,因为有奇数个 32 位引用字段),然后是 64 - 位字段。接下来是常规 32 位字段。请注意,所有字段都是 32 位或 64 位(填充了较短的原语)。特别是,此时,VM 不会使用少于 4 个字节来存储字节/字符/短/布尔字段,尽管理论上它当然可以支持这一点。

请注意,所有这些都是基于阅读提交时的 Dalvik 源代码43241340(2013 年 2 月 6 日)。由于虚拟机的这方面似乎没有公开记录,因此您不应依赖它作为虚拟机对象布局的稳定描述:它可能会随着时间的推移而改变。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 上的 Java 对象在内存中是如何布局的? 的相关文章

  • 使用正则表达式验证电子邮件的最大长度

    我找到了用于电子邮件验证的正则表达式 a z0 9 a z0 9 a z0 9 a z0 9 a z 2 4 我希望电子邮件的最大长度为 20 个字符 因此我将其更改为 a z0 9 a z0 9 a z0 9 a z0 9 a z 2 4
  • 如何使用 aether 从 Java 找到最新版本的 Maven 工件?

    他们的文档非常薄弱 我无法弄清楚 我找到了部分答案here https stackoverflow com questions 27428068 how to retrieve the latest also snapshot versio
  • 在 doxygen 中使用 @see 或 @link

    我之前用 Javadoc 记录并使用了标签 see link or see foo and link foo 在我的描述中链接到其他课程 现在我尝试了doxygen 似乎这些标签不兼容 如果我运行 doxygen 完整的标签将被简单地解释为
  • android gradle插件-离线安装

    我必须在离线电脑上安装 android gradle 插件 通过谷歌搜索 我了解到我可以通过本地 Maven 存储库来做到这一点 但从不成功的尝试和所有关于这个问题的质量保证中我知道这并不简单 我从来没有和maven一起工作过 有经验的人可
  • 在 Eclipse 中删除空块之前的新行

    我更喜欢奥尔曼式 http en wikipedia org wiki Brace style Allman style大括号 例如 if foo magical prancing unicorn stuff 而不是 if foo unma
  • 为休息服务实施 JUnit 测试

    我必须为我的休息服务实现一些 JUnit 测试 例如 这是我的休息服务之一 Path dni fe public class HelloWorld POST Path home Consumes MediaType APPLICATION
  • Google 的 Android OpenGL 教程是否教授了错误的线性代数?

    在帮助另一位用户解决有关该问题的问题后响应触摸事件 http developer android com training graphics opengl touch htmlAndroid教程 我下载了源代码 并且对我所看到的感到非常困惑
  • 如何在 JASPIC 中保存经过身份验证的用户?

    我开发了一个安全认证模块 SAM 并实现了validateRequest方法 我还有一个简单的 Web 应用程序配置为使用此 SAM In my validateRequest方法 我检查 clientSubject 并设置一个Caller
  • 向Java类库添加函数

    我使用的 Java 类库在很多方面都不完整 有很多类我认为应该内置其他成员函数 但是 我不确定添加这些成员函数的最佳实践 让我们调用不足的基类A class A public A long arbitrary arguments publi
  • 在 Tensorflow-lite Android 中将位图转换为 ByteBuffer(浮点)

    在用于图像分类的tensorflow lite android演示代码中 图像首先转换为ByteBuffer格式以获得更好的性能 这种从位图到浮点格式的转换以及随后到字节缓冲区的转换似乎是一个昂贵的操作 循环 按位运算符 float mem
  • 如果 Modelmapper 中的整个属性为空,如何排除它们

    ModelMapper 是否 http modelmapper org http modelmapper org 支持什么排除属性 如果该值为空 我刚刚找到了 PropertyMap 但这对我来说是一种限制 因为我必须描述我想要的特定属性
  • 谷歌地图URL中参数的含义是什么

    我正在 Android 上使用 Webkit 浏览器 我想在以下 URL 中获得一个红色 A 符号
  • Desire HD 中的应用程序崩溃

    由于某些莫名其妙的原因 我的应用程序在 HTC Desire HD 上崩溃了 它在其他手机和模拟器中运行良好 这是崩溃报告 java lang RuntimeException Unable to start activity Compon
  • 如何更改 JAX-WS Web 服务的地址位置

    我们目前已经公开了具有以下 URL 的 JAX RPC Web 服务 http xx xx xx xx myservice MYGatewaySoapHttpPort wsdl http xx xx xx xx myservice MYGa
  • 为什么/何时应该使用泛型方法?

    学习Java的时候遇到过通用方法 public
  • 用于生成 ISO 文件的 Maven 插件

    有没有可以生成ISO镜像的maven插件 我需要获取一些模块的输出 主要是包含 jar 的 zip 文件 并将它们组合成一个 ISO 映像 Thanks 现在有一个 ISO9660 maven 插件可以完成这项工作 https github
  • Android 模拟器提示和技巧 [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 如何将 Android 应用程序添加到已在 iOS 应用程序中使用的现有 Firebase 项目?

    我一直在我的 iOS 应用程序中使用 Firebase 项目 我现在想开始为 Android 应用程序使用相同的 Firebase 项目及其所有数据库和存储 在您的应用程序下Overview菜单 你应该按添加另一个应用程序并选择Androi
  • FragmentMap + ActionBar 选项卡

    我一直在尝试插入一个MapView进入一个ActionBar Tab 但我什至无法解决问题 即使谷歌搜索 这是主要活动 Override public void onCreate Bundle savedInstanceState supe
  • Android 可扩展列表视图随机播放子项

    你好 我正在使用 Android Expandable listview 并用不同的视图在其中膨胀子视图 我遇到的问题是 当我展开视图然后打开另一个父视图时 布局中的子视图会变得混乱并在代码中膨胀错误的布局 这是我的两个项目的示例代码 这是

随机推荐