Android - OutOfMemory when reading a text file

I am making a dictionary application on Android. During startup, the application will download the contents of the .index file (~ 2 MB, 100,000 lines)

However, when I use BufferedReader.readLine () and do something with the returned string, the application calls OutOfMemory.

// Read file snippet Set<String> indexes = new HashSet<String)(); FileInputStream is = new FileInputStream(indexPath); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String readLine; while ( (readLine = reader.readLine()) != null) { indexes.add(extractHeadWord(readLine)); } // And the extractHeadWord method private String extractHeadWord(String string) { String[] splitted = string.split("\\t"); return splitted[0]; } 

When reading the log, I found that when executed, it forces the GC to explicitly clean objects repeatedly (GC_EXPLICIT releases xxx objects in which xxx is a large number, such as 15000, 20000).

And I tried another way:

 final int BUFFER = 50; char[] readChar = new char[BUFFER]; //.. construct BufferedReader while (reader.read(readChar) != -1) { indexes.add(new String(readChar)); readChar = new char[BUFFER]; } 

.. and it works very fast. But that was not quite what I wanted.

Is there any solution that works fast as a second snippet and is easy to use as a first?

Regard.

+4
source share
2 answers

extractHeadWord uses the String.split method. This method does not create new lines, but relies on the main line (in your case, the line object) and uses indexes to indicate the "new" line.

Since you are not immersed in the rest of the line, you need to drop it so that it collects garbage, otherwise the whole line will be in memory (but you use only its part).

A call to the String(String) constructor String(String) ("copy constructor") discards the rest of the string:

 private String extractHeadWord(String string) { String[] splitted = string.split("\\t"); return new String(splitted[0]); } 
+4
source

What happens if your extractHeadWord does this return new String(splitted[0]); .

It will not reduce temporary objects, but can reduce the size of the application. I don’t know if split does roughly the same thing as a substring, but I think it does. substring creates a new view on top of the original data, which means that the full array of characters will be stored in memory. An explicit new String(string) prompt will truncate the data.

+3
source

All Articles