Hotspot 中一个变量槽应该为多少字节?

2020-05-13 21:46:50 +08:00
 zzkde

Java 虚拟机规范并没有定义变量槽的大小,那么在具体虚拟机,比如 Hotspot 上一个 slot 多大呢?

Java 虚拟机规范要求一个 reference 占用一个 slot,只有 long 和 double 这类 64 位的数据才使用两个 slot 。假设一个 slot 为 4 字节,在 64 位机器上一个 32 位的 slot 的 reference 怎么利用超过 4G 的内存?

虽然压缩指针可以实现 32G 的寻址,但是我看文档 https://wiki.openjdk.java.net/display/HotSpot/CompressedOops 中说在变量表上并不启用压缩

In the interpreter, oops are never compressed. These include JVM locals and stack elements, outgoing call arguments, and return values. The interpreter eagerly decodes oops loaded from the heap, and encodes them before storing them to the heap.

1508 次点击
所在节点    问与答
3 条回复
wushigejiajia01
2020-05-13 22:43:43 +08:00
菜鸡观摩楼下大佬给个答案
1194129822
2020-05-13 23:36:49 +08:00
纠结 slot 无意义,slot 是 class 层面的,类加载后就变不存在了。JVM 规范并没有规定 slot 大小,局部变量的大小 32 位机器除了 long 和 double 都是 32 位,64 位机器所有局部变量都是 64 位(一个字长)即使 long 和 double 在 class 层面占两个 slot,加载后 64 位 JVM 对 long 和 double 局部变量只分配一个字长并没有违反 JVM 规范,使用 SA 工具 HSDB 就能看到 Java 栈帧的布局了
zzkde
2020-05-14 17:14:42 +08:00
@1194129822 我去试了 hsdb,但是刚连接上去就报了下面的异常

```
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Unable to deduce type of thread from address 0x00007fc30016f800 (expected type JavaThread, CompilerThread, ServiceThread, JvmtiAgentThread or CodeCacheSweeperThread)
at jdk.hotspot.agent/sun.jvm.hotspot.runtime.Threads.createJavaThreadWrapper(Threads.java:172)
at jdk.hotspot.agent/sun.jvm.hotspot.runtime.Threads.first(Threads.java:156)
at jdk.hotspot.agent/sun.jvm.hotspot.ui.JavaThreadsPanel.cache(JavaThreadsPanel.java:463)
at jdk.hotspot.agent/sun.jvm.hotspot.ui.JavaThreadsPanel.<init>(JavaThreadsPanel.java:89)
at jdk.hotspot.agent/sun.jvm.hotspot.HSDB$45.run(HSDB.java:1380)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: sun.jvm.hotspot.types.WrongTypeException: No suitable match for type of address 0x00007fc30016f800
at jdk.hotspot.agent/sun.jvm.hotspot.runtime.InstanceConstructor.newWrongTypeException(InstanceConstructor.java:62)
at jdk.hotspot.agent/sun.jvm.hotspot.runtime.VirtualConstructor.instantiateWrapperFor(VirtualConstructor.java:80)
at jdk.hotspot.agent/sun.jvm.hotspot.runtime.Threads.createJavaThreadWrapper(Threads.java:168)
... 17 more
```

我的环境:archliunx 、openjdk8 和 openjdk11 都试过了。是以同一个用户身份运行的。
在网上查询了很久没啥解决方案,不知道大佬怎么解决?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/671394

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX