Search code examples
iosunity-game-enginebackground-process

Unity for iOS takes a lot of memory in background


I'm writing Unity iOS application, which tracks user location in background. When app is working in foreground, it takes 50-60MB of memory, and when it goes background, all data still remain in memory. In this situation application terminates due to memory issue in few seconds. Resources.UnloadUnusedAssets(); doesn't reduce anything.

How can I reduce occupied memory in background?


Solution

  • That's the desired behaviour. When an app goes in the BG iOS doesn't start to free up memory, that's the app's responsibility.

    The best way to approach memory issues is to do profiling on the app and see where the memory goes. In most of the cases textures take up a large amount of memory so you can think about reducing their size or use compression. Another thing you can do is to turn off mipmapping for textures, but this will only give good visual results if you are using textures for 2D UI, for 3D is not a good idea.

    Resources.UnloadUnusedAssets() will only work if there are unused assets, if you don't do anything about that there's a slim change that it will have any effect.

    One little hack that you can try is to create an empty scene and transition to that one when going in background. The scene could have one 2D texture that shows the app is in background mode. Then you can call Resources.UnloadUnusedAssets() and of course transition back to the main scene when coming back in foreground.