6

I need to create a space efficient 2D array for a large number of 8 bit values. I began writing my class using a few layers of abstraction and generics to allow for code reuse. Once I got to implementing the concrete class it occurred to me I cannot pass in a primitive type as a generic class argument and I would have to use a wrapper class. Because I am concerned about space efficiency, I need to know: what is the space efficiency difference between a Byte array using the wrapper class compared to a primitive byte array?

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
recursion.ninja
  • 5,377
  • 7
  • 46
  • 78
  • but why use generics on 8 bit values ? what is the purpose of different types if you explicitly store 8 bit values ? – giorashc Apr 03 '13 at 13:31
  • @giorashc I might want to later store `objects` or `ints` in the same unique way I am currently storing 8 bit values, The 2D array is the concrete storage implementation but the class's data structure access control is unique. – recursion.ninja Apr 03 '13 at 13:38

3 Answers3

6

Yes, primitives are light-weight compared to corresponding Wrapper class objects.

You can read about it here : Primitives vs Wrappers

giorashc
  • 13,691
  • 3
  • 35
  • 71
IndoKnight
  • 1,846
  • 1
  • 21
  • 29
4

Accroding to http://www.javamex.com/tutorials/memory/object_memory_usage.shtml

byte[] size ~= 12 + length

Byte[] size ~= 12 + 20 * length (20 = 16 + 4 the size of 1 Byte object + 4 bytes reference)

so, Byte[] may take 20 times more memory than byte[]. It is actually maximum, it depends on the way you create a Byte. new Byte is always a new Object, Byte.valueOf is always a cached instance. It also depends on CPU, for x64 each reference takes 8 bytes.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • I don't know whether `Byte` objects are cached, or just integers, but even if they're not I would think that for any size `Byte` array one could use a static `Byte[256]` to hold an instance of each value; the overhead for that would be fixed at ~4096 bytes, each `Byte[]` array that used cached instances would typically take 4 bytes per entry. – supercat Apr 03 '13 at 16:11
  • It is 4 bytes for each reference to a Byte object which is 16 bytes. As for caching it depends on how you create it. new Byte is always a new object. – Evgeniy Dorofeev Apr 03 '13 at 16:26
  • @Evgeniy Dorofeev: The reference takes 8 bytes on a 64-bit JVM without compressed OOPs. – maaartinus Oct 28 '13 at 19:49
1

Watch this other question: Wrappers of primitive types in arraylist vs arrays

The big issue with double versus Double is that the latter adds some amount of memory overhead -- 8 bytes per object on a Sun 32-bit JVM, possibly more or less on others. Then you need another 4 bytes (8 on a 64-bit JVM) to refer to the object.

So, assuming that you have 1,000,000 objects, the differences are as follows:

double[1000000]

8 bytes per entry; total = 8,000,000 bytes

Double[1000000]

16 bytes per object instance + 4 bytes per reference; total = 20,000,000 bytes

Whether or not this matters depends very much on your application. Unless you find yourself running out of memory, assume that it doesn't matter.

Community
  • 1
  • 1
Waji
  • 71
  • 3