在代码库中搜索大型方法

发布于 2024-11-28 07:56:44 字数 345 浏览 4 评论 0原文

默认情况下,HotSpot JIT 拒绝编译大于约 8k 字节码的方法 (1)。有没有什么可以扫描 jar 中的此类方法 (2)?

  1. 除非你传递-XX:-DontCompileHugeMethods

  2. Jon Masamitsu 描述了解释方法如何减慢垃圾收集速度,并指出重构是通常比 -XX:-DontCompileHugeMethods

By default the HotSpot JIT refuses to compile methods bigger than about 8k of bytecode (1). Is there anything that can scan a jar for such methods (2)?

  1. unless you pass -XX:-DontCompileHugeMethods

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风透绣罗衣 2024-12-05 07:56:44

感谢 Peter Lawrey 对 ASM 的指导。该程序打印出 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) {
            }
        }
    }
}

Thanks to Peter Lawrey for the pointer to ASM. This program prints out the size of each method in a 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) {
            }
        }
    }
}
_失温 2024-12-05 07:56:44

Checkstyle 可能对此很有用 - 它不适用于 8k 限制,但适用于一般而言,方法中的可执行语句。老实说,这是您在实践中想要的限制。

正如您已经指出的,-XX:-DontCompileHugeMethods 通常是一个坏主意 - 它迫使 JVM 挖掘所有丑陋的代码并尝试用它做一些事情,这可能会对性能产生负面影响而不是积极的!重构,或者最好不要一开始就编写如此庞大的方法,这将是前进的方向。

哦,如果如此巨大的方法最终是通过一些人为设计而不是自动生成的代码而结束的,那么您的团队中可能有人需要与......

Checkstyle would probably be good for this - it doesn't work on the 8k limit but the number of executable statements in a method in general. To be honest, this is a limit that you want in practice though.

As you already state, -XX:-DontCompileHugeMethods is generally a bad idea - it forces the JVM to dig through all that ugly code and try and do something with it, which can have a negative effect on performance rather than a positive one! Refactoring, or better still not writing methods that huge to start with would be the way forward.

Oh, and if the methods that huge ended up there through some human design, and not auto-generated code, then there's probably some people on your team who need talking to...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文