Loading topic
free() or delete obj returns memory to the system. A component of the engine called the garbage collector (GC) finds memory the program can no longer use and reclaims it. Before you can use WeakRef or `Finaliza6 min readMap and Set, and have read the previous chapter on reachability. The question here is narrower: which references count when the garbage collector decides whether an object is reachable? Most of them count. A few spec6 min readWeakRef. ES2015 added WeakMap and WeakSet, two collections that hold their entries without keeping the keys alive. Knowing exactly what these collections guarantee, and what they cannot do, explains why WeakRef and FinalizationRegistry were added l6 min readSymbol() and Symbol.for(), and have read the earlier chapters on WeakRef and deref(). It covers which values the engine will accept as the target of a weak reference, why it rejects the rest, 5 min readWeakRef and call deref(), and that you understand the event loop at a conceptual level: tasks, microtasks, and the idea that synchronous code runs to completion. It explains one specific rule in the specification. Once your code has seen a we5 min readWeakRef lets you check whether an object is still alive, but only when you ask. FinalizationRegistry works the other way around. You give it a callback, and the engine may call that callback after an object you registered has been garbage collected. This chapter covers the constructor, the `re5 min readFinalizationRegistry and its cleanup callback. This chapter covers the second argument to register(), the held value. It is the only information your callback receives. Choosing it badly can keep the target alive forever, so it needs care.5 min readregister(), called the unregister token, lets you cancel a registration before the callback 5 min readWeakRef, call deref(), and register objects with a FinalizationRegistry using held values and unregister tokens. It also assumes you understand jobs and the event loop at a conceptual level. Here we look at what the ECMAScript specification6 min readWeakRef constructor and deref(), how FinalizationRegistry.register() uses held values and unregister tokens, and that the engine makes no promises about when callbacks run. Here those pieces are combined into a cache whose values can be garbage collect5 min readFinalizationRegistry, held values, and unregister tokens from earlier chapters. It also assumes you know that cleanup callbacks have no timing guarantees. Here those pieces come together in one practical pattern: using finalization to catch resources that code5 min readWeakRef and FinalizationRegistry APIs from earlier chapters, including held values and unregister tokens, and the weak-value cache pattern. It also assumes you are comfortable with closures and async/await.5 min readWeakRef, deref(), FinalizationRegistry, and the fact that a dereferenced target stays alive until the current job ends. You should also be able to run Node.js from a terminal and open Chrome DevTools. The goal is practical. You will learn how to se6 min readWeakRef and FinalizationRegistry can do. This chapter covers the opposite question: when you should not rely on them. It assumes you know JavaScript classes, closures, try/finally, and promises, and that you have read the earlier chapters on finalization timi5 min read