3

I'm new to Java and I've been wondering lately about memory consumption of huge, but partly empty array like in this case:

  • I need array of unknown size- it can be 300k of values, it can be 300m, but to keep in mind it might as well keep only like 50 values,
  • i'm initialising array with size of

    int values[] = new int[Integer.MAX_VALUE];
    
  • user generates some number of values that has to be stored in array every time given generating method is used

Is there any contraindication of using excessively huge arrays? How the size of array matters in case of performance when, say only 0,1% of that array is used vs 100% array usage?

In other words- when im calling empty array of X int values, will the JVM on initialisation reserve memory for X*(memory used to store 1 int value) even if there isn't any value stored yet?

Thanks, Daniel.

8
  • What's wrong with an ArrayList? Commented Jun 26, 2016 at 13:29
  • 1
    @BoristheSpider are you seriously suggesting using an ArrayList of size Integer.MAX_VALUE for storing int values? Just no. Commented Jun 26, 2016 at 13:30
  • The OP says up to 300m values; this should be fine for an ArrayList. Commented Jun 26, 2016 at 13:32
  • @BoristheSpider I wouldn't make sense to me to use an ArrayList here, since there is only one reason why OP might create such a large array, but doesn't fill it completely (as far as I see it): he prefers to access the items by their index. So an ArrayList is no help here. A Map should be more suited, with Integer as the key. Commented Jun 26, 2016 at 14:03
  • 1
    @BoristheSpider my assumption from the question was that the indexes aren't known. So "300m" values doesn't mean 300 million values ranging from 0 through 299999999, it means 300 million arbitrary values. An ArrayList is not a good choice in this case, especially considering the overhead of storing an int (as an object) as compared to storing into an int[]. Commented Jun 26, 2016 at 14:16

2 Answers 2

6

How the size of array matters in case of performance when, say only 0,1% of that array is used vs 100% array usage?

There's no such thing as an "empty array of X int values". Every element will have a value - it'll just be 0 by default. Every element will take 4 bytes, regardless of its value. Your new int[Integer.MAX_VALUE] array will take 8GB of memory.

If you want to create a sparse array, you might want to consider using a Map instead. However, note that while you can have an int array, a map would need an Integer type for the value - if you get outside the range of cached Integer values (and if you have a lot of different values) you could end up wasting a lot of space on Integer objects (and references to them).

Sign up to request clarification or add additional context in comments.

2 Comments

Or ArrayList, or any kind of List really.
@Brandon: Well, yes to ArrayList - but it would be perfectly feasible to create a List implementation designed for sparse population.
0

When you initialize an array, you reserve a place in memory for it. This memory size will not change depends on the values in the array. The default value in int array is 0, so your array is not partially empty.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.