JVM 压缩 Oops 背后的技巧

2024-02-21

So I understand the compressed oops is enabled by default in HotSpot VM now. It has support for this from Java SE 6u23 onwards through the VM option -XX:+UseCompressedOops. I understand that it allows for efficient CPU cache utilization as the CPU caches can hold a larger number of references than if they had to deal with 64 bit sized references. But what I do not understand is how using only 32 bits JVM can address up to 264 addresses.

To simplify the problem how can we address up to 24 memory address's using just 2 bits? What can be a possible encoding/decoding of such an address scheme?


有关压缩 oops 的详细说明,请参阅“Hotspot JVM 中的压缩 oops” https://wiki.openjdk.java.net/display/HotSpot/CompressedOops约翰·罗斯@Oracle 的文章。

TL;DR 版本是:

  • 在现代计算机体系结构中,内存地址是字节地址,
  • Java object references are addresses that point to the start of a word1,
  • on a 64-bit machine, word alignment means that that the bottom 3 bits of an object reference / address are zero2
  • 因此,通过将地址右移 3 位,我们可以将 64 位地址的 35 位“压缩”为 32 位字,
  • 并且,可以通过向左移动 3 位来完成解压缩,这会将这 3 个零位放回原处,
  • 35 位寻址允许我们使用适合 64 位机器上 32 位(半)字的压缩 oop 来表示最多 32 GB 堆内存的对象指针。

Note that this only works on a 64-bit JVM. We still need to be able to address the memory containing that (up to) 32 GB heap1, and that means 64-bit hardware addresses (on modern CPUs / computer architectures).

Note also that there is a small penalty in doing this; i.e. the shift instructions required to translate between regular and compressed references. However, the flip side is that less actual memory is consumed3, and memory caches are typically more effective as a consequence.

1 - This is because modern computer architectures are optimized for word-aligned memory access.

2 - This assumes that you haven't used -XX:ObjectAlignmentInBytes to increase the alignment from its default (and minimum) value of 8 bytes.

3 - In fact, the memory saving is application specific. It depends on the average object alignment wastage, ratios of reference to non-reference fields and so on. It gets more complicated if you consider tuning the object alignment.


To simplify the problem how can we address up to 24 memory addresses using just 2 bits? What can be a possible encoding/decoding of such an address scheme?

You can't address 24 byte addresses. But you can address 22 word addresses (assuming 32-bit words) using 2-bit word addresses. If you can assume that all byte addresses are word-aligned, then you can compress a 4-bit byte address as 2-bit word address by shifting it by 2-bit positions.

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

JVM 压缩 Oops 背后的技巧 的相关文章

随机推荐

  • Java 流压缩两个列表

    我有一个人员哈希集 一个人有名字 姓氏和年龄 例如 Person Hans Man 36 我的任务是获取 17 岁以上人员的列表 按年龄对他们进行排序 并将名字与姓氏连接起来 比如 汉斯 曼 另一个名字 另一个名字 我刚刚被允许导入 imp
  • 禁用 Reactjs 中的依赖下拉选项

    我正在制作一个简单的反应应用程序 其中有一些下拉列表 其中一个依赖于另一个 gt 这里下拉菜单 1 的值为游戏类型 例如Indoor and Outdoor gt 这里下拉菜单 2 的值为运动类型 例如Chess Tennis and Fo
  • “perf stat”输出是什么意思?

    I use perf stat 命令对一些事件进行统计 root root test perf stat a e r81d0 r82d0 v a r81d0 71800964 1269047979 1269006431 r82d0 2665
  • Graphics.Drawstring 在 PictureBox 中看起来不错,但在 Bitmap 中却很糟糕

    我正在尝试使用DrawString将文本写为图像 然后将其旋转 90 度 它无论是在位图上还是直接在 PictureBox 上都可以正常工作 但最大的区别在于质量 这PictureBox绘制的文本质量很好而且看起来很漂亮 当我把它画在图像上
  • 使用 jimp 在 Node.js 中调整图像大小并获取新图像的路径

    我正在使用 jimp 调整 node js 中的图像大小 我成功地降低了图像质量 但有点困惑如何获取新图像的路径 Jimp read test jpg function err test if err throw err test resi
  • 事件循环和Promise之间有什么关系[重复]

    这个问题在这里已经有答案了 我很好奇Event Loop和Promise之间的关系 演示暴露了这个问题 我预计p1 fulfilled出现在中间 因为它们将任务排队到同一个任务队列中并逐个执行 var p1 new Promise func
  • Fortran 中的断言

    Fortran 是否有与 C 等效的标准函数 关键字assert 我找不到assert我在 Fortran 2003 标准中提到过 我发现了几种使用预处理器的方法 但是在这个answer https stackoverflow com a
  • Sparklyr 处理分类变量

    Sparklyr 处理分类变量 我来自 R 背景 习惯于在后端处理分类变量 作为因子 对于 Sparklyr 来说 使用起来相当混乱string indexer or onehotencoder 例如 我有许多变量在原始数据集中被编码为数值
  • R 中的 For 循环与 while 循环

    我在 R 工作时注意到一件奇怪的事情 当我有一个使用 for 循环和 while 循环实现的计算从 1 到 N 的平方的简单程序时 行为并不相同 在这种情况下我不关心矢量化或应用函数 fn1 lt function N for i in 1
  • pandas dataframe:loc 与查询性能

    我在 python 中有 2 个数据框 我想查询数据 DF1 4M 记录 x 3 列 这query功能好像多了 效率比loc功能 DF2 2K 记录 x 6 列 这loc功能似乎多了 效率比query功能 两个查询都返回一条记录 模拟是通过
  • 即使禁用 SSR,评估 SSR 模块也会出错 - svelte-kit

    我希望我的应用程序中的路线之一不要在服务器端渲染 这样做的方法是export const ssr false在模块脚本或设置中ssr false in svelte config js如中提到的精简文档 https kit svelte d
  • 在 null 上调用成员函数 helper()

    我正在尝试找出 codeigniter 但目前无法插入到我的数据库中 我一直在关注官方文档 成功从数据库中读取数据 我的错误完整 遇到未捕获的异常类型 错误 消息 在 null 上调用成员函数 helper 文件名 Library WebS
  • Android 录音

    我想知道如何使用 android 中的 AudioRecord 功能而不是 MediaRecorder 来录制我们的声音和播放 请给我示例代码或网址 提前致谢 我认为您可以从使用以下原型代码开始 import android media A
  • 难以理解 Android 应用程序中的复杂多线程

    我在理解应用程序中的多线程方面遇到了很大的问题 因此发现了一个错误 我已经检查过我认为所有的可能性 但仍然遇到各种 有时是意外的 错误 也许这里有人可以建议我 我应该做什么 在我的项目中 我使用两个外部库 图形视图 https github
  • 重定向到不同的控制器

    我在 IAuthorizationFilter 中有一些代码 它将用户重定向到登录页面 但我在更改所使用的控制器时遇到了问题 所以我可能会这样做 public void OnAuthorization AuthorizationContex
  • Redux 状态正在更新,无需分派任何操作

    我首先应该说这不是重复的这个问题 恰好有相同的标题 https stackoverflow com questions 48648736 redux state is being edited without dispatching any
  • 正则表达式: boost::xpressive 与 boost::regex

    我想用 C 做一些正则表达式 所以我查看了 interwebz 是的 我是 C 的初学者 中级 并发现这个答案 https stackoverflow com questions 181624 c what regex library sh
  • Angular SSR NgApexcharts SVG 未定义

    最初我在使用 Angular SSR 时遇到了这个包的问题 因为我在导入时遇到了这个错误Window is not defined 但是 你可以在server ts通过以下方式 const MockBrowser require mock
  • Python 重定向(有延迟)

    所以我在 Flask 上运行了这个 python 页面 它工作得很好 直到我想要重定向 app route last visit def check last watered templateData template text water
  • JVM 压缩 Oops 背后的技巧

    So I understand the compressed oops is enabled by default in HotSpot VM now It has support for this from Java SE 6u23 on