Unity: WebGL에서 메모리 오류로 고생한 이야기

소개



안녕하세요, 여유롭게 엔지니어의 츠츠입니다.
블로그 운영하고 있으므로 꼭 흥미 있으면 봐 주세요.

Unity의 WebGL 플랫폼에서 메모리 오류에 대해 상당히 고민되었으므로 이 오류에 대한 해결 방법을 공유하겠습니다.

구체적인 오류 내용



오류 1



"memory access out of bounds"오류가 표시되고 Javascript 에서 SendMessage 를 사용하여 Unity 측 함수 호출이 오류가 되어 움직이지 않는 경우.
Invoking error handler due to
Uncaught RuntimeError: memory access out of bounds
Uncaught RuntimeError: memory access out of bounds
    at wasm-function[8336]:324
    at wasm-function[8331]:257
    at wasm-function[1315]:929
    at wasm-function[1391]:124
    at wasm-function[6601]:898
    at wasm-function[6598]:117
    at wasm-function[8314]:478
    at wasm-function[26579]:17
    at Object.dynCall_iiii (blob:http://127.0.0.1:8887/df084535-121f-44bd-9351-339a7a3e5c5c:2:422103)
    at Object.handlerFunc (blob:http://127.0.0.1:8887/df084535-121f-44bd-9351-339a7a3e5c5c:2:208347)
    at jsEventHandler (blob:http://127.0.0.1:8887/df084535-121f-44bd-9351-339a7a3e5c5c:2:204833)

오류 2



Unity 빌드 설정에서 "Development Build"를 선택하면 이 오류가 표시됩니다.


Stack overflow! Attempted to allocate 5242881 bytes on the stack, but stack has only 5242721 bytes available!
Invoking error handler due to
Uncaught abort("Stack overflow! Attempted to allocate 5242881 bytes on the stack, but stack has only 5242721 bytes available!") at Error
    at jsStackTrace (App.wasm.framework.unityweb:693:12)
    at stackTrace [Object.stackTrace] (App.wasm.framework.unityweb:707:11)
    at Object.onAbort (http://127.0.0.1:8887/Build/UnityLoader.js:1039:50)
    at abort (App.wasm.framework.unityweb:25388:20)
    at abortStackOverflow (App.wasm.framework.unityweb:752:2)
    at stackAlloc (wasm-function[590]:33)
    at blob:http://127.0.0.1:8887/cd9afda3-5ef3-4581-837c-a22f63982caa:24136:37
    at stringToC (App.wasm.framework.unityweb:368:10)
    at ccall [Object.ccall] (App.wasm.framework.unityweb:388:16)
    at SendMessage [Object.SendMessage] (App.wasm.framework.unityweb:72:151)
    at Object.SendMessage (http://127.0.0.1:8887/Build/UnityLoader.js:1057:50)
    at http://127.0.0.1:8887/:19:22

대책 방법



인스턴스를 생성할 때 "Module: { TOTAL_STACK: 6 * 1024 * 1024 }"를 지정해 보겠습니다. 다음은 제대로 작동했습니다!

덧붙여서입니다만, TOTAL_MEMORY/TOTAL_STACK 의 2개의 영역을 조정하는 것을 추천합니다.
메모리의 지정 사이즈는, TOTAL_MEMORY > TOTAL_STACK 가 되도록(듯이) 지정합니다.
    <script>
      var gameInstance = UnityLoader.instantiate(
                            "gameContainer", 
                            "Build/App.json", 
                             {
                               onProgress: UnityProgress, 
                               Module: { TOTAL_STACK: 6 * 1024 * 1024 }
                             });

      setInterval(function() {
        var text = "";
        for(var i = 0; i < 5*1024*1024 / 4; i++) {
          text += "*";
        }
        gameInstance.SendMessage('MyGameObject', 'MyFunction', text);
      }, 1000);
    </script>

기타



재현하는 소스 코드, 적당한 해설, 해결하기 위해 읽은 자료 등은 블로그 「Unity: WebGL에서 메모리 오류로 고생한 이야기」에 기재되어 있으므로 걱정되는 분은 꼭 참조하십시오.

좋은 웹페이지 즐겨찾기