1

(Beginner Android Question: ) I'm loading a 25MB thesaurus text file into a HashMap but I get an Out Of Memory Fatal Exception. I have researched this site to find ideas on other ways to do this but am still unsure. Would it be sensible to split the text file into a number of smaller ones (say 26, one for each letter) , and then read these from text file to HashMap as needed by the program ? Are there better ways to do this ? The app is meant to show all the synonyms for words inputted by the user.

12-23 13:04:44.336: E/AndroidRuntime(9865): FATAL EXCEPTION: main
12-23 13:04:44.336: E/AndroidRuntime(9865): Process: com.example.dictfromtextfile, PID: 9865
12-23 13:04:44.336: E/AndroidRuntime(9865): java.lang.OutOfMemoryError: Failed to allocate a 6532 byte allocation with 4262 free bytes and 4KB until OOM
12-23 13:04:44.336: E/AndroidRuntime(9865):     at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95
GordonBooker
  • 45
  • 1
  • 7

1 Answers1

0

Loading the entire file into memory at once is highly not recommended and will throw you an OutOfMemoryException error depending on the device you're using. You can use a SQLite database to store your data and perform database actions on it to search, insert or remove data.

In case you wish to store your data in a text file, you can use a BufferedReader to read the file line by line using the readLine() method (loading each line into the memory only when it's needed). You can then wrap it with a class of your own to enable all the features you need, like searching.

mittelmania
  • 3,393
  • 4
  • 23
  • 48