2

Today I got the following error message for the first time ever...

java.lang.Error: Unresolved compilation problem: 
    The code of method init() is exceeding the 65535 bytes limit

    at generator.Main.init(Main.java:36)
    at sun.applet.AppletPanel.run(AppletPanel.java:425)
    at java.lang.Thread.run(Thread.java:680)

The error is being caused because I am creating an array list with around 10,000 ints in my init() method. The solution to this problem is not very complicated, I made 3 methods that each had 3,300 ints in them and called them all from my init().

But I have a couple questions purely out of curiosity.

  1. Why do methods have a limit on space?
  2. Why is it limited to 65535 bytes? (2^something-1?)
  3. Is there any way to give a method more space or will they all be limited the same amount?
  4. How much information exactly is 65535 bytes?
java
  • 1,319
  • 6
  • 15
  • 30
  • possible duplicate of [Maximum size of a method in java?](http://stackoverflow.com/questions/6570343/maximum-size-of-a-method-in-java) and/or [Maximum size of a method in Java 7 and 8](http://stackoverflow.com/questions/17422480/maximum-size-of-a-method-in-java-7-and-8) – Ted Hopp Oct 01 '13 at 21:55
  • 65535 = 2^16 - 1, so, two bytes. – nhgrif Oct 01 '13 at 21:55
  • Have you considered using a for loop rather than 10,000 individual ints? – FThompson Oct 01 '13 at 21:56
  • @TedHopp should I delete my question? – java Oct 01 '13 at 21:57
  • @Vulcan its 10k points that have a specific order and theres no pattern to them – java Oct 01 '13 at 21:58
  • 1
    @java Consider containing them in a text file and reading them into the arraylist. – FThompson Oct 01 '13 at 22:02
  • How much is 65535 bytes? It's 64k, about the entire amount of memory for a Commodore 64 - imagine that... – MadProgrammer Oct 01 '13 at 22:14
  • @java - Not necessarily; if one of the possible duplicates answers your question, you can delete this, which will prevent other people from spending their time writing answers when you already have one. Or you can accept one of the answers here if it works for you. Or you can leave the question open to see if a better answer appears. Be aware that if enough people vote to put this question on hold as being a duplicate, then no more answers would be possible (unless the question were reopened, which doesn't happen all that often). – Ted Hopp Oct 01 '13 at 22:21

2 Answers2

2

From the Java Virtual Machine Specification section 4.11:

Limitations of the Java Virtual Machine

The amount of code per non-native, non-abstract method is limited to 65536 bytes by the sizes of the indices in the exception_table of the Code attribute (§4.7.3), in the LineNumberTable attribute (§4.7.8), and in the LocalVariableTable attribute (§4.7.9).

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • Nit pick: While this says why the compiler is limited to 64k, it doesn't really explain why the decision was made, which, IMHO, is probably what the OP is asking... – MadProgrammer Oct 01 '13 at 21:59
  • @MadProgrammer:- Yes got your point. To be honest I have no idea why this decision was taken. I found a very interesting thread which mentions the same:- http://www.velocityreviews.com/forums/t146001-reached-the-65535-bytes-limit.html – Rahul Tripathi Oct 01 '13 at 22:06
  • I would imagine it would have come down to choices available at the time when the language was defined, but I would still have no idea :P – MadProgrammer Oct 02 '13 at 00:15
-1

The answer can be found in the "class file format" page of the JVM spec. JVMs have to work this way, or they're not JVMs.

http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110