Skip to content

Commit e8a3aa6

Browse files
committed
Merge branch 'master' of https://github.com/JohannesDeml/UnityWebGL-LoadingTest into 6000.0
2 parents a3714d5 + 9e03c3a commit e8a3aa6

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
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
}

Assets/Plugins/WebGL/WebTools/WebToolPlugins.jslib

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,41 +88,53 @@ var WebGlPlugins =
8888

8989
_GetTotalMemorySize: function()
9090
{
91-
if(typeof TOTAL_MEMORY !== 'undefined') {
91+
if(typeof HEAP8 !== 'undefined') {
92+
return HEAP8.length;
93+
}
94+
if(typeof TOTAL_MEMORY !== 'undefined') { // Legacy support
9295
return TOTAL_MEMORY;
9396
}
9497

95-
console.warn("Problem with retrieving unity value. TOTAL_MEMORY: " + typeof TOTAL_MEMORY);
98+
console.warn("Problem with retrieving total memory size");
9699
return -1;
97100
},
98101

99102
_GetTotalStackSize: function()
100103
{
101-
if(typeof TOTAL_STACK !== 'undefined') {
104+
if(typeof Module !== 'undefined' && typeof Module.STACK_SIZE !== 'undefined') {
105+
return Module.STACK_SIZE;
106+
}
107+
if(typeof TOTAL_STACK !== 'undefined') { // Legacy support
102108
return TOTAL_STACK;
103109
}
104110

105-
console.warn("Problem with retrieving unity value. TOTAL_STACK: " + typeof TOTAL_STACK);
111+
console.warn("Problem with retrieving stack size");
106112
return -1;
107113
},
108114

109115
_GetStaticMemorySize: function()
110116
{
111-
if(typeof STATICTOP !== 'undefined' && typeof STATIC_BASE !== 'undefined') {
117+
if(typeof Module !== 'undefined' && typeof Module.staticAlloc !== 'undefined') {
118+
return Module.staticAlloc;
119+
}
120+
if(typeof STATICTOP !== 'undefined' && typeof STATIC_BASE !== 'undefined') { // Legacy support
112121
return STATICTOP - STATIC_BASE;
113122
}
114123

115-
console.warn("Problem with retrieving unity value. STATICTOP: " + typeof STATICTOP + ", STATIC_BASE: " + typeof STATIC_BASE);
124+
console.warn("Problem with retrieving static memory size");
116125
return -1;
117126
},
118127

119128
_GetDynamicMemorySize: function()
120129
{
121-
if(typeof HEAP32 !== 'undefined' && typeof DYNAMICTOP_PTR !== 'undefined' && typeof DYNAMIC_BASE !== 'undefined') {
130+
if(typeof Module !== 'undefined' && typeof Module.dynamicAlloc !== 'undefined') {
131+
return Module.dynamicAlloc;
132+
}
133+
if(typeof HEAP32 !== 'undefined' && typeof DYNAMICTOP_PTR !== 'undefined' && typeof DYNAMIC_BASE !== 'undefined') { // Legacy support
122134
return HEAP32[DYNAMICTOP_PTR >> 2] - DYNAMIC_BASE;
123135
}
124136

125-
console.warn("Problem with retrieving unity value. HEAP32: " + typeof HEAP32 + ", DYNAMICTOP_PTR: " + typeof DYNAMICTOP_PTR + ", DYNAMIC_BASE: " + typeof DYNAMIC_BASE);
137+
console.warn("Problem with retrieving dynamic memory size");
126138
return -1;
127139
}
128140
};

0 commit comments

Comments
 (0)