Currently I'm using the following data structure:
MyObject[] arr = new MyObject[100]
now most of the fields in the Array are actually null (let's say 97%)
As far as I can see the JVM reserves for each field the required memory for one instance of MyObject
so a lot of memory is reserved even though it is never really used.
Is there a way to save memory? Like only allocate the space for MyObject
on demand?
Is there a better data structure then just a simple array? (needs to be both mem efficient and fast)
NULL cannot be used as a “missing value” placeholder in an array.
In Java, null is just a value that a reference (which is basically a restricted pointer) can have. It means that the reference refers to nothing. In this case you still consume the space for the reference. This is 4 bytes on 32-bit systems or 8 bytes on 64-bit systems.
An array value can be non-empty, empty (cardinality zero), or null. The individual elements in the array can be null or not null. An empty array, an array value of null, and an array for which all elements are the null value are different from each other. An uninitialized array is a null array.
In Java, a null value can be assigned to an object reference of any type to indicate that it points to nothing. The compiler assigns null to any uninitialized static and instance members of reference type.
As far as I can see the JVM reserves for each field the requiered memory for one instance of MyObject so a lot of memory is reserved even though it is never really used.
No, it doesn't. It reserves enough memory for 100 object references, not 100 instances of MyObject
. An object reference is not big (32 or 64 bits, see this answer for details). ASCII-art below.
Now, if you're bothered by even reserving 100 slots that are the size of an int
or long
, you could use ArrayList
which will reallocate a backing array as necessary (but this can add to memory fragmentation), a LinkedList
which doesn't use an array at all (but has higher memory overhead for the entries that it does have), or even a structure such as a Map
that doesn't (necessarily) use arrays at all (but again has higher overhead per entry).
ASCII-art and details for the 100-place array:
So when you first create that array, here's what you have in memory:
+-----------+ | arr | +-----------+ +------------+ | reference |------>| MyObject[] | +-----------+ +------------+ | null | | null | | null | | (96 more) | | null | +------------+
Then you assign an instance, say:
arr[1] = new MyObject();
That gives you
+-----------+ | arr | +-----------+ +------------+ | reference |------>| MyObject[] | +-----------+ +------------+ | null | +-------------------+ | reference |---->| MyObject instance | | null | +-------------------+ | (96 more) | | someField | | null | | someOtherField | +------------+ | ... | +-------------------+
...then maybe you add another:
+-----------+ | arr | +-----------+ +------------+ | reference |------>| MyObject[] | +-----------+ +------------+ | null | +-------------------+ | reference |---->| MyObject instance | | reference |--+ +-------------------+ | (96 more) | | | someField | | null | | | someOtherField | +------------+ | | ... | | +-------------------+ | | +-------------------+ +->| MyObject instance | +-------------------+ | someField | | someOtherField | | ... | +-------------------+
...and so on as you store more instances in the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With