Java – search the code base for large methods
•
Java
By default, hotspot JIT refuses to compile methods larger than 8K bytecode (1) Is there any way to scan cans (2)?
>Unless you pass - XX: - dontcompilehugemethods > Jon Masamitsu describes explaining how methods slow down garbage collection and points out that refactoring is usually smarter than - XX: - dontcompilehugemethods
Solution
Thank Peter Lawley for pointing to ASM This program prints out the size of each method in the jar:
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) { } } } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码