I write a simple page to test Garbage Collection.
Monitor performance panel data changes by repeatedly adding and removing IFrames
- iframe page content
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
#view3d {
width: 100%;
height: 100%;
overflow: hidden;
position: relative;
}
</style>
</head>
<body>
<canvas id="view3d"></canvas>
</body>
<!-- <script src="./babylon.js"></script> -->
</html>
A minute after removing the iframe page, the internal dom still hasn’t been freed
- iframe page content
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
#view3d {
width: 100%;
height: 100%;
overflow: hidden;
position: relative;
}
</style>
</head>
<body>
<canvas id="view3d"></canvas>
</body>
<script src="./babylon.js"></script>
</html>
15 seconds after removing the iframe page, the internal dom is released
Therefore, I think babylon did some processing before unloading the page;
Then I went to babylon’s code and found this:
At this point, I think babylon manually destroyed the canvas element to avoid memory leaks
I tried to do the test in the same way:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
#view3d {
width: 100%;
height: 100%;
overflow: hidden;
position: relative;
}
</style>
</head>
<body>
<div id="view3d"></div>
</body>
<script type="module">
var fn = () => {
let dom = document.querySelector("#view3d");
const parentControl = dom.parentElement;
parentControl.removeChild(dom);
dom = null;
};
window.addEventListener("beforeunload", fn);
</script>
</html>
It did not have the desired effect and the dom was not destroyed
Maybe I’m looking in the wrong place
Maybe someone can show me the right way to destroy the dom and achieve memory reclamation
Thanks !!!