Strings generated at runtime killing memory and crash app

In my app users can download, watch and delete 3d-objdata (*.obj) in runtime.
To generate the meshes on the fly, I heavily deal with strings.
My problem is, that these strings never get released by GarbageCollector and soon or later the app crashes (“ApplicationDidReceiveMemoryWarning”).

For further testing I wrote a very basic code that simply reads text from file into a string.
Setting this string to null or “” and reading from file again doubles up used memory.
This is not covered by the Unity profiler, you only see it in XCode.

Background info:
Optimizing-garbage-collection-unity-games
Unity-webgl-memory-the-unity-heap

My question now is, how can I prevent my app from running out of memory?

It’s not generally considered good practice, but if you’re reading huge amounts of data into strings and not dereferencing in between, you may not be getting a GC cycle triggered (or not cleaning them up). This gets compounded if you do thinks like concatenating strings, etc, because those create new copies of the string in memory.

One thing you should probably try is using a StringBuilder to work with your strings if possible to ease that pain if you’re concatenating strings. If you’re just working with a large string that’s getting crated and recreated, it may be necessary to force a garbage collection between by calling GC.Collect();

Thank you, I already tried StruingBuilder and System.GC.Collect()…no luck.

My newest assumption is that GarbaceCollector ( System.GC.Collect() ) does not work on mobile for strings.

Working with the OP via DM but for posterity this might be the issue:

1 Like

Yes, that´s it. Meanwhile I was able to code a workaround in my project.