0

How to take an array of size<=10^8 as input and perform operations on it? arr[i]<=10^8.

The below code gives runtime error and java.lang.OutOfMemoryError. I have got this question multiple times and never got the answer as it gives error for large values of n and arr[i]. I know that it can't be done in the way given below. So the question is, how can it be done in Java ?

n=100000000;//User input
int[] arr=new int[n];
int[] b=new int[n];
for(int i=0;i<n;i++)
{
    b[i]=arr[i]+1;
}
  • 1
    2 arrays of such size are ~800MB of RAM and you must have a large enough contiguous region of memory in order to allocate successfully. Are you on a 32-bit OS? But what's the real problem? Are you doing some competition? It's very rare that you're required to read a huge array like that – phuclv Oct 06 '20 at 06:59
  • Oops, it will actually be [heap size](https://stackoverflow.com/q/4667483/1270789) that may be limited; anyway, one of the two should give you the answer. – Ken Y-N Oct 06 '20 at 07:00
  • I got this question in multiple competitions and just don't know the correct way to do operations on array of this size. – Devarshi Kothari Oct 06 '20 at 14:42
  • no, you don't operate on arrays this size. You need to **change the algorithm**, usually it'll just need at most a O(n.logn) space, although most probably O(1) or O(n) and will be much faster than brute forcing like this – phuclv Oct 06 '20 at 23:51

3 Answers3

0

First, investigate if it is really necessary to keep the whole array in memory. In many cases you can perform the required operation by just working on one element at a time.

If there is really a need to store the array, you allocate in the example two int arrays with 1e8 elements each. They will roughly need 800MB of memory. This is not out of reach on modern hardware. Just make sure you start the JVM with enough heap space. See the -Xmx and -Xms options.

Henry
  • 40,427
  • 6
  • 56
  • 72
0

Your memory is divided between Xmx (young and old), permanent, stack and misc zones. You need expand Xmx memory for working with bigger objects. example of dividing java memory

0

The limitation here is always going to be constrained by physical memory available. The Java primitive type int requires 32 bits (4 bytes), 100 million ints requires roughly 400MB of memory.

The default JVM max heap size differs for each version of Java. Let's take Java 8, for example, according to Oracle Java 8 documentation

maximum heap size

Smaller of 1/4th of the physical memory or 1GB. Before Java SE 5.0, the default maximum heap size was 64MB. You can override this default using the -Xmx command-line option.

As you need two arrays you'll need 800MB

You will need to start java with say -Xms800M (this will set the initial heap size 800MB) and set the maximum heap size to something appropriate to you system, let's say 1GB -Xms1G

MarkAddison
  • 166
  • 7