Loading multiple textures from disk results in memory leak

Hey guys,

I hope that I’m posting in the correct category.

I’m working on a scanner app for iOS that creates textured scans of the real environment. In the App, im applying captured images to simultaneously captured geometry data at runtime. However, when I’ve loaded my 70th image / texture2D from the HDD and assigned it to the 70th material, I’m out of memory and the application crashes.

Is there any way that I can assign 70+ materials and texture2D components at runtime without running out of memory? I read that I can destroy these components but when I do that I also delete the texture / material reference for a particular game object and the it begins to look incorrect.

Any hint or idea is greatly appreciated.

There is only a certain amount of RAM on an iOS device. Assuming modest 1024x1024 RGBA textures that works out to 4mb per texture, so at 70 of those you’re using nearly 300mb of RAM just for the textures.

Older iPads started to get wiggly when your total program memory use reached 220mb or so, and if you asked for 300mb you were thrown out of the nightclub.

Modern devices have more usable RAM, but I’m not sure where they stand now.

Obviously 2048x2048 would be approximately 1.2gb of RAM and there aren’t a lot of phones that can do that.

If you want to see actual usage, attach the profiler (Window → Analysis → Profiler) and see what’s going on.

You can also just run it from Xcode directly and go to the memory tab at runtime in Xcode and see where you stand.

1 Like

Apple devices are always RAM limited. Even the current iPhone 12 has just 4GB total RAM, and that is a lot for an Apple device since most are far lower than 4GB. Compare that to a Samsung Galaxy S20 which comes with 12GB. Mobile devices don’t support virtual memory (swap files), so all applications and services running all fit into that same RAM.

1 Like

Thanks for the replies.

I understand that Apple devices are RAM limited.
Fortunately, I’m using the newest iPad pro which has around ~3.3GB of useable RAM.

I know my question is really broad, but imagine that what I’m trying to create is an application like this:

As mentioned, things work fine until all RAM is used. My current implementation approach is to:

  1. Save 2 captured images a second to disk (not RAM).
  2. Do geometry calculations which use practically no RAM at all.
  3. Create a GameObject for each captured image. Each GameObject has a MeshRenderer component and a Texture2D component as main texture.
  4. Export a single parent GameObject which contains all the GameObjects from step 3 as a single 3D model (GLB file).

I can see that this approach is very RAM heavy. So, I guess my question i whether you could think of any other way to export multiple GameObjects with multiple textures that will not result in a RAM overflow at some point? I know that apps like “3D Scanner App” and “Polycam” are able to create 3D models from 200+ images. Likely, they are not coded using Unity, but wouldn’t it be possible to do something similar in Unity without reaching a RAM limit?