为什么Java对象的指针指向指针? [英] Why are Java objects pointers to pointers?

查看:208
本文介绍了为什么Java对象的指针指向指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JVMS :

在Oracle对Java虚拟机的某些实现中,对类实例的引用是指向本身是一对指针的句柄的指针:一个指向包含对象的方法的表和指向Class的指针表示对象类型的对象,另一个表示从堆为对象数据分配的内存.

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.

我不明白为什么引用会以这种方式实现,而不是使它们成为直接指向对象数据后的方法表指针的指针.这样可以避免在对象创建时分配额外的内存,而在字段访问时避免额外的指针取消引用.

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.

Oracle如此实施它们的原因是什么?

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.

但是,对于所有最近的JVM实现而言,这种间接成本并不值得节省,因此"Oracle的某些实现"实际上意味着"Oracle从Sun购买的Sun的一些非常老的JVM".在内存中移动对象时,当今的现实世界中的JVM确实会适应所有引用.

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". 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.

这篇关于为什么Java对象的指针指向指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆