使用opencsv - java.lang.OutOfMemoryError:Java堆空间

时间:2013-12-31 11:42:37

标签: java eclipse csv opencsv

我使用opencsv来解析两个csv文件。我只复制这两个文件中的一些值。

我有一个单独的函数来处理CDax.csv。看起来像这样:

enter image description here

public HashMap<String,String> readCDax() throws Exception {
    String csvDaxFile = "C:\\Users\\CDAX.csv";

    CSVReader reader = new CSVReader(new FileReader(csvDaxFile), ';');
    String [] line;
    HashMap<String, String> cdaxMap = new HashMap<String, String>();

    while ((line = reader.readNext()) != null) {            
        cdaxMap.put(line[0], line[7]);
    }

    System.out.println("Process CDax File!");

    reader.close();
    return cdaxMap;
}

我的主要方法是run(),我在我的主方法中执行:

public void run()抛出异常{

while ((firstLine = reader.readNext()) != null && (secondLine = reader.readNext()) != null && i<10) {            

    //fileName of the String
    fileName = firstLine[0];

    writerPath = "C:\\Users\\" + fileName + ".csv";
    //write csv file
    CSVWriter writer = new CSVWriter(new FileWriter(writerPath), ';');

    //write Header
    //String[] entries = "Name;Date;TotalReturn;Currency".split(";");
    String [] entries = {"Name","Date", "TotalReturn", "Currency"};

    writer.writeNext(entries);

    //create Content

    //companyName of the String
    companyName = secondLine[1];

    //currency
    currency = secondLine[2];

    //dates
    dateList = new ArrayList<String>();
    for(int p = 3; p < firstLine.length; p++) {
        dateList.add(firstLine[p]);
    }

    //total returns
    returnList = new ArrayList<String>();
    for(int j = 3; j < secondLine.length; j++) {
        returnList.add(secondLine[j]);
    }

    // cDaxList
    cDaxList = new ArrayList<String>();
    for(int j = 1; j <dateList.size(); j++) {
        if(cDaxMethodValuesMap.containsKey(dateList.get(j))){
            cDaxList.add(cDaxMethodValuesMap.get(dateList.get(j)));
        } else{
            dateList.add("na"); // I get the error here!
        }
    }

    if(dateList.size()!=returnList.size()) {
        System.out.println("Dates and Returns do not have the same length!");
    }

    int minSize = Math.min(dateList.size(), returnList.size());

    //"Name;Date;TotalReturn;Currency"
    List<String[]> data = new ArrayList<String[]>();
    for(int m = 0; m < minSize; m++) {
        data.add(new String[] {companyName, dateList.get(m), returnList.get(m), currency, cDaxList.get(m)});
    }

    writer.writeAll(data);

    //close Writer
    writer.close();

    i++;
    System.out.println(fileName + " parsed successfully!");


}
System.out.println("Done");

}

然而,当我运行我的程序时,我得到:

Process CDax File!
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 com.TransformCSV.main.ParseCSV.run(ParseCSV.java:109)
    at com.TransformCSV.main.ParseCSV.main(ParseCSV.java:21)

我在这个方法中遇到错误:

cDaxList = new ArrayList<String>();
for(int j = 1; j <dateList.size(); j++) {
    if(cDaxMethodValuesMap.containsKey(dateList.get(j))){
        cDaxList.add(cDaxMethodValuesMap.get(dateList.get(j)));
    } else{
        dateList.add("na"); //I get the error here!!!
    }
}

我尝试通过vm设置放置堆,但我认为不应该这样做因为我只读取了两个csv文件中的3000值。

感谢您的回复!

1 个答案:

答案 0 :(得分:3)

你的循环:

for(int j = 1; j <dateList.size(); j++) {

循环遍历dateList,但在该循环中,您要添加到dateList

dateList.add("na"); //I get the error here!!!

所以dateList会变得越来越大,直到你的内存不足为止。每次通过循环评估dateList.size(),而不是在开始时评估。