如何解决 ArrayList 大小的 Java 堆空间异常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13579981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to solve Java heap space Exception for ArrayList Size?
提问by vij
When i run the below program which displays subsets of a given array, where array size is <=19 , it works fine. But if array size >19 it throws Java Heap Space Exception. How to overcome this problem in order to get subsets where array size > 19?
当我运行以下显示给定数组子集的程序时,其中数组大小 <=19 ,它工作正常。但是如果数组大小 >19,它会抛出 Java 堆空间异常。如何克服这个问题以获得数组大小 > 19 的子集?
import java.lang.*;
import java.util.*;
class def
{
static List<List<Integer>> subset(int ele,List<List<Integer>> w)
{
if(w.size()==0)
{
List<Integer> temp=new ArrayList<Integer>();
temp.add(ele);
w.add(temp);
}
else
{
int i,len=w.size();
for(i=0;i<len;i++)
{
List<Integer> temp=new ArrayList<Integer>();
for(Integer agh:w.get(i))
{
temp.add(agh);
}
temp.add(ele);
w.add(temp);
}
List<Integer> ghi=new ArrayList<Integer>();
ghi.add(ele);
w.add(ghi);
}
return w;
}
static void sub(int set[])
{
List<List<Integer>> ints = new ArrayList<List<Integer>>();
int len=set.length,i;
for(i=0;i<len;i++)
{
ints=subset(set[i],ints);
}
int count=0;
for(List<Integer> temp:ints)
{
System.out.println("SET---"+count++);
for(Integer agh:temp)
{
System.out.println(agh);
}
}
}
public static void main(String args[])
{
int a[]={3,4,9,14,15,19,28,37,47,50,54,56,59,61,70,73,78,81,92,95,97,99};
sub(a);
}
}
Here is the exception: C:\Program Files (x86)\Java\jdk1.6.0_07\bin>javac def.java
这是例外:C:\Program Files (x86)\Java\jdk1.6.0_07\bin>javac def.java
C:\Program Files (x86)\Java\jdk1.6.0_07\bin>java def
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2760)
at java.util.Arrays.copyOf(Arrays.java:2734)
at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
at java.util.ArrayList.add(ArrayList.java:351)
at def.subset(def.java:22)
at def.sub(def.java:39)
at def.main(def.java:55)
回答by Debobroto Das
It seems that you are making too much instance of ArrayList. You can increase the heap size. but I think you can make a small modification in your code
看来您正在制作太多的 ArrayList 实例。您可以增加堆大小。但我认为你可以对你的代码做一个小的修改
else
{
int i,len=w.size();
List<Integer> temp=new ArrayList<Integer>(); ///reuse this arraylist
for(i=0;i<len;i++)
{
for(Integer agh:w.get(i))
{
temp.add(agh);
}
temp.add(ele);
w.add(temp);
temp.clear();
}
List<Integer> ghi=new ArrayList<Integer>();
ghi.add(ele);
w.add(ghi);
}
return w;
Though this may not solve your problem fully. But obviously it will help the garbage collector to take some rest. :D
虽然这可能无法完全解决您的问题。但显然它会帮助垃圾收集器休息一下。:D
回答by BevynQ
The reason you are getting a heap error is because you are creating billions of lists. Do not create sublists just display them.
您收到堆错误的原因是您正在创建数十亿个列表。不要创建子列表只是显示它们。
public static void sub(int[] input){
sub(new int[0],input);
}
public static void sub(int[] current, int[] remaining){
System.out.println(Arrays.toString(current));
int[] newCurrent = Arrays.copyOf(current, current.length+1);
for(int i = 0;i < remaining.length;i++){
newCurrent[newCurrent.length-1] = remaining[i];
sub(newCurrent , Arrays.copyOfRange(remaining, i + 1, remaining.length));
}
}
otherwise you will need a smarter data structer than a list of lists.
否则,您将需要一个比列表列表更智能的数据结构。
回答by ice
To reduce memory consumption you can store sets as bit fields, for example set with elements 3, 9, 14
will be represented as 10110000000000000000
- so for each subset you will need only one int
为了减少内存消耗,您可以将集合存储为位字段,例如带有元素的集合3, 9, 14
将表示为10110000000000000000
- 因此对于每个子集,您只需要一个int