简体   繁体   中英

OutOfMemory Error java heap space

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

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

我遇到了Eclips关注Java堆的同样问题,解决方法是将mx512m修改为mx4096m或mx2048m(扩展允许的最大内存限制)所以在你的情况下尝试命令java -mx4096m Test将允许java使用4你公羊的GB

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.

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?

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

It should be: java -Xmx512M Test

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM