[WebGL][iOS] Unity sometimes reloads after startup

When this WebGL is executed in Safari on the iPhone, sometimes a reload process occurs suddenly right after Unity starts.
In most cases, this occurs immediately upon startup, but sometimes it occurs after a while.

Safari iPhone11(MHDD3J/A) iOS 17.6.1

Notes:
・This issue only occurs on iPhone, not on other devices.
・Not tested on iPad.
・Reloading may occur a few seconds after Unity starts.

Unity version 2022.3.40f1, version 6000.0.23f1

  • I am not good at English.

You need to analyze the logs, see manual for details how to access them.

iOS watchdog is very strict. If your app tries to load too much content up front the app is considered inactive and gets killed. This can happen within mere seconds. It‘s best to load a nearly empty scene before starting to load any other content, ideally as addressables.

You may also exceed the memory limit, which can be as low as 500 MiB depending on device and usage (try rebooting). Memory pressure can also quickly add up if too much is done/loaded in a single frame since GC in web only runs at the end of a frame even if you manually call collect.

Long story short: this is common, and is likely only fixable by analyzing and heavily optimizing.

1 Like

Thanks for the info.
I want to check Safari’s memory usage details, but Web Inspector on my Mac closes when Safari reloads.
Using “Instruments”, I can get a rough com.apple.WebKit.WebContent memory usage information, but not in detail.
Do you know the best tool to investigate WebGL memory usage?

Sorry, haven’t worked on iOS for many many years. Check if there’s a way to connect the Unity (Memory) Profiler, I think it works generally for Web builds but maybe only on desktop - not sure.

On Desktop I would refer to Firefox dev tools. On iOS even Firefox uses WebKit and maybe it shows the same behaviour and perhaps it has better dev tools.

Alternatively, stripping down the build (at least first scene) to see if it will eventually boot up may allow you to see memory usage in Instruments. Then enable/disable various parts and see how much they contribute overall.

Thank you for your advice.
I will look into them and give them a try.

1 Like

any luck? our game also exceeds the 500MB threshold apparently, but not sure, the initial scene is under 300MB and even that causes a reload some times

Did you check a log for any suspicious information?
I would try to profile WebGL build on Mac with Memory Profiler to see is there are any easy objects to tune down to fit the 500MB threshold. (Use Build&Run with Autoconnect Profiler option enabled in build window).

You can also add the following code to enable high level WebGL memory profiler overlay

using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;

public class MemeoryPostBuild : IPreprocessBuildWithReport
{
    public int callbackOrder { get { return 0; } }
    public void OnPreprocessBuild(BuildReport report)
    {
        Debug.Log("MemeoryPostBuild.OnPreprocessBuild for target " + report.summary.platform + " at path " + report.summary.outputPath);
        PlayerSettings.WebGL.emscriptenArgs = "--memoryprofiler --profiling-funcs";
    }
}
2 Likes

thanks for your response.
I don’t own a Mac but I use this live.browserstack.com website to test the builds on real iOS devices. Unfortunately, no memory profiles there. But I created a test build and I’m finding interesting results, like crashing once allocating more than 40MB at a time (simple array allocation. a List<int[]> where each element contains 1024*1024 ints, so around 4MB for each entry). I’m testing with different memory allocation settings from PlayerSettings, hopefully will find a suitable config there.

That answers the crash, but not the reloading. For reloading, would you say keeping the game under 500MB can ensure that it’ll not reload on most iOS scenarios? because that’s a big task, would be a shame to do it all and see it’ll still reload :smiling_face_with_tear:(we already decreased memory down from 1GB to 500MB)

Update: changing initial memory to 1000MB and the memory alloc cap to 1000MB and the rate cap to 1:1 apparently fixed it :face_with_monocle: now it can go up to ~880MB before crashing or reloading. I still don’t understand why it should crash with a wasm message, but 880MB seems good

1 Like

nice! looks like your solved the issue by raising the memory cap :slightly_smiling_face:

if you are on Windows you can also use Unity + Firefox to profile there

1 Like

Thank you all for all the information you have provided.

I have succeeded in getting it to work on my iPhone.
I reduced the memory usage on the iPhone by setting the texture format to “ASTC”.
In return, it consumes memory when running on Android and PC browsers, but they do not stop using a lot of memory.

  • This text was translated by DeepL.
1 Like