gpt4 book ai didi

java - 使用 opencsv - java.lang.OutOfMemoryError : Java heap space

转载 作者:行者123 更新时间:2023-11-29 05:31:42 26 4
gpt4 key购买 nike

我正在使用 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 值。

感谢您的回复!

最佳答案

你的循环:

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

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

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

所以 dateList 会越来越大,直到内存不足。 dateList.size() 每次都在循环中计算,而不是在开始时计算一次。

关于java - 使用 opencsv - java.lang.OutOfMemoryError : Java heap space,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20856823/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com