6

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)

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Yojimbo
  • 209
  • 1
  • 7

1 Answers1

10

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.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • @t-j-crowder, you said "** even a structure such as a Map that doesn't use arrays at all **". As far as I know any Map implementation in java use a backed array. for eg: HashMap and HashTable both use backend array to storing map entires. Is that true ?. – Pandiri Jan 06 '15 at 12:53
  • @RavinderReddy: A particular implementation may use one or more arrays. Or not. That's an implementation detail. – T.J. Crowder Jan 06 '15 at 12:59
  • 1
    @Ravinder Reddy: `TreeMap`, for example, does not use an array. – Holger Jan 06 '15 at 14:17