Finding code for great methods

By default, HotSpot JIT refuses to compile methods that exceed approximately 8 thousand bytes of code (1). Is there anything that can scan a jar for such methods (2)?

  • if you don't pass -XX:-DontCompileHugeMethods

  • Jon Masamitsu describes how interpreted methods can slow down garbage collection, and notes that refactoring is generally wiser than -XX:-DontCompileHugeMethods

+4
source share
2 answers

Thanks to Peter Laurie for pointing to ASM. This program displays the size of each method in the bank:

 import org.objectweb.asm.ClassReader; import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.MethodNode; public static void main(String[] args) throws IOException { for (String filename : args) { System.out.println("Methods in " + filename); ZipFile zip = new ZipFile(filename); Enumeration<? extends ZipEntry> it = zip.entries(); while (it.hasMoreElements()) { InputStream clazz = zip.getInputStream(it.nextElement()); try { ClassReader cr = new ClassReader(clazz); ClassNode cn = new ClassNode(); cr.accept(cn, ClassReader.SKIP_DEBUG); List<MethodNode> methods = cn.methods; for (MethodNode method : methods) { int count = method.instructions.size(); System.out.println(count + " " + cn.name + "." + method.name); } } catch (IllegalArgumentException ignored) { } } } } 
+1
source

Checkstyle will probably be good for this - it does not work with a limit of 8k, but with the number of executable statements in the method as a whole. Honestly, this is the limit you want to practice, though.

As you said, -XX:-DontCompileHugeMethods is usually a bad idea - it makes the JVM dig all this ugly code and try to do something with it, which can negatively affect performance, not positive! Refactoring, or even better not writing methods that could be started from the very beginning, is the way forward.

Oh, and if the methods that were huge ended there through some kind of human design, rather than automatically generated code, then there are probably some people on your team who need to talk to ...

0
source

All Articles