Cut the references that leak memory so the garbage collector can sweep it up: 18 levels of reachability, memory leaks and reference cycles.
JavaScript frees memory on its own. V8's garbage collector starts from the roots (global variables, document, timers still running, listeners on window), marks everything it can reach through references, and sweeps the rest. Memory leaks when there is still a reference to something no longer used: nothing is broken, it is just still reachable. In Mr. Tarjo's yard the roots are the shed's posts, every object a bundle, and every reference a rope with its code written on its tag. Whatever is no longer tied to any post goes on the Thursday cart.
A closure remembers the variables of the function it was made in. V8 keeps those variables in a context ([[Context]]), and every closure of the same call holds the same context. One closure still alive keeps every variable its sibling closures use, even if it never uses them itself. A timer still running is a root: setInterval(f, …) holds f until clearInterval.
V8's garbage collector works by mark and sweep: mark everything reachable from the roots, sweep the rest. Two objects holding each other are still swept if there is no way from a root to either of them. That is the difference from reference counting, which counts how many references point at each object and never frees a cycle.
An element taken off the page (remove(), replaceChildren()) is not necessarily free. It is called detached: not in the document, but still held by a JavaScript variable. A node holds its tree through parentNode and its children, so holding one small node can keep a whole piece of the page.
WeakMap and WeakRef change what holds. A WeakMap's key does not hold its object: once that object is reachable from nowhere else, the entry goes. Its value is held only while its key lives (V8 calls this an ephemeron). Right for extra data about objects another part of the program owns, like a photo for a resident object.
pesanan, a global object. The fried rice p1 has been delivered, but it is still tied to pesanan, so Mr. Tarjo's cart may not take it. Tap its rope and choose the line for antar() that cuts it. The iced tea p2 is still cooking: leave it.antrean. Dimas has been weighed, but his entry is still at antrean[0], so he is never let go. Tap his rope and choose the line for timbang(). Sekar and Bima are still waiting their turn.panitia.konsumsi and panitia.lomba, each with an anggota list. The games are over and their section is dissolved. Tap the games section's rope and choose the line for bubarkan() that lets the whole branch go. The catering section is still at work.Map and receipts in antreanCetak until the printer prints them. The fried rice p1 has been paid and its receipt printed, but both are still tied. Cut both ropes: one in bayar(), one in cetak(). The iced tea p2 is not paid yet.arisan.cadangan and never used again. What is announced is still arisan.putaran, and that round runs until next month. Let the spare go in umumkan() without letting the round go.pasangPengingat() prints the KMS card's photo once, then sets a health post reminder that rings every day. The reminder only uses jadwal, yet the photo is never let go. Follow the rope from the timer post: through what does the reminder hold that photo? Choose the line at the end of pasangPengingat(). The reminder must keep ringing.sebelum and sesudah entry, so they hold each other. October's book is closed and printed; its history is no longer used, but it is still tied through buku.terakhir. Choose the line for tutupBuku() that lets the whole history go. The cash book itself is still in use.<ul> list on the page. The neighbourhood head tapped the health post notice, so dipilih holds its <li>. When the week changes, the old list is taken off the page, but dipilih still holds one <li>. See what is held along with it, then choose the line for gantiMinggu().<section> panel. The Send button has a listener of its own, and window gets an online listener that updates the message count. When the chat is closed its panel is taken off the page, but it is never freed. Choose the line for tutup().Map pesanan and queued to dapur as well. The fried rice p1 has been delivered, and two ropes still hold it. While even one is left, it is not let go. Choose the lines for antar(). The iced tea p2 is not delivered yet.posyandu.aktif to posyandu.lulus, and once the report is sent to the puskesmas the graduates list is no longer used. Let the graduates go in kirimLaporan(), without letting go of Sekar, who is still active. The posyandu rope is used by both..berikut, so the history is one long chain. arsipkan() copies that chain to a file, then also keeps it in obrolan.lama, which is never read again. Let the whole chain go in arsipkan(). Today's message stays.And more, 18 levels in all.