【发布时间】:2014-12-24 17:23:08
【问题描述】:
我需要浏览大约 3000 个文件夹,每个文件夹包含 300 个 CSV 文件。
这是((nextLine=csvReader.readNext()) != null):
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
at au.com.bytecode.opencsv.CSVParser.parseLine(CSVParser.java:206)
at au.com.bytecode.opencsv.CSVParser.parseLineMulti(CSVParser.java:174)
at au.com.bytecode.opencsv.CSVReader.readNext(CSVReader.java:237)
at DA.readTelemetryData(DA.java:78)
at DA.main(DA.java:24)
问题是如何解决这个问题?为什么会发生,我的代码有什么问题?
这里我提供代码:
private static HashMap<Integer,HashMap<Integer,List<double[]>>> readTelemetryData() throws Exception
{
HashMap<Integer,HashMap<Integer,List<double[]>>> xy_total = new HashMap<Integer,HashMap<Integer,List<double[]>>>();
for (int i=0; i<Constants.MAX_FOLDERS; i++)
{
HashMap<Integer,List<double[]>> xy_total_per_folder= new HashMap<Integer,List<double[]>>();
for (int j=0; j<Constants.MAX_FILES_INSIDE_FOLDER; j++)
{
CSVReader csvReader = null;
File f = new File("data/"+ (i+1) +"/"+ (j+1) +".csv");
if(f.exists())
{
csvReader = new CSVReader(new FileReader(f));
List<double[]> xyArr = new ArrayList<double[]>();
String[] firstLine=csvReader.readNext();
if (firstLine != null)
{
String[] nextLine=null;
while ((nextLine=csvReader.readNext()) != null)
{
double[] d = new double[2];
d[0]=Double.parseDouble(nextLine[0]);
d[1]=Double.parseDouble(nextLine[1]);
xyArr.add(d);
}
}
xy_total_per_folder.put(j, xyArr);
csvReader.close();
}
}
xy_total.put(i, xy_total_per_folder);
}
return xy_total;
}
【问题讨论】:
-
你有什么问题?
-
@talex:问题是如何解决这个问题?为什么会发生,我的代码有什么问题?我认为我应该避免使用 HashMap,但我不确定。
-
你的代码没问题。如果您需要所有这些数据,这是一个简单的解决方案。增加内存限制。用
-XMX=1G运行java -
对不起。标志是
-Xmx=1g -
@talex:好的,谢谢。但是在这种情况下使用 HashMap 是个好主意吗? HashMaps 比数组慢得多吗?
标签: java filereader opencsv