83

I know that a method cannot be larger than 64 KB with Java. The limitation causes us problems with generated code from a JavaCC grammar. 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?

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LaurentG
  • 11,128
  • 9
  • 51
  • 66
  • 4
    I faced the same problem when I tried to compile a huge brainf**k code. – johnchen902 Jul 02 '13 at 10:01
  • 3
    Given no information as to the contrary, I think it is safe to assume that this limit will still be enforced in Java 8... Of course, another (costly) option could be to change the grammar engine for [parboiled](https://github.com/sirthias/parboiled), which allows you to write your grammars in pure Java. – fge Jul 02 '13 at 10:02
  • 4
    have a look at [this](http://chrononsystems.com/blog/method-size-limit-in-java) article – Anirudha Jul 02 '13 at 10:02
  • 7
    Personally I think creating such big methods is a *bug in JavaCC*. It really should be able to spread its code. Especially considering that the JVM is definitely not built for optimizing such huge methods. – Joachim Sauer Jul 08 '13 at 07:35

5 Answers5

60

According to 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.

But this is about Java 7. There is no final specs for Java 8, so nobody (except its developers) could answer this question.

UPD (2015-04-06) According to JVM8 it is also true for Java 8.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
  • 2
    Really there is such a "bug" in Java that it's still not fixed? Could be a heavy limitation, expecially for LaurentG situation. – Francesco Belladonna Jul 02 '13 at 19:11
  • 3
    @Fire-Dragon-DoL The quoted discussion is only about the method having to be *one byte less* in length than it could have been otherwise. This is quite immaterial. The limit is not due to any simple "bug" like that: it's in the *overall design of bytecode* and to fix it would take respecifying it in entirety. – Marko Topolnik Feb 25 '14 at 19:36
11

Good question. As always we should go to the source to 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:

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.

Cheers,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
  • 7
    The “greatest number of local variables” happens to have the same numerical value, but is still an entirely different thing than the “maximum size of a method”, the OP asked for. – Holger Dec 08 '17 at 10:13
8

It has not changed. The limit of code in methods is still 64 KB in both Java 7 and Java 8.

References:

  1. From the Java 7 Virtual Machine Specification (4.9.1 Static Constraints):

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.
  1. From the Java 8 Virtual Machine Specification (4.7.3 The Code Attribute):

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.

Community
  • 1
  • 1
Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239
1

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:

Quoting from 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.

As you see seems this historical problem doesn't seem to remedy at least in this version (java 8).

Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
-1

As a workaround, and if you 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)

ombud
  • 199
  • 2
  • 10