maximum size of java class / exception table

前端 未结 3 634
情话喂你
情话喂你 2020-12-03 22:24

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#1546 in the Code attribute str

相关标签:
3条回答
  • 2020-12-03 22:49

    § 4.8.1 Static Constraints of the JVM spec says:

    The value of the code_length item must be less than 65536.

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

    0 讨论(0)
  • 2020-12-03 22:58

    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.

    0 讨论(0)
  • 2020-12-03 23:02

    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)

    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.

    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.

    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.

    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.

    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!

    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)
    
    0 讨论(0)
提交回复
热议问题