Overhead: method invocation versus object creation

As far as I understand the JVM, it is generally cheaper to call a method (i.e. allocate a new stack stack, etc.) than to create a new object.

However, can we estimate how big the difference between the overhead is between them in general, assuming that both the method and the object declare the same number of local / instance variables of the same type and create them for the same values

+3


source to share


2 answers


In modern JVM objects, object creation is very fast. See the article for example. It says, "Sun estimates allocation costs at about ten machine instructions."



+1


source


Despite the fact that in this scenario the () method needs to be colored to the class

and registering the class on the JVM heap itself enables the allocation of the meme member (method ()).



so object creation is a bit on the side above anyway.

0


source







All Articles