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.

12
  • What is the size/content of Nodes.txt? In which thread does this code running? Commented Mar 10, 2015 at 18:35
  • @kupsef The file just has 7 lines with one word on each line, and sorry but what do you mean by thread? Commented Mar 10, 2015 at 18:41
  • What happens when you put System.out.println(stringBuffer); instead of fileLines.add(stringBuffer); . What is printed to LogCat ? Commented Mar 10, 2015 at 18:51
  • Your app probably crashes with an Exception that you should be able to see in LogCat. Try to find it and update your answer with it. Commented Mar 10, 2015 at 18:55
  • @JonasCz LogCat displays the contents of the file Commented Mar 10, 2015 at 18:57

3 Answers 3

1

Try this instead of what you have in your code:

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

Sign up to request clarification or add additional context in comments.

Comments

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?

3 Comments

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 What about other parts of the program? And what is in the "error" part of the LogCat when the screen goes black?
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
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);

1 Comment

Tried this but no change

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.