Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
JavaScript

JavaScript: Fix a WeakMap Key Surviving Longer Than Expected

- 24.11.26 - ErcanOPAK

⚡ JavaScript: Fix a WeakMap Key Surviving Longer Than Expected

A WeakMap only allows its entry to be garbage collected once there are zero remaining strong references to the key object anywhere in the program, because the WeakMap’s own reference to that key is deliberately weak and never counts toward keeping it alive. Holding the key object alive through any other strong reference, a closure capturing it, an array it was pushed into for logging, a cached DOM node reference, keeps the WeakMap entry alive right along with it, even though the code that actually cares about the caching behavior has long since finished using the key and expected the entry to have been reclaimed.

🔎 The Problem

const cache = new WeakMap();
const debugLog = []; // accidental strong reference

function processNode(node) {
  cache.set(node, computeExpensiveResult(node));
  debugLog.push(node); // keeps every node alive forever,
                        // so its WeakMap entry never gets
                        // collected even after the node is
                        // removed from the DOM and should
                        // be eligible for GC
  return cache.get(node);
}

✅ Fix: Hunt Down the Stray Strong Reference

  • Audit every array, object, or closure in the same module that might be holding onto the exact object used as a WeakMap key, since a single stray strong reference anywhere defeats the whole point of using a WeakMap for that cache.
  • Use the browser DevTools memory snapshot’s retainer path on a sampled object to confirm what’s actually keeping it alive, rather than assuming the WeakMap itself is the leak just because its entries aren’t shrinking.
  • If debug logging genuinely needs to reference the same objects, store a WeakRef to them instead of a direct strong reference, so the logging array itself doesn’t become the thing preventing collection.

🚨 Why This Is Dangerous

  • A cache intentionally built on WeakMap specifically to avoid memory growth can still grow unbounded in production, and because the WeakMap itself behaves correctly, the actual leak hides in whatever unrelated code is holding the key alive instead.

A WeakMap’s weak reference only matters if it’s the only reference — one stray strong one anywhere else in the program and you’ve built a regular Map with extra steps.

— JavaScript Tip

Related posts:

How to format date in Javascript

How to format date in Javascript

JavaScript: Fix a Web Worker postMessage Call That Silently Clones a Large ArrayBuffer Instead of Tr...

JavaScript: Fix a Web Worker postMessage Call That Silently Clones a Large ArrayBuffer Instead of Tr...

JavaScript: Build Real-time Data Dashboards with Array Methods

JavaScript: Build Real-time Data Dashboards with Array Methods

Views: 11
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Photoshop: Fix a Vector Mask Losing Anchor Points on Export
CSS: Fix a :has() Selector Missing Matches After a DOM Swap

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 629 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com