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 ?
a) Methods should not have more than an average of 30 code lines (not counting line spaces and comments).
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.
The number of methods that may be declared by a class or interface is limited to 65535 by the size of the methods_count item of the ClassFile structure (§4.1).
A method contains too many lines of code. Generally, any method longer than ten lines should make you start asking questions.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With