I would like to ask about Memory Overhead in java, I have a large ArrayList array (61,770 elements) and an attempt to calculate the amount of memory taken by each element (counting the object and its ArrayList record) profiling the application, I get that after loading all the data a lot takes ~ 25 Mb. when an ArrayList has only 2 elements, the heap takes ~ 1 MB, so rude:
(24 * 1024 * 1024) / 61 768 = 407 bytes.
however , when I count the fields of each object, I get 148 bytes (not including ArrayList and assuming int = 4, float = 4, reference = 4), I'm curious to know where all these extra bytes came from ...
I can guess that since the objects stored in ArrayList implement the interface, they store additional values, maybe the VM stores a function pointer 4 bytes for each method implemented? the interface they implement has 20 functions, so 80 bytes for a total of 228 bytes are still not close to the measured 400 bytes.
Any help would be appreciated.
wow, thanks for all the great answers.
@Bolo: thanks for the link, this class I measures ~ 350 bytes per object, so I can least confirm the source of using large memory.
@Yuval A: Thank you for this presentation, a valuable source of information.
@Ukko: point marked.
@Jayan: Right now, the NetBeans profiler is giving me errors when I try to dump the heap, I will try again later.