How much memory is an intensive Java object?

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

I am instantiating 1000 objects from this class.

Did I use 100,000 KB of memory? Or just 100K + (20bytes * 1000)? Or something else?

+6
java memory-management
source share
4 answers

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

+10
source share

Here is a general guide to determining memory usage in Java: http://www.javamex.com/tutorials/memory/object_memory_usage.shtml

I suspect that the memory usage obtained from the method definitions will be a fixed value (possibly placed in the permento space when loading the class), and not proportional to the number of instances.

+2
source share

You will create an instance of only the "variable" part of the objects, so only attributes (and, I think, a few bytes overhead here and there). The last assumption is true.

0
source share

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

0
source share

All Articles