Java Overhead

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.

+4
source share
5 answers

These results are not surprising. JVM adds a huge amount of overhead for each object.

About doubling the expected size for a single object due to excessive memory usage, the JVM is not unusual.

This presentation provides a wonderful, detailed explanation and overview of the memory usage of various data structures in Java.

+4
source

ArrayList ist more than the number of elements. Use getCapacity() to get the current size of the underlying array.

+2
source

The big problem with your approach is interacting with the garbage collector. It basically does any test, as you suggested, completely opaque from the outside.

Like a thought experiment, if you want to do this, you must

  • start the JVM and make a couple of global GCs to get all the unwanted messages.
  • Measure the heap size and the Java view of how much free space it has.
  • Run the test
  • Gc a couple of times
  • Repeat measurements from step # 2

After all this and a little math, you will be closer, but still wrong. The only real solution is to actually ask for an implementation, as other people have mentioned. Or understand this from knowledge of implementation.

+2
source

The memory consumed by the arraylist is a bit vague.

Take a dump of the process heap at the appropriate stage - after the values ​​have been fully assigned. Then use tools such as a memory analyzer (from eclipse).

You fill in finding the small and saved heap sizes.

+1
source

As a note, since you know exactly how many objects will be in your ArrayList, why not just use the [] array? Will the number of objects inside be changed?

0
source

Source: https://habr.com/ru/post/1308135/


All Articles