在java中调用空方法是否使用资源? [英] Does calling an empty method in java use resources?

查看:62
本文介绍了在java中调用空方法是否使用资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问的原因是因为在我的程序中,我有一个父类,其中包含子类可能会或可能不会使用的方法.我只是想知道编译器是否为每个子实例优化了这个,或者它是否需要少量资源来检查是否有什么东西存在?

The reason I am asking is because in my program I have a parent class which holds methods which may or may not be used by the child class. I am just curious to know if the compiler optimizes this for each child instance, or does it require the small bit of resource to check to see if something is there?

推荐答案

JVM 使用某些启发式方法来决定要内联哪些方法.该方法需要是一个热点(即被调用足够多的次数以使内联有价值),它不应该很大,也不应该被子类覆盖(这会修改其行为并使优化无效).可能还有其他启发式方法.

The JVM uses certain heuristics to decide what methods to inline. The method needs to be a hotspot (i.e. being invoked enough times to make the inlining worthwhile), it shouldn't be very big and it shouldn't be overridden by child classes (which would modify its behaviour and invalidate the optimization). There are probably other heuristics too.

在您的情况下,如果这些空方法从未被覆盖,并且它们被调用的次数足够多,它们可能会被内联.

In your case, if these empty methods are never overridden, and they are invoked enough times, they will probably be inlined.

除了猜测之外,您还可以启用 java 命令行标志来查看 JIT 正在做什么(即它是否内联了方法).请参阅此博客文章:https://techblug.wordpress.com/2013/08/19/java-jit-compiler-inlining/

Instead of guessing, you can also enable java command line flags to see what the JIT is doing (i.e. if it inlines or not the methods). See this blog post: https://techblug.wordpress.com/2013/08/19/java-jit-compiler-inlining/

这篇关于在java中调用空方法是否使用资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆