tags:

views:

1510

answers:

6

How many lines of code can we write in any java file ? Does it depend on JVM ?

A: 

I'm thinking it's more based on the limits of your filesystem than any inherent limit in the compiler or JVM, and unless you're writing horrible, horrible code, you are highly unlikely to exceed any of them either way.

Daniel Bruce
+4  A: 

I believe there is a 64kb limit on bytecode size per method.

Jonas Klemming
There is a bug/rfe logged with Sun for this at http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4262078
bmatthews68
+5  A: 

I really hope for your sake that you never run into this limit, if there is one!

jason
+22  A: 

To extend upon Jonas's response, the Java Virtual Machine Specification, Section 4.8 Constraints on Java Virtual Machine Code says that:

The Java virtual machine code for a method, instance initialization method (§3.9), or class or interface initialization method (§3.9) is stored in the code array of the Code attribute of a method_info structure of a class file. This section describes the constraints associated with the contents of the Code_attribute structure.

Continuing to Section 4.8.1, Static Constraints

The static constraints on a class file are those defining the well-formedness of the file. With the exception of the static constraints on the Java virtual machine code of the class file, these constraints have been given in the previous section. 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 value of the code_length item must be less than 65536.

...

So a method does have a limit of 65535 bytes of bytecode per method. (see note below)

For more limitations to the JVM, see Section 4.10 Limitations of the Java Virtual Machine.

Note: Although there is apparently a problem with the design of the JVM, where if the instruction at byte 65535 is an instruction that is 1 byte long, it is not protected by exception handler - this is listed in footnote 4 of Section 4.10.

coobird
+2  A: 

I remember actually running into this limit once in a complex JSP page in Tomcat 4 (way in the past when people were still using JSPs). The java file generated from the JSP had a method that was too big to compile, I think I had to split up the file or do some other stunt, which of course was a good idea in terms of readability anyway.

Sun's bug tracker tells me some people still have the same problem.

Henning
been there. worked on a JSP that included a whole bunch of JSPs in a loop, or something along those lines.
Ken Liu
+2  A: 

There is no limit on "lines of code", but there is a limit on total size. Each method has a 64kb limit.

I've only ever run into this with code generation tools.

If you are coming close to the limit, be careful. A lot of profiling and monitoring tools use byte code insertion. They can push you over the top if you're too close. What's worse is that they often alter your class files after compilation. Everything compiles and runs in your development environment, but it crashes when you turn on your monitoring tools in Test or QA.