Search Unity

Unity3d 5.6 - WebGL - Memory usage

Discussion in 'Web' started by Deleted User, Apr 5, 2017.

  1. Deleted User

    Deleted User

    Guest

    Hey,

    before 5.6 we were able to examine the current memory usage through this code:

    Code (JavaScript):
    1. setInterval(function() {
    2.   if (typeof TOTAL_MEMORY !== 'undefined') {
    3.     try {
    4.       var totalMem = TOTAL_MEMORY/1024.0/1024.0;
    5.       var usedMem = (TOTAL_STACK + (STATICTOP - STATIC_BASE) +
    6.                     (DYNAMICTOP - DYNAMIC_BASE))/1024.0/1024.0;
    7.       console.log('Memory stats - used: ' + Math.ceil(usedMem) + 'M' +
    8.                   ' free: ' + Math.floor(totalMem - usedMem) + 'M');
    9.     } catch(e) {}
    10.   }
    11. }, 5000);
    This doesn't work anymore in 5.6
    Any advice to get the same functionality?

    And with 5.6 it seems like the memory footprint is generally bigger. Is this something to expect?

    Regards,
    Chen
     
  2. mikaelwallen

    mikaelwallen

    Joined:
    Jun 3, 2016
    Posts:
    56
    Below is a code snippet from the following blog post:
    https://blogs.unity3d.com/2016/12/05/unity-webgl-memory-the-unity-heap/

    Code (JavaScript):
    1. GetDynamicMemorySize: function() {
    2.         if (typeof DYNAMICTOP !== 'undefined') {
    3.             return DYNAMICTOP - DYNAMIC_BASE;
    4.         }
    5.         else {
    6.             // Unity 5.6+
    7.             return HEAP32[DYNAMICTOP_PTR >> 2] - DYNAMIC_BASE;
    8.         }
    9.     }
     
  3. Deleted User

    Deleted User

    Guest

    My bad. Didn't think that there would be already something for 5.6.

    Thanks for the help!
     
  4. AlexHell

    AlexHell

    Joined:
    Oct 2, 2014
    Posts:
    167
    It doesn't work in FF 57 nor Chrome 62.0.3202.75 portable
    Builded by unity 5.6.3p2

    it don't find TOTAL_STACK nor TOTAL_MEMORY nor any other (tried to comment lines and change if condition)

    my full version in index.html
    Code (JavaScript):
    1.  
    2. <script>
    3.    setInterval(function() {
    4.        console.log("tick");
    5.        if (typeof TOTAL_STACK !== 'undefined')
    6.        {
    7.            console.log("mem.."
    8.                //+ "GetTotalMemorySize = " + GetTotalMemorySize() + "; "
    9.                + "GetTotalStackSize = " + GetTotalStackSize() + "; "
    10.                + "GetStaticMemorySize = " + GetStaticMemorySize() + "; "
    11.                + "GetDynamicMemorySize = " + GetDynamicMemorySize() + "; "
    12.                + "GetReservedMemorySize = " + GetReservedMemorySize() + "; "
    13.            );
    14.        }
    15.      }, 5000);
    16.    function GetReservedMemorySize()
    17.    {
    18.        return GetTotalStackSize() + GetStaticMemorySize() + GetDynamicMemorySize();
    19.    }
    20.    function GetTotalMemorySize()
    21.    {
    22.        return TOTAL_MEMORY; // WebGLMemorySize in bytes
    23.    }
    24.    function GetTotalStackSize()
    25.    {
    26.        return TOTAL_STACK;
    27.    }
    28.    function GetStaticMemorySize()
    29.    {
    30.        return STATICTOP - STATIC_BASE;
    31.    }
    32.    function GetDynamicMemorySize()
    33.    {
    34.        if (typeof DYNAMICTOP !== 'undefined') {
    35.            return DYNAMICTOP - DYNAMIC_BASE;
    36.        }
    37.        else {
    38.            // Unity 5.6+
    39.            return HEAP32[DYNAMICTOP_PTR >> 2] - DYNAMIC_BASE;
    40.        }
    41.    }
    42.    </script>
     
    Last edited: Nov 23, 2017
  5. CodeBear

    CodeBear

    Joined:
    Jan 17, 2016
    Posts:
    4