2

I will start by saying I am very new to android and also fairly new to java so I apologize for missing anything that seems blatantly obvious to others.

My code which is shown below is supposed to read a line from a file in the assets folder as long as the next line isn't empty, assign the contents of this line to a variable (The 'stringBuffer' String), and then add this variable to an ArrayList.

String stringBuffer = "";
BufferedReader reader = null;
ArrayList<String> fileLines = new ArrayList<>();

input = mContext.getAssets().open("Nodes.txt");
reader = new BufferedReader(new InputStreamReader(input));

while((stringBuffer = reader.readLine()) != null) {
    fileLines.add(stringBuffer);
}

reader.close();`

I'm running the application on my phone, and when this process should take place the application just goes black, with LogCat showing "GC_FOR_ALLOC freed" followed by differing numbers.

I tried commenting out the fileLines.add(stringBuffer); line and the program worked again, which is why I'm assuming that whatever is going wrong is due to this line.

I would like to know what this GC_FOR_ALLOC means and also why it is happening. If any other information is needed please feel free to ask.

EDIT: This is in a non activity class is that matters.

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
sparky
  • 23
  • 4

3 Answers3

1

Try this instead of what you have in your code:

ArrayList <String> fileLines = new ArrayList<String>();

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
Sid
  • 30
  • 1
  • 6
0

GC_FOR_ALLOC means that garbage collection was triggered because there wasn't enough space on the heap to create new objects. This can be triggered while creating new objects. Looks like you are creating a lot of local objects (?). Try to cut down on those, maybe.

Source: What do GC_FOR_MALLOC, GC_EXPLICIT, and other GC_* mean in Android Logcat?

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221
  • It should just be adding 7 different strings to the array list, with each consisting of one word. Surely there should be enough space for this? – sparky Mar 10 '15 at 18:50
  • @sparky What about other parts of the program? And what is in the "error" part of the LogCat when the screen goes black? – An SO User Mar 10 '15 at 18:51
  • They all work fine, I spent a lot of time commenting different sections of code to actually find what was causing the error. Any there is no error as such, just those GC_FOR_ALLOC don't stop, they just keep repeating – sparky Mar 10 '15 at 18:59
0

You can try this- Providing buffer size can solve the issue perhaps,working fine for me.

            AssetManager am = getAssets();
            InputStream is = am.open("tables.txt");
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
Sanjeet A
  • 5,171
  • 3
  • 23
  • 40