Digital Dracula Digital Dracula - 5 months ago 18
Android Question

In android, how do I deal with memory crashes (possibly) caused by large string arrays?

I'm taking my first timid steps in java and android development. I've successfully created a java program that randomly combines words from several string arrays. It runs flawlessly, without crashes or other issues.

Porting the code to android studio (with the necessary alterations), I have also succeeded in creating a very simple app that, when installed, also runs in the intended way (the user gets a new combined phrase every time a button is tapped). Only, for no apparent reason, the app crashes perhaps once every 4-5 time the button is tapped. This isn't set in stone - in sometimes it crashes on the first attempt, in others it works ten times in the row.

Based on the above, I suspect the crash might be a result of some memory problem - in particular, I have very large string arrays (containing thousands of words) stored inside the main activity.

My questions:

a) Is this a plausible reason, could large chunks of such data in the main activity possibly cause the crash, and would it perhaps help to move them elsewhere?

b) What would be the "proper"(easy?) way to do it (i.e. move them)?

c) bonus question: is there any easy way to obtain a crash log (e.g. by adding a piece of code in the main activity that would automatically save a .txt file when crashing)?

I'm not including any code, because I don't think it's directly relevant (not to mention it's pretty long), but if you need any particular pieces, let me know. Thanks

Answer Source

a) Is this a plausible reason, could large chunks of such data in the main activity possibly cause the crash, and would it perhaps help to move them elsewhere?

Yes, there is a Throwable called OutOfMemoryError that is thrown when there is not enough memory. Your large string array could be causing this. It might help if you move it somewhere else (other than memory).

However, are you sure there is not enough memory? You didn't seem to mention anything about what you saw in logcat, so I suspect that you are purely guessing it's because there is not enough memory. It is equally possible that the crash is caused by a NullPointerException or any other exception. Look at your logcat first, check if it is really an OutOfMemoryError.

Because of this, I will assume that the crash is caused by an OutOfMemoryError.

b) What would be the "proper"(easy?) way to do it (i.e. move them)?

There are a lot of ways. The easiest in my opinion, is to store them in an SQLite database on the hard disk instead of memory. Each time you need to get stuff from the strings, don't get every string into memory, get only a few of them that you need. Another way is to store it on Firebase database, and request it to give you a part of the strings when you need it.

c) bonus question: is there any easy way to obtain a crash log (e.g. by adding a piece of code in the main activity that would automatically save a .txt file when crashing)?

To obtain the crash log, just look at the logcat, as I said before.