我想一个12MB的txt文件加载到内存中的HashMap,以使其可用于应用程序使用它,但我得到一个OutOfMemoryError越来越内存不足的错误
09-27 15:42:17.560: ERROR/AndroidRuntime(19030): ... 11 more
09-27 15:42:17.560: ERROR/AndroidRuntime(19030): Caused by: java.lang.OutOfMemoryError
09-27 15:42:17.560: ERROR/AndroidRuntime(19030): at java.util.HashMap.makeTable(HashMap.java:559)
09-27 15:42:17.560: ERROR/AndroidRuntime(19030): at java.util.HashMap.doubleCapacity(HashMap.java:579)
09-27 15:42:17.560: ERROR/AndroidRuntime(19030): at java.util.HashMap.put(HashMap.java:409)
09-27 15:42:17.560: ERROR/AndroidRuntime(19030): at org.com.SentencesActivity.loadBigramFrequencies(SentencesActivity.java:151)
09-27 15:42:17.560: ERROR/AndroidRuntime(19030): at org.com.SentencesActivity.onClick(SentencesActivity.java:56)
有什么办法解决它还是它是dalvik vm的限制?
代码文件加载到内存中:
public HashMap<String, Double> loadBigramFrequencies() throws IOException {
AssetManager assetManager = getAssets();
HashMap<String, Double> bigramFrequencies = new HashMap<String, Double>();
String[] splittedLine;
try {
// open the file for reading
InputStream instream = assetManager.open("bigramFrequencies.txt");
// if file the available for reading
if (instream != null) {
// prepare the file for reading
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
// read every line of the file into the line-variable, on line
// at the time
while ((line = buffreader.readLine()) != null) {
// do something with the settings from the file
splittedLine = line.split("\\#");
bigramFrequencies.put(splittedLine[0].trim(),
Double.parseDouble(splittedLine[1].trim()));
}
Log.v(LOG_TAG, "bigram frequencies loaded");
}
// close the file again
instream.close();
} catch (java.io.FileNotFoundException e) {
// do something if the myfilename.txt does not exits
}
return bigramFrequencies;
}
请你任一张贴本实施例的完整源代码,或优选更小的,更简单的再生这个问题?要么发布到问题上,要么发布到pastebin上。谢谢! –
将txt文件加载到内存中的代码是什么?首先,12MB是巨大的开始,然后你试图制作一个数据结构。看看是否没有办法缩小文本文件或以增量或其他方式加载它:) – Bob
12mb在服务器上非常庞大,更不用说在移动设备上。我认为这是一个用于句子分析的digram词典?你不能在服务架构之后在线托管它吗? – mcfinnigan