Does adding a method to a Java class increase the memory usage of its instance?
•
Java
Adding a member variable to a class will certainly increase the memory usage of its instance on the heap, but what about the method of the class?
For example, if I have a class composed of a single StringBuilder, but continue to add many methods to operate StringBuilder, will each instance of the class occupy more memory when I add more methods?
thank you!
Solution
No, adding methods does not increase the space occupied by objects, but only the space occupied by compiled code In other words, the memory cost per instance is zero
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
二维码