Java VM是方法结束时操作数栈总是空的

标签 java jvm stack

在 Java VM 中,在方法结束时,当它返回时,操作数堆栈是否仅包含返回值(或者对于 void 方法为空)。还是返回值下还有其他值需要弃栈?

我正在标准 C 堆栈上创建每个方法框架。如此图所示:

enter image description here

如您所见,调用者中的操作数堆栈成为被调用者帧中的参数。 在被调用者框架中,在恢复保存的寄存器之前,操作数堆栈上是否有“垃圾”(返回值除外)需要清理?

最佳答案

请参阅 return 的文档:

operand stack

... → [empty]

Description

… If no exception is thrown, any values on the operand stack of the current frame (§2.6) are discarded.

ireturn ,分别是:

operand stack

..., value → [empty]

Description

… If no exception is thrown, value is popped from the operand stack of the current frame (§2.6) and pushed onto the operand stack of the frame of the invoker. Any other values on the operand stack of the current method are discarded.

我认为,这给出了足够的提示,表明操作数堆栈上可能存在必须“丢弃”的值,但在典型的实现中,不需要采取任何操作,因为丢弃堆栈帧作为一个整体意味着丢弃操作数堆栈。

我不确定为什么这会成为您的障碍。如果您真的想通过使用当前操作数堆栈位置到达堆栈帧的开头,您无论如何都需要有关堆栈帧的元信息,即您必须知道该帧中局部变量的数量。检索此信息并计算堆栈帧起始位置,再便宜不过了首先检索帧起始位置。

关于Java VM是方法结束时操作数栈总是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38058539/

相关文章:

Java 标志 -XX :+CompileTheWorld usable for Microbenchmarks

html - 图 slider "Stacks"

java - 有哪些 JVM 汇编程序?

java - 节点(和对象)相互分配时实际如何工作

java - PDFBOX : U+000A ('controlLF' ) is not available in this font Helvetica encoding: WinAnsiEncoding

java - 从 SpringMVC Controller 记录

Java终于回归了,奇怪的字节码

azure - Azure 是用什么堆栈/编程语言编写的?

c++ - 在堆栈类中使用指针?

java - 如何保存谷歌时间轴图表