简体   繁体   English

为什么Java对象指向指针?

[英]Why are Java objects pointers to pointers?

The JVMS says that : JVMS

In some of Oracle's implementations of the Java Virtual Machine, a reference to a class instance is a pointer to a handle that is itself a pair of pointers: one to a table containing the methods of the object and a pointer to the Class object that represents the type of the object, and the other to the memory allocated from the heap for the object data. 在Oracle的一些Java虚拟机实现中,对类实例的引用是指向句柄的指针,该句柄本身是一对指针:一个指向包含对象方法的表和指向表示Class对象的指针对象的类型,另一个是从堆为对象数据分配的内存。

I don't understand why references would be implemented this way rather than making them a pointer to the method table pointer directly followed by the object's data. 我不明白为什么引用会以这种方式实现,而不是使它们成为指向方法表指针的指针,该指针直接跟随对象的数据。 This would avoid an extra memory allocation on object creation and an extra pointer dereference on field access. 这将避免在对象创建时额外的内存分配以及对字段访问的额外指针解除引用。

What's the reason Oracle implemented them like that instead? 甲骨文这样实现它们的原因是什么?

Such a strategy would allow moving objects in memory without the need to adapt all existing references, as only the one direct pointer needs to be adapted. 这样的策略将允许在存储器中移动对象而不需要调整所有现有的引用,因为仅需要调整一个直接指针。 Moving objects in memory will be done by copying or compacting garbage collectors. 在内存中移动对象将通过复制或压缩垃圾收集器来完成。

However, for all recent JVM implementations the costs of this indirection were considered not worth the saving, so “some of Oracle's implementations” actually means “some very old JVMs from Sun which Oracle got when buying Sun”. 但是,对于所有最近的JVM实现,这种间接的成本被认为不值得节省,因此“Oracle的一些实现”实际上意味着“一些非常古老的JVM来自Sun在购买Sun时获得的Sun”。 Today's real world JVMs do adapt all references when moving objects in memory. 今天的现实世界JVM在内存中移动对象时会调整所有引用。

In other words, that's rather an outdated statement regarding JVMs still in use, but was kept in the specification as an example of an alternative implementation strategy. 换句话说,这是关于仍在使用的JVM的过时陈述,但作为替代实现策略的示例保留在规范中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM