Skip to content

Commit e2bd823

Browse files
committed
Add some more commands to test memory limits
1 parent c86e872 commit e2bd823

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Assets/Plugins/WebGL/WebBridge/CommonCommands.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ namespace Supyrb
2121
/// </summary>
2222
public class CommonCommands : WebCommands
2323
{
24+
/// <summary>
25+
/// List that stores allocated byte arrays to prevent them from being garbage collected
26+
/// </summary>
27+
private List<byte[]> byteArrayMemory = new List<byte[]>();
28+
2429
/// <summary>
2530
/// Disable capturing all keyboard input, e.g. for using native html input fields
2631
/// Browser Usage: <code>unityGame.SendMessage("WebGL","DisableCaptureAllKeyboardInput");</code>
@@ -58,6 +63,41 @@ public void LogMemory()
5863
WebToolPlugins.LogMemory();
5964
}
6065

66+
/// <summary>
67+
/// Allocate memory to test memory usage and limits
68+
/// The memory will be stored in a list to prevent it from being garbage collected
69+
/// </summary>
70+
/// <param name="mb">MB to allocate</param>
71+
[WebCommand(Description = "Allocate memory to test memory usage and limits")]
72+
public void AllocateByteArrayMemory(int mb)
73+
{
74+
byte[] memory = new byte[mb * 1024 * 1024];
75+
byteArrayMemory.Add(memory);
76+
Debug.Log($"Allocated {mb} MB of memory, total memory usage: {WebToolPlugins.GetTotalMemorySize():0.00}MB");
77+
}
78+
79+
/// <summary>
80+
/// Release all allocated byte array memory
81+
/// </summary>
82+
[WebCommand(Description = "Release all allocated byte array memory")]
83+
[ContextMenu(nameof(ReleaseByteArrayMemory))]
84+
public void ReleaseByteArrayMemory()
85+
{
86+
byteArrayMemory.Clear();
87+
Debug.Log("Released all allocated byte array memory, it can now be garbage collected");
88+
}
89+
90+
/// <summary>
91+
/// Trigger garbage collection
92+
/// </summary>
93+
[WebCommand(Description = "Trigger garbage collection")]
94+
[ContextMenu(nameof(TriggerGarbageCollection))]
95+
public void TriggerGarbageCollection()
96+
{
97+
GC.Collect();
98+
Debug.Log("Garbage collection triggered");
99+
}
100+
61101
/// <summary>
62102
/// Unloads all unused assets <see cref="Resources.UnloadUnusedAssets"/>
63103
/// Browser Usage: <code>unityGame.SendMessage("WebGL","UnloadUnusedAssets");</code>
@@ -67,6 +107,7 @@ public void LogMemory()
67107
public void UnloadUnusedAssets()
68108
{
69109
Resources.UnloadUnusedAssets();
110+
Debug.Log("Unloaded unused assets");
70111
}
71112

72113
/// <summary>
@@ -79,6 +120,7 @@ public void UnloadUnusedAssets()
79120
public void SetApplicationRunInBackground(int runInBackground)
80121
{
81122
Application.runInBackground = runInBackground == 1;
123+
Debug.Log($"Application.runInBackground: {Application.runInBackground}");
82124
}
83125

84126
/// <summary>
@@ -90,6 +132,7 @@ public void SetApplicationRunInBackground(int runInBackground)
90132
public void SetApplicationTargetFrameRate(int targetFrameRate)
91133
{
92134
Application.targetFrameRate = targetFrameRate;
135+
Debug.Log($"Application.targetFrameRate: {Application.targetFrameRate}");
93136
}
94137

95138
/// <summary>
@@ -101,6 +144,7 @@ public void SetApplicationTargetFrameRate(int targetFrameRate)
101144
public void SetTimeFixedDeltaTime(float fixedDeltaTime)
102145
{
103146
Time.fixedDeltaTime = fixedDeltaTime;
147+
Debug.Log($"Time.fixedDeltaTime: {Time.fixedDeltaTime}");
104148
}
105149

106150
/// <summary>
@@ -113,6 +157,7 @@ public void SetTimeFixedDeltaTime(float fixedDeltaTime)
113157
public void SetTimeTimeScale(float timeScale)
114158
{
115159
Time.timeScale = timeScale;
160+
Debug.Log($"Time.timeScale: {Time.timeScale}");
116161
}
117162

118163
/// <summary>
@@ -214,6 +259,7 @@ public void LogTextureSupport()
214259
public void DeleteAllPlayerPrefs()
215260
{
216261
PlayerPrefs.DeleteAll();
262+
Debug.Log("Deleted all player prefs");
217263
}
218264

219265
/// <summary>
@@ -226,6 +272,7 @@ public void DeleteAllPlayerPrefs()
226272
public void LogShaderCompilation(int enabled)
227273
{
228274
GraphicsSettings.logWhenShaderIsCompiled = enabled == 1;
275+
Debug.Log($"GraphicsSettings.logWhenShaderIsCompiled: {GraphicsSettings.logWhenShaderIsCompiled}");
229276
}
230277
}
231278
}

0 commit comments

Comments
 (0)