[英]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 它给出了OutOFMemoryError:Java堆空间,指示堆栈跟踪中提到的语句的行号
How do i solve this problem 我该如何解决这个问题
Edit: I'm trying to solve a practice problem on codechef 编辑:我正在尝试解决codechef上的练习问题
You may need to consider an approach to your problem which requires less memory. 您可能需要考虑一种需要较少内存的问题方法。
From Google Calculator (assuming a 64bit integer size): 来自Google Calculator(假设64位整数):
(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. 将元素及其坐标存储在地图数据结构中(例如
Map<Integer,Map<Integer,Integer>>
对于2d稀疏数组Map<Integer,Map<Integer,Integer>>
),并假设不在地图中的任何内容为零。
Well, that's 25 trillion ints, each of which takes 4 bytes, so 100 trillion bytes overall. 那么,这是25万亿英镑,每个占用4个字节,总计100万亿字节。 Easiest solution is to buy ~90 terabytes of RAM and a 64 bit OS.
最简单的解决方案是购买~90TB的RAM和64位操作系统。
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. 但说真的,正确的解决方案可能是分配一个更合理的数据结构,可以更有效地存储数据,假设你实际上并不需要一次将90TB的数据加载到RAM中。 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
它应该是:
java -Xmx512M Test
You are using too much memory. 你使用的内存太多了。 Use less or have more.
少用或少用。 Each int element is 32-bits.
每个int元素都是32位。 And your memory limit of 512MB is much less than you think it is.
你的内存限制为512MB远远低于你的想象。
for int[5000000][5000000] you need 100000000000000B or 100000GB. 对于int [5000000] [5000000],您需要100000000000000B或100000GB。 So you only can wait about 100 years when something like that is going to be possible.
所以你只能等待大约100年才能实现这样的目标。
You need a more enlightened data structure. 您需要一个更加开明的数据结构。 Unless you actually NEED 25 trillion integers, which is doubtful.
除非你实际上需要25万亿个整数,这是值得怀疑的。
That's a matrix with 5 million columns and 5 million rows. 这是一个包含500万列和500万行的矩阵。 Is that really what you wanted?
这真的是你想要的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.