java java类/异常表的最大大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5497495/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:24:39  来源:igfitidea点击:

maximum size of java class / exception table

javaclassexception

提问by mete

I was wondering what is the maximum size of a java class. As shown here http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#1546in the Code attribute structure, code length is specified as 4-bytes, so it is a big number. What I did not understand is that the pc attributes of exception table are 2 bytes. How can it work if code length is more than 2-bytes but exception tables can only address the 2-bytes ?

我想知道 java 类的最大大小是多少。如这里http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#1546在 Code 属性结构中,代码长度指定为 4 字节,因此一个很大的数字。我不明白的是异常表的pc属性是2个字节。如果代码长度超过 2 个字节但异常表只能寻址 2 个字节,它如何工作?

回答by Joachim Sauer

§ 4.8.1 Static Constraintsof the JVM spec says:

JVM 规范的§ 4.8.1 静态约束说:

The value of the code_lengthitem must be less than 65536.

code_length物品的价值必须小于65536

So although it is a 4 byte value, it must not exceed 64k.

所以虽然是4个字节的值,但不能超过64k。

回答by Ingo

The exception table indexes into the code of a specific method, whose code can indeed not be longer than 64k. But as there may be many methods in a class, this places in itself no restriction on the size of a class.

异常表索引到特定方法的代码中,其代码确实不能超过64k。但是由于一个类中可能有很多方法,所以这本身对类的大小没有限制。

回答by mjuarez

So, I stumbled on this and happened to be bored, so I decided to try it out. (Running Java 1.6.0_26 on a quad-Xeon MacPro 1,1)

所以,我偶然发现了这个,碰巧很无聊,所以我决定尝试一下。(在四核 Xeon MacPro 1,1 上运行 Java 1.6.0_26)

Turns out, there is a limit acknowledged by Eclipse at least, saying there can't be more than 65,535 bytes in a method. However, for some reason, this only applies to the main method and to constructor methods, NOT to normal class methods. Once the main and constructor methods were under 65K, the code ran with no problems. Eclipse had some problems coping with such big methods (100% cpu for a few seconds, and error icon ghosts would stay on the editor), but the code actually ran just fine.

事实证明,Eclipse 至少承认有一个限制,即一个方法中不能超过 65,535 个字节。但是,出于某种原因,这仅适用于 main 方法和构造函数方法,不适用于普通的类方法。一旦 main 方法和构造函数方法低于 65K,代码运行就没有问题了。Eclipse 在处理如此大的方法时遇到了一些问题(100% cpu 几秒钟,错误图标鬼影会留在编辑器上),但代码实际上运行得很好。

I added 5 additional methods that all had at least 100K of code (mostly gibberish System.out.println("blah")), and the methods themselves used a few parameters, and returned a value. So they were as "normal" as I could make them. I tried running those a few times, and they always ran perfectly.

我添加了 5 个额外的方法,它们都至少有 100K 的代码(主要是乱码 System.out.println("blah")),这些方法本身使用了一些参数,并返回了一个值。所以它们和我能做的一样“正常”。我尝试运行了几次,它们总是运行得很完美。

So, it does seem that there is a 65K limit, but it's definitely not to absolute class size, and it apparently only applies to "special" methods like main or constructor methods.

因此,似乎确实有 65K 的限制,但这绝对不是绝对的类大小,而且它显然只适用于“特殊”方法,如 main 或构造函数方法。

However, in my case at least, the bigger problem was that Eclipse just couldn't handle these types of files. CPU starts going to 100%, and the entire IDE seemed to freeze at times. Most of the time, it looked like Eclipse was simply recalculating syntax highlighting (!). The rest of the system was responsive during that time at least, which I guess says more about OS X than about Java.

但是,至少在我的情况下,更大的问题是 Eclipse 无法处理这些类型的文件。CPU 开始使用 100%,整个 IDE 有时似乎冻结。大多数时候,Eclipse 看起来只是在重新计算语法高亮 (!)。系统的其余部分至少在那段时间内是响应式的,我想这更多地是关于 OS X 而不是关于 Java。

Of course, why would you ever get even close to those limits is beyond me. Reason and common sense, if not OO principles, should have taken care of that long before it got close to 65K. But it was a nice experiment, and hey, I learned something!

当然,你为什么要接近这些极限是我无法理解的。理性和常识,如果不是面向对象原则,应该在它接近 65K 之前早就解决了。但这是一个不错的实验,嘿,我学到了一些东西!

Just for reference, this is what I tested it on:

仅供参考,这是我测试的:

~> java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03-383-11A511)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02-383, mixed mode)