0

How to declare an array of size 10^9 in java ?.I have tried Array list but the problem is i need to find minimum and maximum element in array and so I need to compare 0th element of an array with all other elements of array and i initially need some fixed size of array which is required in the input format of array on code-chef. Can anybody help?. I tried using long array but it gave out of memory error.

15
  • 1
    can you add more code snippet of what you are tying to do? Commented Jun 14, 2016 at 15:04
  • Just new Whatever[1000000000] to create the array. But if that's an array of references, and if we assume 4 bytes per reference, then that's ~4 GB of data. If you're running out of memory, try working with -Xmx parameters. (That said, if you only want to fetch out the min and max values of N elements, you can do that in constant space -- no need to load up the whole array.) Commented Jun 14, 2016 at 15:06
  • 4
    If it's on code chef, there almost certainly is a way to do what is being asked without storing all that data in memory. You should explain what problem you are really trying to solve - at the moment it sounds like an XY Problem. Commented Jun 14, 2016 at 15:09
  • 1
    @vidhit I have not read in detail but it appears that the size of the array is N and N is 10^5 max (i.e. 100,000, which should easily fit in memory). And the input contains the value of N, so you only need to build an int[N] with N being whatever is on the first line of the input. Commented Jun 14, 2016 at 18:39
  • 1
    @ErickG.Hagstrom Actually I got interested and couldn't resist writing a solution: you don't need to store the numbers at all - no array involved... Commented Jun 14, 2016 at 18:59

1 Answer 1

2

A Java array can have a maximum size equal to Integer.MAX_VALUE (or in some cases a slightly different value) which is about 2.3 * 10^9, so creating that large an array is theoretically possible. However since 10^9 would mean the prefix giga (to make it easier to read) the array would have a size of at least 1GB (when using byte[]). Depending on the data type you're using the array probably just takes too much memory (an int[] would already take up 4GB).

You could try to increase the JVM's max memory by using the -Xmx option (e.g. to allow a maximum of 4GB you could use -Xmx=4g) but you're still limited by the maximum of adressable memory (e.g. IIRC a 32-bit JVM can only adress up top 4GB in total) and available memory.

Alternatively you could try and split the array over multiple machines or JVMs and employ some distributed approach. Or you could write the array to a (memory mapped) file and keep only a part of the array in memory.

The best approach, however, would probably be to check whether you really need that much memory. In many cases using some clever algorithms or structures can dramatically reduce memory requirements. What to use depends on what you're trying to achieve in the end though.

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

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.