Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug My mobile WEBGL build keeps restarting on iOS

Discussion in 'WebGL' started by gosoon, Aug 24, 2023.

  1. gosoon

    gosoon

    Joined:
    Jun 9, 2023
    Posts:
    9
    Hello, I apologize for using a translator because I am not good at English.

    I'm making an in-app game for mobile using WebGL, and when I run the game on IOS and play it, I'm experiencing a problem where the game is forced to restart from the beginning intermittently.

    As a result of doing everything possible, such as changing the WEBGL version to 1 or optimizing the game as much as possible, the frequency of bugs is significantly lower than before, but the problem is that the bugs still exist.

    A lot of people seem to be having the same problem as me. However, at least from my point of view, no one seems to have completely solved this problem.

    Please let me know if there is any information that could be helpful to me.

    (I'm using IOS version 16, Unity version 2022.3.4f1. The IOS version and this bug don't seem to be related...)

    +This usually happens when loading Scene.
     
    Last edited: Aug 24, 2023
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    On iOS this seems to be related to either the app being considered „unresponsive“ - meaning it may be taking too long to do one thing such as loading a scene. Or more likely running out of memory respectively memory pressure forcing the browser to shut down tabs that consume a large amount of memory.

    For WebGL on iOS the consensus seems to be that you can use at most 500 MB and it‘s best to stay well below that, like 350 MB.

    You should check the browser logs under settings. I think you need to enable development mode for Safari first. And use the profiler to check how much memory you are using. As a quick fix, in Build Settings choose one of the limit texture size options to generally reduce the size of textures. Try running that build and if that‘s stable you know it‘s a memory issue.
     
  3. gosoon

    gosoon

    Joined:
    Jun 9, 2023
    Posts:
    9
    First of all, thank you for your reply.

    I reduced the texture size in the build settings but the browser still refreshed.

    By the way, I tested the game through the Instruments app on my MacBook, and I got meaningful results. My problem is that the memory in the "compression" tab is not initialized every time I move the scene and keeps accumulating, so when the memory exceeds a certain number (like 500Mib) the app restarts.

    If you know anything about this issue, please share.
     
  4. gosoon

    gosoon

    Joined:
    Jun 9, 2023
    Posts:
    9
    Here's what I've found out so far:
    After running a game made with WebGL, the memory used by that web browser is increasing endlessly each time a scene is loaded within the game.
    The same thing happened when I ran the game in the Chrome browser on my desktop, and I noticed Chrome occasionally dropping memory again. However, Safari continues to increase memory usage without any such thing.

    So, now we need to find a solution to the problem that the memory used by the game keeps growing when changing scenes.

    Can anyone point me to a workaround for this issue? I've been looking for a way for 2 months.

    I apologize again for my poor English.
     
    Last edited: Aug 25, 2023
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    Then you have some sort of memory leak. You should use the Profiler and all other analysis instruments available to pinpoint the issue. And I bet it's not just a single issue, it is likely going to be many such issues.

    Just as an example: if you subscribe a lambda as an event handler and do this, this won't actually unsubscribe it because they are two separate objects:
    Code (CSharp):
    1. someClass.anEvent += () => { /* some code here */ };
    2. someClass.anEvent -= () => { /* some code here */ };
    There are countless of these things that could end up causing objects not being garbage collected. It's particularly bad if you have resources that may be duplicated. A common issue is accessing a renderer's .material property rather than .sharedMaterial since using .material will actually make a copy of the material.

    One cause of action would be to start with empty scenes and just reload them all the time, the same way as you do in the game. Then add scripts and resources to the scenes and see when memory usage starts to go up but never down. Then analyze this particular issue and keep doing that until repeated scene reloads aren't consistently increasing memory usage over time - some amount of extra memory will be used when loading scenes but eventually this should level out.

    You can also try calling Resources.UnloadUnusedAssets and GC.Collect to see if that helps - but it's not going to fix leaks. It does help to show the expected baseline after a scene load though.
     
  6. gosoon

    gosoon

    Joined:
    Jun 9, 2023
    Posts:
    9
    Perhaps, I seem to have found the cause:

    The results were interesting when BGM (AudioSource) was excluded from all components of the scene and only sound was included in the scene.

    Excluding sound only: game crashes after loading scene 200 times in 0.1 second intervals
    Contains sound only: game crashes after loading scene 20 times in 1 second intervals

    Also, when I checked on the console, I saw that the memory increased whenever a scene containing sound was loaded, and the memory did not decrease even if I moved the scene.

    Looks like someone has a similar problem to me, so I'll attach that link too: https://forum.unity.com/threads/audio-memory-in-webgl.1424112/

    Now all that remains is to figure out how to solve the memory leak problem while including the BGM.

    Thanks for your kind help, also if anyone knows anything about this issue please post.