java的this指针,为什么Java对象的指针指向指针?

The JVMS says that:

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.

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”. Today’s real world JVMs do adapt all references when moving objects in memory.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值