简体   繁体   中英

Overhead: method call vs. object creation

As far as I understand the JVM, it should generally be cheaper to invoke a method (ie allocate a new stack frame, etc), than to create a new object.

However, can we estimate just how big the difference in overhead between the two generally is, assuming that both the method and the object declare the same number of local/instance variables of the same type, and instantiate them to the same values?

In modern JVMs object creation is extremely fast operation. See this article , for example. It says: "Sun estimates allocation costs at approximately ten machine instructions".

Even though in that scenario method() needs to be scaffolded into a class ??

and registering class in JVM heap itself includes member (method()) meme allocation.

so , in any case object creation is little bit on higher side.

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