Search Unity

How do you profile memory for a standalone WebGL application?

Discussion in 'Web' started by VirtualMaestro, Apr 11, 2022.

  1. VirtualMaestro

    VirtualMaestro

    Joined:
    Jul 2, 2014
    Posts:
    22
    We have a big project, and there is a massive memory leak. This behavior can't be reproduced in the Editor, so we have to find out the leak by running the game in a browser (in the best case in release mode).

    (I'm compiling the project with Emscripten flags: "--profiling-funcs --memoryprofiler")

    All the official info I've found is about how to run and profile either directly from Unity, which is not a way for us, or to use the profiler in Firefox. You can't attach Unity Editor to the already running game in a browser. The problem with Firefox's profiler is that it shows good performance CPU statistics but not for memory (and of course, it doesn't give such cool features, like snapshots and comparing, that Unity's "Memory profiler" provides).

    Maybe I'm wrong and Firefox's profiler can handle such cases, and I didn't find them?
    Can you share your experience and how you solve it?
    Maybe you know some third-party profilers that can help with it?

    (maybe JetBrains dotTrace can somehow help here or any other tools you know?)



    Thank you in advance
     
  2. CodeStarrk

    CodeStarrk

    Joined:
    Jan 5, 2017
    Posts:
    37
    I'm in the same situation right now, hoping someone can help us
     
    VirtualMaestro likes this.
  3. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    273
    Here are some tools for profiling memory in a WebGL application based on the category of memory you're looking at:
    1. Total javascript memory in a page:
      1. Firefox about:memory page shows memory allocation breakdowns for each process/webpage. I've included a screenshot of what it shows for a test WebGL application.
      2. Besides Chrome's in-browser memory tab in the dev tools, you can also the performance.memory API to get information about the current total/used Javascript heap size. This works on Chrome only right now.
      3. As for Safari, I don't know of anything that useful outside of their in-browser dev tools.
    2. WASM heap memory: the --memory-profiler flag will build the page with an added section that breaks down the WASM heap memory usage. Since the WASM heap memory is part of the total JS memory, you can then see how much of the memory used on the page is coming from the WASM heap as opposed to everything else in your page.
    3. C# managed memory: The Unity Profiler package will show you detailed info about managed memory. You'll see breakdowns of textures/meshes/materials. A lot of the time we recommend that you run the profiler to the WebGL build and a native build, and see if there's a major difference between the two, if there isn't, you can then turn your attention towards the javascript memory.
    I hope this helps! :)
     

    Attached Files:

    VirtualMaestro and MartinTilo like this.
  4. CodeStarrk

    CodeStarrk

    Joined:
    Jan 5, 2017
    Posts:
    37

    How do I enable this --memory-profiler flag?
     
  5. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    273
    You can enable it by adding a script to your project like so:
    Code (CSharp):
    1. using UnityEditor;
    2.  
    3. public class memoryprofiler
    4. {
    5.     [MenuItem("HTML5/Activate MemoryProfiler")]
    6.     static void foo() {
    7.         PlayerSettings.WebGL.emscriptenArgs = "--memoryprofiler --profiling-funcs";
    8.     }
    9. }
    and then clicking on the Activate MemoryProfiler under the HTML5 menu item the script created, and then it should show up in your resulting WebGL page once you build.
     
    Last edited: Apr 19, 2022
    CodeStarrk likes this.
  6. risyalfebrianto97

    risyalfebrianto97

    Joined:
    Aug 2, 2022
    Posts:
    8
    excuse me but how can i disable this?
     
  7. andreifilip

    andreifilip

    Joined:
    Feb 14, 2018
    Posts:
    2
    easy
    PlayerSettings.WebGL.emscriptenArgs = ""
     
    risyalfebrianto97 likes this.
  8. UnityProdS

    UnityProdS

    Joined:
    Jan 11, 2014
    Posts:
    42
    @unityruba PlayerSettings.WebGL isn't it deprecated now?
     
    travlake likes this.