mml-io / mml

Metaverse Markup Language
https://mml.io
MIT License
120 stars 13 forks source link

[Bug]: Significant memory leak with <m-frame "load-range"="..."> #178

Closed tbergmueller closed 2 months ago

tbergmueller commented 3 months ago

Describe the bug

When using <m-frame> to partition a large MML-Document (e.g. a large world), a straight-forward approach is to use load-range and unload-range in 3d-web-experience. So when structuring an MML-Document properly into <m-frame "load-range":"*"> and keeping the maximum memory consumption of all m-frames loaded simultaneously under a certain threshold, practically open-worlds can be created.

This works perfect for loading and unloading, however, the memory allocated during loading is not deallocated when unloaded. This causes the heap to build up over time and eventually crash the application (client-side).

Video: https://youtu.be/JFctnH2PCds

The vast majority of allocations comes from MML's ModelLoader.ts, which internally uses ThreeJS GltfLoader. Without getting into the weeds of the code, I assume that somewhere in the MML-Library a reference to the Data Array is not cleaned up properly when unloading an m-frame.

image

Expected behavior

When a is unloaded, the memory is freed. This eventually crashes the application after loading too many , even when the currently loaded m-frames only account for a fraction of the allocated memory.

How to reproduce the bug

<m-group id="ducks"></m-group>

<script>
  const stairs = document.getElementById("stairs");

  const spacing = 5;
  const s = 4
  const loadRangeFactor = 1.3;

  for (let i = 0; i < 20; i++) {
    for (let j = 0; j < 20; j++) {
      const duck = document.createElement("m-frame");
      duck.setAttribute("x", `${i*spacing}`)
      duck.setAttribute("z", `${j*spacing}`)
      duck.setAttribute("min-x", -s)
      duck.setAttribute("max-x", s)
      duck.setAttribute("min-y", -s)
      duck.setAttribute("max-y", s)
      duck.setAttribute("min-z", -s)
      duck.setAttribute("max-z", s)
      duck.setAttribute("load-range", s*loadRangeFactor)
      duck.setAttribute("debug", true)

      const src = "https://digitalsoul.metaanchor.io/api/v1/assets/RDFC/0xd9a98f803ffac7a8217a55d61a81b1ab2aa0e9e6b5b47cffb942fd6e5967d2a0"

      duck.setAttribute("src", src)
      ducks.append(duck);
    }
  }
</script>

You can of course use other SRC-Documents as well.

Link to the code that reproduces this bug

No response

Packages

mml-web, mml-3d-web-experience

Package versions

v0.17.0

Extra details

No response

tbergmueller commented 2 months ago

Thank you @MarcusLongmuir , confirm it's not leaking anymore, memory consumption for the same test as described in the issue is now stable!

image