28

I come to know that the maximum size of a method in java is 64k. And if it exceeds, we'll get a compiler warning like "Code too large to compile". So can we call this a drawback of java with this small amount of memory.

Can we increase this size limit or is it really possible to increase ?

Any more idea regarding this method size ?

Reuben
  • 5,556
  • 10
  • 43
  • 55

3 Answers3

37

In my experience the 64KB limit is only a problem for generated code. esp. when intiialising large arrays (which is done in code)

In well structured code, each method is a manageable length and is much smaller than this limit. Large pieces of data, to be loaded into arrays, can be read from a non Java files like a text or binary file.

EDIT:

It is worth nothing that the JIT won't compile methods larger than 8 K. This means the code runs slower and can impact the GC times (as it is less efficient to search the call stack of a thread with methods which are not compiled esp big ones)

If possible you want to limit your methods to 8 K rather than 64 K.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 3
    My work around for the limit was to generate methods for each N lines of generated code. Then call them all in a new method. – Diederik Sep 14 '12 at 08:55
  • 2
    @Diederik its a shame the compiler isn't smart enough to do that for you, or that byte code support larger methods. – Peter Lawrey Sep 14 '12 at 08:57
  • See my edit suggesting you limit your methods to 8 K or less. – Peter Lawrey Sep 14 '12 at 09:00
  • 4
    I've seen people hit into this limit several times with JSPs. JSPs, by default, get compiled to servlets with one huge doGet() method containing everything there is in a JSP. People who need lots of stuff in one page but haven't splitted the JSP into JSP fragments seem to hit this, so it is not only a problem for generated code. – eis Jun 18 '13 at 12:14
  • (Of course, the answer then is to start splitting up the gigantic JSP.) – eis Jun 18 '13 at 12:15
  • 4
    Two cases where I've been bit by this: One is using Java versions of YACC or LEX. They can generate very large state machines implemented in a single method. It also occurs when trying to measure code coverage. The coverage tools instrument the .class files with the additional bytecode required to collect the runtime data. The instrumentation bytecode can push methods over the 64K limit even if the original code is quite a bit smaller than that. – Dan Haynes Sep 05 '13 at 19:14
  • @Peter, have any link talking ahout the 8K no jit, thing? – MeBigFatGuy Mar 08 '14 at 22:03
  • @MeBigFatGuy http://blog.leenarts.net/2010/05/26/dontcompilehugemethods/ was the best I could find in 2 mins ;) – Peter Lawrey Mar 08 '14 at 22:31
  • 1
    @MeBigFatGuy See https://www.youtube.com/watch?v=wgQBz2Ldhvk&t=511s – **Important addition**: We're talking about the bytecode, not size of the java code that you're writing. – Philzen Jul 28 '23 at 20:19
8

64k is quite a lot, if you exceed it you may think about reorganizing you code.

In my project I met this constraint once in generated sources. Solved by just splitting one method to several.

Sergey Aslanov
  • 2,397
  • 15
  • 16
5

If your method is longer than 50 lines including inside comments - split it. In this case you will never reach any limitation (even if one exists).

I personally saw 1000 lines long methods (written by criminals that call themselves programmers :) ) but did not see such kind of limitation.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 6
    It bights most commonly in 2 cases: (a) JSP's, which is where I tripped over it; and (b) generated code. In both cases it can be a hell of a lot of work to ensure that your bytecode is allways under 64k. – corlettk Jul 04 '11 at 11:00