[英]outofmemory error when adding hashmap and arraylist
csv文件中有大量數據。 它有250萬行,每行有10個字段,我們正在嘗試為每行准備哈希圖,然后將該哈希圖添加到arraylist中。
我無法執行此操作,因為海量數據導致Java Heap空間錯誤。
但是我的應用程序需要哈希表列表(我不想增加堆空間)。
reader = new CSVReader(new FileReader(dataFile),',');
Map<String, String> feedMap = null;
String[] firstLine;
String[] nextLine;
String mappingKey = null;
String mappingValue = null;
//Read one line at a time
firstLine = reader.readNext();
while ((nextLine = reader.readNext()) != null){
int i = 0;
feedMap = new HashMap<String, String>();
for(String token : nextLine){
mappingKey = xmlNodeMap.get(firstLine[i]);
if (mappingKey != null) {
mappingValue = token.trim().length() > 0 ? token : Constants.NO_VALUE;
feedMap.put(mappingKey, mappingValue);
}
i++;
}
listOfMaps.add(feedMap);
}
這聽起來像是glib,但是您的問題是您的應用程序需要250萬行的列表作為HashMaps。
這是荒謬,不合理和坦率的荒謬的要求; 我無法想象使用這種數據結構有什么好處。
將應用程序更改為不需要它。
您可以嘗試使用byte []代替String對象: byte[] key = mappingKey.getBytes("UTF-8")
每個String對象都包含一組UTF-16字符。 在大多數情況下,這意味着每個符號2個字節。 UTF-8編碼使用一個字節表示ASCII,使用兩個字節表示許多歐洲語言。
另外,每個String對象都包含對char數組的引用。 這意味着您在內存堆中有兩個對象:String和char數組。 每個對象(甚至只是new Object()
)的開銷約為24個字節(取決於Java VM版本和選項)。
因此,您可以輕松地將對象數減少兩倍(一個字節[],而不是成對的String + char []),並且UTF-8符號的數組長度通常小於UTF-16字符的長度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.