Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does an IntBuffer compare to an array in terms of memory usage?

Tags:

java

So I have a big list of integers I have to work with (in the order of 70Mb). As a part of the process of reading them, I need to temporarily store them. I can either spread them over several IntBuffers, or allocate a couple of large arrays.

I couldn't find any documentation however on how an IntBuffer compares to an array in terms of memory usage (with all the metadata that java adds). Does anyone know anything about this?

like image 678
Bartvbl Avatar asked Dec 05 '25 20:12

Bartvbl


2 Answers

There is very little difference between a int[] and a heap IntBuffer or a direct IntBuffer in terms of memory usage esp if the arrays are large. (There is a small over head)

In terms of performance int[] is the fastest and a direct IntBuffer with native byte ordering is the second fastest. The advantage of the IntBuffer is that its off heap and you can have much larger sizes e.g. 70 GB without increasing the heap size or the Full GC times.

For a 70 MB array, it small enough that the simplest solution int[] is likely to be the best. (It will also be the easiest to write and most efficient to run)

like image 63
Peter Lawrey Avatar answered Dec 08 '25 08:12

Peter Lawrey


Relative to 70MB of ints, the metadata an IntBuffer adds is nothing. But a profiler will give you a more complete answer.

like image 33
John Watts Avatar answered Dec 08 '25 09:12

John Watts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!