0

I am having a OutOfMemory error and so I am trying to decide is there is another way I can do things to avoid this. I have asked about that question with no real responses or fixes, you can see my OurOfMemory question here: Android OutOfMemory Error StringBuilder

But would using an ArrayList<String> instead of StringBuilder save memory, get rid of this error or make it process it so it doesn't use as much memory?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
user2101081
  • 445
  • 5
  • 22
  • refer : https://stackoverflow.com/questions/19537550/android-out-of-memory-error-stringbuilder – Ali Feb 16 '18 at 18:02
  • Did you try using the ArrayList? Seems like a quick and easy way to answer your own question. – EJK Feb 16 '18 at 18:03
  • @EJK I have used the ArrayList, but I can't reproduce the error that crashlytics is saying my app is having to begin with. So that is why I am asking if using an ArrayList uses less memory than a StringBuilder – user2101081 Feb 16 '18 at 18:04
  • 1
    We originally discussed this [here](https://stackoverflow.com/q/48769679/115145). Having lots of smaller strings is better, from a memory management standpoint, than is having one massive string. However, since we have no idea what you are doing with this string, we cannot tell you whether an `ArrayList` would somehow be better. As I indicated in my comments on the earlier question, if your objective is to put this stuff in a file, then write directly to the file, rather than building it all up in memory and *then* writing it to a file. – CommonsWare Feb 16 '18 at 18:06
  • @CommonsWare your answer didn't answer my original question, you gave me some technical response when I am new to programming that I don't understand without a single code example, and I am not doing anything with the information as previously stated EXCEPT using it to read what is on screen. I don't need to keep the information at all after it is processed. So that is why I am NOW asking if ArrayList uses less memory than a StringBuilder. – user2101081 Feb 16 '18 at 18:08
  • 1
    "I am new to programming" -- you claim to have distributed an app and are getting crash logs via Crashlytics. "I am not doing anything with the information" -- then why are you generating the `String`? Surely there is something that you are doing with this `String` that is of value to the user. What is it? Are you writing to a file? Are you sending it over a network connection? Are you displaying it in a `TextView` or `WebView`? You might provide a [mcve] showing how you are consuming this string. Then, and only then, can anyone say whether `ArrayList` will help. – CommonsWare Feb 16 '18 at 18:13

1 Answers1

0

Refering to the old question, the proccess to enlargeBuffer will use system call arrayCopy, that will first copy the array to other position before disposing the first one. This will consume more memory than it looks. ArrayLists does have a array as back data, so it will enlarge (unless you start with a great size that affords all swords), copy, then dispose also.

Maybe your ideal structure is a LinkedList.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167