Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Think of WeakRef as a "gentle grip" on an object - you can hold it, but if no one else needs it, it can be garbage collected. FinalizationRegistry is like a cleanup crew that runs code when objects are thrown away. Together, they give you fine-grained control over memory!
A WeakRef lets you reference an object without keeping it alive. If the object has no other references, it can be garbage collected even if a WeakRef points to it.
// Create an object
let obj = { name: 'Important Data', value: 42 };
// Create a weak reference to it
const weakRef = new WeakRef(obj);
// Access the object (might be undefined if GC ran!)
const target = weakRef.deref();
if (target) {
console.log(target.name); // 'Important Data'
} else {
console.log('Object was garbage collected');
}
// Remove strong reference
obj = null;
// Later... the object might be gone
setTimeout(() => {
const target = weakRef.deref();
console.log(target); // might be undefined now!
}, 5000);Always check if deref() returns undefined - the object might have been collected!
Register objects and get notified when they're garbage collected. Perfect for cleanup tasks!
// Create registry with cleanup callback
const registry = new FinalizationRegistry((heldValue) => {
console.log(`Object with value "${heldValue}" was collected`);
// Cleanup resources here
});
// Register objects
let obj1 = { name: 'Object 1' };
let obj2 = { name: 'Object 2' };
registry.register(obj1, 'Object 1 data');
registry.register(obj2, 'Object 2 data');
// Remove references
obj1 = null;
obj2 = null;
// When GC runs, callbacks will be called
// Output: "Object with value "Object 1 data" was collected"
// Output: "Object with value "Object 2 data" was collected"class Cache {
constructor() {
this.cache = new Map();
this.registry = new FinalizationRegistry((key) => {
console.log(`Cleaning up cache for: ${key}`);
this.cache.delete(key);
});
}
set(key, value) {
// Store weak reference
this.cache.set(key, new WeakRef(value));
// Register for cleanup
this.registry.register(value, key);
}
get(key) {
const weakRef = this.cache.get(key);
if (!weakRef) return undefined;
// Dereference (might be undefined if GC ran)
const value = weakRef.deref();
if (!value) {
// Object was collected, remove from cache
this.cache.delete(key);
}
return value;
}
}
// Usage
const cache = new Cache();
let data = { large: 'data structure' };
cache.set('myData', data);
console.log(cache.get('myData')); // { large: 'data structure' }
// Remove strong reference
data = null;
// After GC, cache automatically cleans up
setTimeout(() => {
console.log(cache.get('myData')); // undefined
}, 5000);Cache large objects (images, parsed data) without worrying about memory leaks
Close file handles, network connections when objects are collected
Automatically unregister observers when objects are destroyed
Track object lifetimes and detect memory issues
You can't predict WHEN garbage collection will run. Don't rely on exact timing!
The object might be gone! Always handle undefined case.
These are advanced features. Regular garbage collection handles most cases fine!
Hold objects weakly - they can still be GC'd
Run cleanup when objects are collected
Caches, resource cleanup, memory management
Advanced feature - use only when needed!