How much memory is used for function references in

2019-08-23 06:07发布

问题:

Is it 32-bit / 64-bit depending on the JVM, or is it less as do we really need that many possibilities for addresses when addressing functions?

Surely we would never need that many possibilities as the number of types, and resulting number of functions would never reach these numbers?

回答1:

The by far simplest implementation uses the "real"/full address of the function, whatever that may be on the architecture in question (e.g. the virtual address of the first instruction of the function prologue). So yeah, such a function pointer will probably be a word large. And this is a good thing!

Size is not anywhere as important as for object references or object headers, because there is only one such pointer in the whole process per method, regardless of how many instances there are. Moreover, any scheme for shrinking the address means you need additional operations to call the function pointed to (either some indirection, or some arithmetic). That would be bad, because it has to happen every time a virtual method (which is all of them prior to optimizations, and still quite a few afterwards) is called.