- Home /
Maximum RAM memory exceeded causes app to crash in iOS
Hello, I'm developing an app in which you have to download 40-60 photos, and that causes the app to die.
The "WARNING -> applicationdidreceivememorywarning()" message appears in the XCode logs, and then the app dies.
To give some context on what the images are used for: I have those images Firebase Storage service, downloaded with UnityWebRequestTexture, and then assigning that Texture2D to a RawImage's Texture. The images are photos, not "game textures".
I have read about Texture compression, NPOT/POT things, etc. But can I do anything about Textures that are downloaded from the Internet? Or only with the ones that already exist in the project?
MUCH thanks!
Do you really need to display them all at once? You could cache them all and only load and display those that are currently needed.
Answer by tessellation · Oct 04, 2018 at 09:40 PM
Do what Casiell says, but you can also download the images (JPG is the smallest) from the server as a byte array (raw JPG data) and then create a Texture2D from the stored JPG data only when the image is on-screen. Use ImageConversion.LoadImage() to make the Texture2D when you need to display it. Don't forget to null the RawImage texture property when the RawImage goes off-screen and also call Destroy on the Texture2D.
First of all, thank you for your time, @tessellation and @Casiell. The total of images goes between 30-90, depending on how many that group of user have taken. Good news is I don't need to show them all at once, maximum shown at the same time are 10. I'm kinda new to program$$anonymous$$g, so forgive me but how do I exactly "cache" the Textures? Cacheing means saving it to device storage? Or having a Texture2D [] with all the Textures, and assigning/nulling the RawImage's Texture field?
On regards of what you say, @tessellation, the way I currently create the Textures is this:
The images are in .jpg in the cloud, but I recieve a byte array, that gets converted to a Texture this way and then assigned to the RawImage:
Texture2D tex = new Texture2D (1, 1); tex.LoadImage (bytes); rawImage.texture = tex;
I guess doing tex.LoadImage () should be the same as doing ImageConversion.LoadImage ().
I will null the RawImage's texture and Destroy the created Texture2D as you say.
The only thing I am concerned about is how do I cache the Textures, as I said before, since I don't really know what that exactly means.
Also I've read about UnloadAsset/UnloadUnusedAssets, but I'm not sure if this would help or if it would throw away Textures I still need.
$$anonymous$$UCH thanks to you both <3
Correct, tex.LoadImage(bytes) is just the old way of doing it. Unity changed the API recently. "cache" just means to store something temporarily, in this case I mean for you to keep all 90 raw byte arrays in memory for each texture after downloading from the server. They will take far less space in memory than the full uncompressed Texture2D, which you really only need 10. Also, depending on the size of how they are displayed on the screen, you could down-sample the texture after calling tex.LoadImage() to a smaller size to take even less memory. You can use a high-quality scaler, like this one: http://wiki.unity3d.com/index.php/TextureScale