开发者

What is the memory footprint of a method intensive Java object?

开发者 https://www.devze.com 2022-12-13 03:05 出处:网络
Suppose I have a Java class that has 100K worth of method code containing NO variables, but only 20 bytes of attributes.

Suppose I have a Java class that has 100K worth of method code containing NO variables, but only 20 bytes of attributes.

I instantiate 1000 objects from this class.

Have I consumed 100,000K of memory? Or only 100K + (20bytes * 1000)? Or something else altogethe开发者_运维问答r?


The memory footprint for loading the class itself will approximately correspond to the code size, but the code will not be duplicated for each instance of the class. An instance will only require as much memory as the instance attributes + some overhead for managing the object instance itself.


Here's a general guide to determining the memory usage of objects in Java: http://www.javamex.com/tutorials/memory/object_memory_usage.shtml

I suspect that the memory usage incurred from method definitions will be a fixed amount, (possibly put into permgen space when the class is loaded) rather than proportional to the number of instances.


You will have instantiated only the 'variable' part of the objects, so only the attributes (and I guess a few bytes overhead here and there). Your latter guess is the correct one.


Try attaching with jvisualvm (in the JDK) and tell us what you see.

0

精彩评论

暂无评论...
验证码 换一张
取 消