views:

112

answers:

8

I'm using this statement

//some code
int a[][]=new int[5000000][5000000];
//some code

and running it with command

java -mx512m Test

It is giving OutOFMemoryError: Java Heap space indicating the line number of the mentioned statement in the stacktrace

How do i solve this problem

Edit: I'm trying to solve a practice problem on codechef

A: 

You are using too much memory. Use less or have more. Each int element is 32-bits. And your memory limit of 512MB is much less than you think it is.

Gary
A: 

for int[5000000][5000000] you need 100000000000000B or 100000GB. So you only can wait about 100 years when something like that is going to be possible.

Klark
You really think it'll take 100 years for 100000 GB to be economical? How about a friendly wager?
Mark Peters
The winner gets a 10000 Gb USB key.
Thorbjørn Ravn Andersen
+8  A: 

You may need to consider an approach to your problem which requires less memory.

From Google Calculator (assuming a 64bit integer size):

(5 000 000^2) * 64 bits = 186 264.515 gigabytes
pmalmsten
wow that is a huge array!
Bob Fincheimer
A java int is sepcified as 32 bits though.
Michael Borgwardt
I'm not very familiar with the details of Java, so I assumed the default integer size for a 64-bit computer. So I might be off by 93,000 GB or so.
pmalmsten
+3  A: 

Well, that's 25 trillion ints, each of which takes 4 bytes, so 100 trillion bytes overall. Easiest solution is to buy ~90 terabytes of RAM and a 64 bit OS.

Seriously though, the correct solution is probably to allocate a more reasonable data structure that can store the data more efficiently, assuming that you don't actually need to load 90 terabytes of data into RAM at once. Perhaps if you post more about the problem, we can give a better answer?

dsolimano
Actually I'm trying to solve a practice problem on codechef http://www.codechef.com/problems/PRIMPATT/. I think i will need a matrix of this size. Any help is appriciated
Shwetanka
If solving the problem by modeling the entire field in memory leads you to need a 90 terabyte array, that's a pretty good signal that there's a better way to solve the problem. I would think you would have to figure out a way to determine whether a given square is black, without actually simulating the entire process that the robot goes through.
JacobM
Yes i think i should change the approach. But one thing If i convert the type from int to byte will it help?
Shwetanka
You'd only be allocating 1B * 5M * 5M = 25 TB. Want to squezze it into a bit? It's still 3TB. Do you really think this'll get you anywhere?
meriton
A: 

You need a more enlightened data structure. Unless you actually NEED 25 trillion integers, which is doubtful.

Adrian
A: 

That's a matrix with 5 million columns and 5 million rows. Is that really what you wanted?

Andreas_D
A: 

Amongst the other answers, you have a typo in your command.

It should be: java -Xmx512M Test

Noel M
+4  A: 

I think the data structure you are looking for is a sparse matrix. Store your elements along with their coordinates in a map data structure (eg. Map<Integer,Map<Integer,Integer>> for a 2d sparse array) and just assume anything not in the map is zero.

Doug
Yes I think i'll go this way.. it'll save memory as well as iteration time
Shwetanka
+1 for the idea of a sparse matrix, though I'd suggest using a key type that doesn't require so much complication. Like `Map<Point, Integer>` or some other purpose-made key class that is just a container for the two ints.
Mark Peters