I have this following code snippet:
function outer() {
let a
return function inner() {
a = new Uint8Array(100000)
const b = new Uint16Array(100000)
};
};
const fn = outer();
fn()
Took one heap snapshot, ran it in a about:blank page in chrome 103.0.5060.114, and took a second snapshot. Comparing the two snapshots, I found that in the second snapshot, there is one more Uint8Array. That means a is retained in memory, so it leaked. But I can't find any Uint16Array so b didn't leak.

But I couldn't figure out why that Uint8Array is leaking because it doesn't seem like I can still reference it outside the outer function. So According to the garbage collection algorithm, it should have been collected by now.
Haven't tested this in other browsers or in Node.
ais retained because it is used byinner.inner==fn. If you deletefnI assumeawill be gone.bis also used byinnerbut it didn't get leaked. Plus I didn't return either so they are both unreachable from outside and thus should be all GC'ed.bis irrelevant because it is just a local variable, gone when the function ends. howeveraexists while the function exists. He is bound to it or the other way.afinishes executing, there is no way to reach toaanymore. it should have been GC'edais still referenced (closed over) byfn/inner, which is reachable (alive) itself, so it won't get garbage-collected. That there exists no code that can retrieve (read from)a, or - in more complicated cases - that all code that does retrieveais not reachable from the current state of the program and won't be executed, is not taken into account by the garbage collector. This would require advanced program analysis techniques, and is not generally decidable.