Java 7 和 8 中方法的最大大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17422480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maximum size of a method in Java 7 and 8
提问by LaurentG
I know that a method cannot be larger than 64 KB with Java. The limitation causes us problems with generated code from a JavaCCgrammar. We had problems with Java 6 and were able to fix this by changing the grammar. Has the limit been changed for Java 7 or is it planned for Java 8?
我知道使用 Java 的方法不能大于 64 KB。该限制导致我们在从JavaCC语法生成代码时出现问题。我们在 Java 6 中遇到了问题,并且能够通过更改语法来解决这个问题。Java 7 的限制是否已更改,还是计划用于 Java 8?
Just to make it clear. I don't need a method larger than 64 KB by myself. But I wrote a grammar which compiles to a very large method.
只是为了说清楚。我自己不需要大于 64 KB 的方法。但是我写了一个语法,它编译成一个非常大的方法。
采纳答案by Andremoniy
According to JVMS7:
根据JVMS7:
The fact that end_pc is exclusive is a historical mistake in the design of the Java virtual machine: if the Java virtual machine code for a method is exactly 65535 bytes long and ends with an instruction that is 1 byte long, then that instruction cannot be protected by an exception handler. A compiler writer can work around this bug by limiting the maximum size of the generated Java virtual machine code for any method, instance initialization method, or static initializer (the size of any code array) to 65534 bytes.
end_pc 是独占的这一事实是 Java 虚拟机设计中的一个历史错误:如果一个方法的 Java 虚拟机代码长度正好是 65535 字节,并且以 1 字节长的指令结束,则该指令不能被保护通过异常处理程序。编译器编写者可以通过将任何方法、实例初始化方法或静态初始化程序(任何代码数组的大小)生成的 Java 虚拟机代码的最大大小限制为 65534 字节来解决此错误。
But this is about Java 7
. There is no final specs for Java 8, so nobody (except its developers) could answer this question.
但这是关于Java 7
. Java 8 没有最终规范,所以没有人(除了它的开发人员)可以回答这个问题。
UPD (2015-04-06)According to JVM8it is also true for Java 8
.
UPD (2015-04-06)根据JVM8,对于Java 8
.
回答by Anders R. Bystrup
Good question. As always we should go to the sourceto find the answer ("The Java? Virtual Machine Specification"). The section does not explicitly mention a limit (as did the Java6 VM spec) though, but somewhat circumspectly:
好问题。一如既往,我们应该去源头寻找答案(“Java?虚拟机规范”)。该部分没有明确提到限制(Java6 VM 规范也是如此),但有些谨慎:
The greatest number of local variables in the local variables array of a frame created upon invocation of a method (§2.6) is limited to 65535 by the size of the max_locals item of the Code attribute (§4.7.3) giving the code of the method, and by the 16-bit local variable indexing of the Java Virtual Machine instruction set.
在调用方法时创建的框架的局部变量数组中的最大局部变量数(第 2.6 节)被 Code 属性(第 4.7.3 节)的 max_locals 项的大小限制为 65535方法,并通过 Java 虚拟机指令集的 16 位局部变量索引。
Cheers,
干杯,
回答by Philipp Cla?en
It has not changed. The limit of code in methods is still 64 KB in both Java 7 and Java 8.
它没有改变。在 Java 7 和 Java 8 中,方法中的代码限制仍然是 64 KB。
References:
参考:
- From the Java 7 Virtual Machine Specification (4.9.1 Static Constraints):
- 来自 Java 7 虚拟机规范(4.9.1 静态约束):
The static constraints on the Java Virtual Machine code in a class file specify how Java Virtual Machine instructions must be laid out in the code array and what the operands of individual instructions must be.
The static constraints on the instructions in the code array are as follows:
- The code array must not be empty, so the code_length item cannot have the value 0.
- The value of the code_length item must be less than 65536.
类文件中 Java 虚拟机代码的静态约束指定 Java 虚拟机指令必须如何在代码数组中布局以及单个指令的操作数必须是什么。
代码数组中指令的静态约束如下:
- code 数组不能为空,因此 code_length 项的值不能为 0。
- code_length 项的值必须小于 65536。
- From the Java 8 Virtual Machine Specification (4.7.3 The Code Attribute):
- 来自 Java 8 虚拟机规范(4.7.3 代码属性):
The value of the code_length item gives the number of bytes in the code array for this method.
The value of code_length must be greater than zero (as the code array must not be empty) and less than 65536.
code_length 项的值给出了此方法的代码数组中的字节数。
code_length 的值必须大于零(因为代码数组不能为空)且小于 65536。
回答by mok
Andremoniy has answered the java 7
part of this question already, but seems at that time it was soon to decide about java 8
so I complete the answer to cover that part:
Andremoniy 已经回答了java 7
这个问题的一部分,但当时似乎很快就要决定了,java 8
所以我完成了涵盖该部分的答案:
Quoting from jvms:
引用jvms:
The fact that end_pc is exclusive is a historical mistake in the design of the Java Virtual Machine: if the Java Virtual Machine code for a method is exactly 65535 bytes long and ends with an instruction that is 1 byte long, then that instruction cannot be protected by an exception handler. A compiler writer can work around this bug by limiting the maximum size of the generated Java Virtual Machine code for any method, instance initialization method, or static initializer (the size of any code array) to 65534 bytes.
end_pc 是独占的这一事实是 Java 虚拟机设计中的一个历史错误:如果一个方法的 Java 虚拟机代码正好是 65535 字节长,并且以 1 字节长的指令结束,那么该指令就不能被保护通过异常处理程序。编译器编写者可以通过将任何方法、实例初始化方法或静态初始化程序(任何代码数组的大小)生成的 Java 虚拟机代码的最大大小限制为 65534 字节来解决此错误。
As you see seems this historical problem doesn't seem to remedy at least in this version (java 8).
正如你所看到的,这个历史问题似乎至少在这个版本(java 8)中没有得到解决。
回答by ombud
As a workaround, and ifyou have access to the parser's code, you could modify it to work within whatever 'limits are imposed by the JVM compiler ... (Assuming it den't take forever to find the portions in the parser code to modify)
作为一种变通方法,如果您可以访问解析器的代码,您可以修改它以在 JVM 编译器强加的任何“限制”内工作......(假设它不需要永远找到解析器代码中的部分调整)