Maximum size of a method in java?

Posted on

Maximum size of a method in java?Error is an illegal operation performed by the user which results in the abnormal working of the program. By now, you’ve probably seen a few errors, either when compiling or running your code like Maximum size of a method in java?. It can be frustrating, but they can also give you a lot of information about exactly how you can fix the problems in your code about java and . In this post covers the types of errors you’ll see when programming in Java, and how to fix them. Don’t pay any attention to the number of errors. Just read the first error message and work on fixing that error.

Problem :

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 ?

Solution :

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.

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *