Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maximum size of java class / exception table

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 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 ?

like image 404
mete Avatar asked Mar 31 '11 09:03

mete


People also ask

How big can Java classes be?

But a class can have 65535 attributes, plus 65535 fields, each of them having 65535 attributes of its own and plus 65535 methods, each of them having up to 65535 attribute as well.

What is Java exception class?

The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch. The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions.

How many exceptions are there in Java?

There are three types of exception—the checked exception, the error and the runtime exception.

What are the Java exceptions?

Java Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc. Exception is an unwanted or unexpected event, which occurs during the execution of a program, i.e. at run time, that disrupts the normal flow of the program's instructions.


2 Answers

§ 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.

like image 183
Joachim Sauer Avatar answered Oct 05 '22 06:10

Joachim Sauer


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)
like image 25
mjuarez Avatar answered Oct 05 '22 06:10

mjuarez