Skip to content
Advertisement

Does JVM Stack have no direct references to objects but reference to constant pool?

I am investigating JVM architecture and its working behind the scenes.

I have heard a lot of times that stack stores method return types, operands, local variables and references to objects.

But while reading the Oracle specification I have found the picture where drawn that stack frame has no references to objects directly but the reference to the constant pool.

Am I understanding correctly that stack has the reference to the reference to the objects in heap or not?

Frame – is a part of stack.

Each frame has its own array of local variables (§2.6.1), its own operand stack (§2.6.2), and a reference to the run-time constant pool (§2.5.5) of the class of the current method.

From this explanation I can understand that in order to get an adress of object in the heap we need to find it in the run-time constant pool.
Link to the Oracle specification – https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html

Advertisement

Answer

Stack can contain pointers to heap and constant pool(by the way this is also logically in the heap accoring to JVM spec). Implementation specific these pointers can point to objects or to pointers that points to the objects.

Implementation of the garbage collector can effect this behaviour. For example Shenandoah Gc uses additional pointers for accesing objects(*) . With this implementation a pointer (gc root) in the stack points to another pointer that points to actual object. Oracle Jvm is not implemented like this.

(*) Details of the implementation;

Objects in the heap can be moved to different memory locations during garbage collection. For example objects in the eden space can be moved to survivor space and then to old generation. With this information, lets say an object is pointed by 10 pointers, if this object’s memory address is changed, 10 pointers should be updated to point to correct address. If the pointers, points to a forwarding pointer, and that forwarding pointer points to object; only updating the forwarding pointer is required this time. Aim of the forwarding pointer approach is reducing the garbage collection pause times.(at the cost of lower throughput)

There is an explaination of this process in the following video.

https://youtu.be/AAiB3fDwyRM?t=424

https://wiki.openjdk.java.net/display/shenandoah/Main https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.5.4

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement