Reputation: 1677
Adding member variables to a class certainly boosts the memory footprint of its instances on the heap, but what about a class's methods?
For example, if I have a class composed of a single StringBuilder, but keep adding dozens of methods to manipulate that StringBuilder, will each instance of that class take up proportionally more memory as I add more methods?
Thanks!
Upvotes: 8
Views: 1384
Reputation: 30237
This is VM specific, but for the Oracle Hotspot VM that everybody uses, no, the instance size is not affected.
Upvotes: 3
Reputation: 726889
No, adding methods does not increase the footprint of the object, only the footprint of the compiled code. In other words, the per-instance memory cost is zero.
Upvotes: 14
Reputation: 18488
The short answer is no, it will not add more memory to each object living in the heap. The only thing that will happen is when the class is first loaded, it will also load this methods.
Upvotes: 5