facebook twitter hatena line email

Unity/負荷軽減/WebGL

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

out of memoryについて

out of memoryを引き起こす

3Dの球をSphereとして、Resourcesへ格納。

void Update()
{
    for (int i = 1; i < 600000; i++)
    {
            GameObject prefab = (GameObject)Resources.Load("Sphere");
            GameObject obj = Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity);
     }
}

以下OutOfMemoryのログが、ブラウザのダイアログで出る

Out of memory. If you are the developer of this content, try allocating more memory to your WebGL build in the WebGL player settings.

以下ブラウザのconsole.logに出る(error logではなく通常ログ)

Could not allocate memory: System out of memory!

OutOfMemoryのログを受け取る

OutOfMemoryのログは通常ログなので、受け取れないが、直下に以下ログがでるので、それで判定可能?

Uncaught abort("abort(\"Cannot enlarge memory arrays. Either (1) compile with  -s TOTAL_MEMORY=X  with X higher than the current value 2147418112, (2) compile with  -s ALLOW_MEMORY_GROWTH=1  which allows increasing the size at runtime, or (3) if you want malloc to return NULL (0) instead of this abort, compile with  -s ABORTING_MALLOC=0 \")
window.UnityLoader.Error.handler = function(e, t)
{
    if (e.message.indexOf("Cannot enlarge memory arrays.") != -1)
    {
        console.log("message=" + e.message);
    }
};

参考:https://jpn.itlibra.com/article?id=10941

参考:https://nichijou-graffiti.com/dmm-browser-game-out-of-memory-message/

参考:https://forum.unity.com/threads/weg-gl-out-of-memory-alert.875533/

memory access out of boundsについて

out of memoryとは違うが、memory access out of boundsについて・・・。

memory access out of boundsのエラーを受け取る

window.UnityLoader.Error.handler = function(e: any, t: any)
{
    if (e.message.indexOf("memory access out of bounds") != -1)
    {
        console.log("message=" + e.message)
    }
}

"Uncaught RuntimeError: memory access out of bounds"のエラーを受け取れる

参考:https://forum.unity.com/threads/is-it-possible-to-catch-out-of-memory-errors-on-webgl.734366/

memory access out of boundsを引き起こす

Unity2019の場合

var unityInstance = window.UnityLoader.instantiate("unityContainer", "Build/webgl_project.json", {
    onProgress: UnityProgress,
    Module: { TOTAL_STACK: 9 * 1024  }
});
setInterval(function() {
    var text = "";
    for (var i = 0; i < 1 * 1024 * 1024; i++) {
      text += "a"
    }
    unityInstance.SendMessage('MyGameObject', 'MyFunction', text
}, 10000)

setIntervalはいらなかった。TOTAL_STACKのとこだけで良い。

参考:https://qiita.com/kingyo222/items/1995383a394251abd86d

他調査

throw new OutOfMemoryException();を実行すると以下エラーが起こる。他のでも起こるかも?だが・・

Uncaught undefined - Exception catching is disabled, this exception cannot be caught. Compile with -s DISABLE_EXCEPTION_CATCHING=0 or DISABLE_EXCEPTION_CATCHING=2 to catch.

TOTAL_MEMORYの変更方法

var unityInstance = window.UnityLoader.instantiate("unityContainer", "Build/webgl_project.json", {
    onProgress: UnityProgress,
    Module: {
        TOTAL_STACK: 10 * 1024,
        TOTAL_MEMORY:31 * 1024 * 1024
    }
});

以下エラーメッセージが出る

UnityLoader.js:4 failed to asynchronously prepare wasm: LinkError: WebAssembly.instantiate(): memory import 0 is smaller than initial 512, got 496
UnityLoader.js:4 LinkError: WebAssembly.instantiate(): memory import 0 is smaller than initial 512, got 496
Uncaught (in promise) abort({}) at Error

他エラーも起こる

以下は、Error.handlerで受け取れるエラー(これはループ処理をしたときのエラー)

Uncaught RangeError: Maximum call stack size exceeded

参考

メモリ変更:Player Settings/Publishing Setting/Memory Size(2018,2019では欄がない?) https://www.hanachiru-blog.com/entry/2019/10/04/000000

Loader.jsのソース https://gist.github.com/kyptov/f7e4718ee93b5c42bb975bc006fb10b4

Unity Playerの設定(TOTAL_MEMORYプロパティ)を外部から書き換えれば起こりづらくなる。 http://blog.livedoor.jp/hofupifoba/archives/17053937.html

htmlのBuildのjson内に、TOTAL_MEMORYというプロパティがあるので数値を直接修正できる。 https://jpn.itlibra.com/article?id=10941