Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum size of a method in java?

Tags:

java

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 ?

like image 977
Reuben Avatar asked Jul 04 '11 10:07

Reuben


People also ask

How long should a method be Java?

a) Methods should not have more than an average of 30 code lines (not counting line spaces and comments).

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 the maximum number of methods a class can have?

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

How many lines does a method have?

A method contains too many lines of code. Generally, any method longer than ten lines should make you start asking questions.


1 Answers

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.

like image 180
Peter Lawrey Avatar answered Sep 24 '22 11:09

Peter Lawrey