简体   繁体   中英

Method code too large in Groovy & Grails?

2014-06-17 11:22:18,622 [Thread-11] ERROR compiler.GrailsProjectWatcher  - Compilation Error: startup failed:
General error during class generation: Method code too large!

What is the solution? Only 4-5 line code hide and restart then fully run in successfully, the bootStrap file size is 149k. When I comment or delete 4-5 line code, it will be run without no error!

The Java Virtual Machine has a limitation that methods cannot be larger than 64k (65536 bytes). This post describes this limitation in details.
The best way to overcome this issue is simply splitting your large method into smaller ones, which is generally a good practice.

Also notice that the JVM JIT compiler will not compile methods larger than 8K. You can however change this behavior using the -XX:-DontCompileHugeMethods option.

The problem: Just got in Jenkins pipeline the following exception error: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: General error during class generation: Method code too large! java.lang.RuntimeException: Method code too large! org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: General error during class generation: Method code too large! java.lang.RuntimeException: Method code too large!

Explanation : The root cause related to 64kB limit of byte code of a single method. Java Virtual Machine has implicit limitations on class that there are mandatory to be followed and restricted according to performance and the language limitiation - such as: size of an operand stack in frame, length of fields and method names, number of methods may be declared in class etc... you can follow this "check list" on Oracle JVM documentation . You got the method size limitation on this scenario .

Solution : In order to solve this issue, just separate the class methods into shared-lib library or sub internal / external class (such as Utils.Groovy for example) and import that library in your main class. In general a code should be readable , lean and high level. if it's too long export the functionality use object oriented architecture and you would earn readable and maintainable code as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM