Partner – Microsoft – NPI (cat=Java)
announcement - icon

Microsoft JDConf 2024 conference is getting closer, on March 27th and 28th. Simply put, it's a free virtual event to learn about the newest developments in Java, Cloud, and AI.

Josh Long and Mark Heckler are kicking things off in the keynote, so it's definitely going to be both highly useful and quite practical.

This year’s theme is focused on developer productivity and how these technologies transform how we work, build, integrate, and modernize applications.

For the full conference agenda and speaker lineup, you can explore JDConf.com:

>> RSVP Now

1. Overview 

When a Java method exceeds 65535 bytes, we get the compilation error, “code too large”. In this article, we’ll discuss why this error occurs and how to fix it.

2. JVM Constraints

The Code_attribute is a table of variable length in the method_info structure of JVM specifications. This structure contains the JVM instructions for a method, which can be a regular method or an initialization method for an instance, class, or interface:

Code_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u2 max_stack;
    u2 max_locals;
    u4 code_length;
    u1 code[code_length];
    u2 exception_table_length;
    {   
        u2 start_pc;
        u2 end_pc;
        u2 handler_pc;
        u2 catch_type;
    }
    exception_table[exception_table_length];
    u2 attributes_count;
    attribute_info attributes[attributes_count];
}

The attribute code_length specifies the length of the code in a method:

code_length
The value of the code_length item gives the number of bytes in the code array for this method.
The value of code_length must be greater than zero (as the code array must not be empty) and less than 65536.

As can be seen above, JVM specifications state that the code length of a method has to be less than 65536 bytes, so this implies that the size of a method can’t exceed 65535 bytes.

3. Why the Problem Occurs

Now that we know the size limit for methods, let’s looks at the situations that can result in such large methods:

  • Code Generators: Most large methods are a result of using some code generator like ANTLR parser
  • Initialization methods: GUI initializations can add lots of details like layouts, event listeners, and many more, all in one method
  • JSP Pages: Contains all the code in one single method of the class
  • Code Instrumentation: Adds bytecode to compiled classes at runtime
  • Array Initializers: Methods initializing very large arrays as shown below:
String[][] largeStringArray = new String[][] {
    { "java", "code", "exceeded", "65355", "bytes" },
    { "alpha", "beta", "gamma", "delta", "epsilon" },
    { "one", "two", "three", "four", "five" }, 
    { "uno", "dos", "tres", "cuatro", "cinco" }, 
        
    //More values
};

4. How to Fix the Error

As we’ve noted, the root cause of the error is a method exceeding the threshold of 65535 bytes. So, refactoring the erring method into several smaller methods will fix the problem for us.

In the case of array initializations, we can either split the arrays or load from a file. We can also use static initializers. Even when we’re using code generators, we can still refactor the code. And in the case of a large JSP file, we can use the jsp:include directive and break it into smaller units.

The above issues are relatively easy to handle, but things get complicated when we get a “code too large” error after adding instrumentation to the code. In case we own the code, we can still refactor the method. But when we get this error from a third-party library, we are in a fix. By reducing the instrumentation level, we might be able to fix the problem.

5. Conclusion

In this article, we’ve discussed the causes and potential solutions to the “code too large” error. We can always refer to the Code_Attributes section of the JVM Specifications to find more details about this constraint.

Course – LS (cat=Java)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!