I have a fairly massive game that uses AssetBundles. Recently hit an issue where the debug mono builds are crashing during gameplay when I load some of my larger assets. They were unchanged recently, so I must’ve pushed memory over somewhere else and it was the tipping point. But it lead me down a rabbit hole observing Unity behavior in my project.
For reference, I’m on Unity 2019.4.3f1
My initial scene when booting up the game that leads to a UI menu shows this in the profiler. Now, this profile is the first frame that shows up in the Unity Profiler after waiting to attach the debugger on Android:
Used Total: 377.1 MB Unity: 134.0 MB Mono: 58.3 MB GfxDriver: 145.9 MB Audio: 3.5 MB Video: 0 B Profiler: 35.3 MB
Reserved Total: 429.3 MB Unity: 171.0 MB Mono: 73.4 MB GfxDriver: 145.9 MB Audio: 3.5 MB Video: 0 B Profiler: 35.5 MB
Total System Memory Usage: 3.46 GB
Textures: 251 / 133.8 MB
Meshes: 57 / 20.4 MB
Materials: 163 / 477.0 KB
AnimationClips: 48 / 0.8 MB
AudioClips: 8 / 2.7 MB
Assets: 52014
GameObjects in Scene: 134
Total Objects in Scene: 570
Total Object Count: 52584
GC Allocations per Frame: 2040 / 197.0 KB
Here is another profile but this one is Standalone windows, again, first frame that shows up:
Used Total: 423.0 MB Unity: 181.7 MB Mono: 76.0 MB GfxDriver: 154.9 MB Audio: 3.9 MB Video: 0 B Profiler: 6.4 MB
Reserved Total: 507.1 MB Unity: 258.0 MB Mono: 82.3 MB GfxDriver: 154.9 MB Audio: 3.9 MB Video: 0 B Profiler: 8.0 MB
Total System Memory Usage: 0.76 GB
Textures: 246 / 142.6 MB
Meshes: 65 / 20.5 MB
Materials: 153 / 0.6 MB
AnimationClips: 56 / 0.8 MB
AudioClips: 8 / 2.7 MB
Assets: 52819
GameObjects in Scene: 146
Total Objects in Scene: 648
Total Object Count: 53467
GC Allocations per Frame: 11 / 0 B
The Reserved Total is concerning me the most. It seems quite high especially on the GfxDriver
no?
So what is it showing when I crash? Well, the profiler is showing much much worse:
Used Total: 1.14 GB Unity: 0.60 GB Mono: 182.4 MB GfxDriver: 347.1 MB Audio: 4.0 MB Video: 0 B Profiler: 18.8 MB
Reserved Total: 2.00 GB Unity: 1.44 GB Mono: 204.8 MB GfxDriver: 347.1 MB Audio: 4.0 MB Video: 0 B Profiler: 21.0 MB
Total System Memory Usage: 3.46 GB
Textures: 848 / 367.8 MB
Meshes: 71 / 25.3 MB
Materials: 301 / 503.0 KB
AnimationClips: 50 / 4.1 MB
AudioClips: 8 / 2.8 MB
Assets: 58073
GameObjects in Scene: 125
Total Objects in Scene: 867
Total Object Count: 58940
GC Allocations per Frame: 491327 / 33.5 MB
Am I crazy? Why are the Unity numbers so pumped at launch and super high during my gameplay? I presume the “unity” number is encompassing the textures and other numbers. I’ve tried to take memory snapshots using the Memory Profiler and the tree really shows me that during gameplay right before crashing:
Used Total: 3.43 GB Unity: 1.50 GB Mono: 321.3 MB GfxDriver: 1.03 GB Audio: 4.0 MB Video: 0 B Profiler: 0.58 GB
Reserved Total: 3.83 GB Unity: 1.78 GB Mono: 424.4 MB GfxDriver: 1.03 GB Audio: 4.0 MB Video: 0 B Profiler: 0.60 GB
Total System Memory Usage: 5.77 GB
Memory Profiler Tree view:
Textures: 1GB
Animation: 260MB
Mesh: 190MB
RenderTexture: 100MB
Font: 60MB
Shader: 60MB
MonoScript: 66MB
Particle System: 38MB
Everything else is magnitudes smaller. The above is 1.7 to 1.8GB at most.
Let’s say my game is targeting 4GB RAM devices like a Pixel 2.
- I’ve observed the OS can sometimes take up to 1GB of memory! Let’s assume that’s worst case. A fresh reboot of a Pixel 2 knocks it down to like 200-300MB and this sometimes has me crashing less.
- Debug Mono & Unity are taking up to 500MB on boot, but as I progress into game that reserved number runs up to about 1GB+. At crash, I saw Reserved Total: 2.00GB!!
- That leaves my game only 1-2GB~ RAM for textures and other stuff which apparently is insufficient
What should I be doing? Obviously, I need to knock down texture memory in general to support a healthy build. But, even if I eliminate textures entirely I’m going to keep hitting crashes if I can’t figure out how to wrangle the Unity player memory in.