getting out of memory error
I am trying to load a 12mb txt file into memory as a HashMap to make it available for the application to use it but I am getting an 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)
Is there any way to solve it or it is a limitation of dalvik vm?
Code for loading the file into memory:
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
}
开发者_Python百科
return bigramFrequencies;
}
Reading a 12mb file in memory will slow down the device. 12mb might sound less for a normal pc, but most Android phones come with less then 512MB of RAM.
You should be able to reformat the file or split it up so the application can read the necessary parts when it needs them.
Also, using a SQLite Database might be better suited an speed up the whole thing.
A HashMap
in the application had an item added to it, and in doing so hit its limit for the current array backing. So, it attempted to resize and in doing so it ran out of memory.
HashMaps use hashes to jump to a particular element in an array, then it looks for the item in the backing store. The entire algorithm depends heavily on the array being sparse enough to avoid excessive collision of the hash space (the hash divided modulus by the array size). If you add a lot to a hash oriented structure, it needs to allocate a bigger array to maintain enough empty array elements for the performance to not degrade horribly.
After the allocation of the new array, each element from the old array is relocated into the new array. This effectively adds "empty slots" for other items to utilize.
If device has adequate Memory then
Try increasing the heap size available to app.
Edit the build.prop file and increase the heap size
dalvik.vm.heapsize=32m
精彩评论