I figured it out on my own and put the answer as an edit.
Allocating 2 GB of memory for strings on scene load?
So I've been working on a 2D project in Unity 2018.2.10f1 that I just put onto my phone (Huawei P20 Lite). The game freezes when it tries to load a specific scene due to a system out of memory exception. I realise that this means the system runs out of memory, but the entire project is 94 mb with a lot of unused assets and the exception I am getting is:
AndroidPlayer(ADB@127.0.0.1:34999) Could not allocate memory: System out of memory! Trying to allocate: 1984261488B with 16 alignment. MemoryLabel: String Allocation happened at: Line:303 in ./Runtime/Core/Containers/StringStorageDefault.h Memory overview
[ ALLOC_DEFAULT ] used: 15601032B | peak: 19766386B | reserved: 16062003B
[ ALLOC_TEMP_JOB ] used: 0B | peak: 0B | reserved: 524288B
[ ALLOC_TEMP_BACKGROUND_JOB ] used: 0B | peak: 0B | reserved: 1048576B
[ ALLOC_GAMEOBJECT ] used: 3183236B | peak: 3183236B | reserved: 3185153B
[ ALLOC_GFX ] used: 1591256B | peak: 5406974B | reserved: 1595171B
[ ALLOC_PROFILER ] used: 721774B | peak: 731658B | reserved: 735058B
[ ALLOC_TEMP_THREAD ] used: 42188B | peak: 0B | reserved: 3506176B
It is attempting to allocate 1984261488 bytes = ~2 GB of memory just in strings! I have checked through my code and I am not allocating any strings or arrays that are large, yet it is still trying to allocate 2 GB of memory. On my computer when the game is running in the Unity editor the entire editor is only using ~950 MB.
Could this be a bug in my code or the Unity code? Really any help to get to the bottom of this would be greatly appreciated. I realise this is fairly vague but I'm really not sure what information I should include since no code runs, just loading the scene causes the out of memory exception. Thank you in advance :)
Edit: I have tried uninstalling it off my phone and clearing the cache then reinstalling the app but the problem still persists.
Edit: I tried a windows build and it also broke due to an out of memory exception! Doing more investigating now.
Final Edit/ Fix: I had a scriptable object that had a field in a #if UNITY_EDITOR #endif. I figured since it was just a description of that object for designers I wouldn't need it in a build, but it breaks Unity's serialisation! Removing the #if UNITY_EDITOR fixed the issue :)
Follow this Question
Related Questions
Unity Input Touch 0 Answers
How to use External camera on andorid 0 Answers
Bad android quality ,Bad Android Quality 0 Answers
No camera / images on Android build 1 Answer
[Android] Unity profiler not showing actual memory usage on device? 0 Answers