| | 1 | | using UnityEngine; |
| | 2 | | using System.Runtime.InteropServices; |
| | 3 | |
|
| | 4 | | namespace Kongregate { |
| | 5 | | public class WebGLMemoryStats : MonoBehaviour { |
| | 6 | | [Tooltip("Interval (in seconds) between log entries")] |
| 0 | 7 | | public uint LogIntervalSeconds = 15; |
| | 8 | |
|
| | 9 | | public static uint GetUsedMemorySize() { |
| 0 | 10 | | return GetTotalStackSize() + GetStaticMemorySize() + GetDynamicMemorySize(); |
| | 11 | | } |
| | 12 | |
|
| | 13 | | #if UNITY_WEBGL && !UNITY_EDITOR |
| | 14 | | public static uint GetFreeMemorySize() { |
| | 15 | | return GetTotalMemorySize() - GetUsedMemorySize(); |
| | 16 | | } |
| | 17 | |
|
| | 18 | | void Start() { |
| | 19 | | InvokeRepeating("Log", 0, LogIntervalSeconds); |
| | 20 | | } |
| | 21 | |
|
| | 22 | | private void Log() { |
| | 23 | | var total = GetTotalMemorySize() / 1024 / 1024; |
| | 24 | | var used = GetUsedMemorySize() / 1024 / 1024; |
| | 25 | | var free = GetFreeMemorySize() / 1024 / 1024; |
| | 26 | | Debug.Log(string.Format("WebGL Memory - Total: {0}MB, Used: {1}MB, Free: {2}MB", total, used, free)); |
| | 27 | | } |
| | 28 | |
|
| | 29 | | [DllImport("__Internal")] |
| | 30 | | public static extern uint GetTotalMemorySize(); |
| | 31 | |
|
| | 32 | | [DllImport("__Internal")] |
| | 33 | | public static extern uint GetTotalStackSize(); |
| | 34 | |
|
| | 35 | | [DllImport("__Internal")] |
| | 36 | | public static extern uint GetStaticMemorySize(); |
| | 37 | |
|
| | 38 | | [DllImport("__Internal")] |
| | 39 | | public static extern uint GetDynamicMemorySize(); |
| | 40 | | #else |
| 0 | 41 | | public static uint GetFreeMemorySize() { return 0; } |
| 0 | 42 | | public static uint GetTotalMemorySize() { return 0; } |
| 0 | 43 | | public static uint GetTotalStackSize() { return 0; } |
| 0 | 44 | | public static uint GetStaticMemorySize() { return 0; } |
| 0 | 45 | | public static uint GetDynamicMemorySize() { return 0; } |
| | 46 | | #endif |
| | 47 | | } |
| | 48 | | } |