facebook twitter hatena line email

Unity/負荷軽減/WebGL

提供: 初心者エンジニアの簡易メモ
2021年6月10日 (木) 16:06時点におけるAdmin (トーク | 投稿記録)による版

移動: 案内検索


範囲外のメモリアクセスのエラーを受け取る

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/

範囲外のメモリアクセスを引き起こす

Unity2019の場合

var unityInstance = 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.

OutOfMemoryのログ

Out of memory. は、それはそれでエラーメッセージがある?

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

以下のようにすれば受信できる?OutOfMemoryのメッセージは強制発生させることができなかったので、未検証

UnityLoader.Error.handler = function(e, t)
{
    if (e.message.indexOf("Out of memory") != -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/

TOTAL_MEMORYの変更方法

var unityInstance = 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

以下はout of memory のときに、Error.handlerでは受け取れず、ブラウザのconsoleに表示される

Could not allocate memory: System out of memory!

参考

メモリ変更: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