Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

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

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Tag: Garbage Collection

C#

C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon

- 16.11.26 - ErcanOPAK comment on C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon

🔧 C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon ConditionalWeakTable<TKey, TValue> ties an entry’s lifetime to its key object being reachable – the moment nothing else in the application holds a strong reference to the key, the garbage collector is free to collect it and the table drops that entry, value included, even if the […]

Read More
JavaScript

JavaScript: Fix a WeakMap Entry Disappearing Before You Expected

- 15.11.26 - ErcanOPAK comment on JavaScript: Fix a WeakMap Entry Disappearing Before You Expected

⚡ JavaScript: Fix a WeakMap Entry Disappearing Before You Expected A WeakMap holds its keys weakly, meaning an entry can be garbage collected — and silently vanish from the map — as soon as nothing else in the program holds a strong reference to that key object. Code that assumes “the DOM node is still […]

Read More
JavaScript

JavaScript: Fix a WeakRef’s Target Getting Garbage-Collected in the Gap Between an Async Callback’s Check and Its Later Use

- 14.11.26 - ErcanOPAK comment on JavaScript: Fix a WeakRef’s Target Getting Garbage-Collected in the Gap Between an Async Callback’s Check and Its Later Use

⚡ Fix a WeakRef’s Target Getting Garbage-Collected in the Gap Between an Async Callback’s Check and Its Later Use A WeakRef lets you hold a reference to an object without keeping it alive, which is exactly the point, but that also means the object it points to can be collected by the garbage collector at […]

Read More
Visual Studio

Visual Studio: Fix the Diagnostic Tools Memory Usage Graph Showing a Misleading Dip Because Taking a Snapshot Forces a Full GC Right Before It Measures

- 04.11.26 - ErcanOPAK comment on Visual Studio: Fix the Diagnostic Tools Memory Usage Graph Showing a Misleading Dip Because Taking a Snapshot Forces a Full GC Right Before It Measures

🛠️ Fix the Diagnostic Tools Memory Usage Graph Showing a Misleading Dip Because Taking a Snapshot Forces a Full GC Right Before It Measures The Memory Usage tool in Visual Studio’s Diagnostic Tools window needs an accurate, stable object graph to report live object counts and sizes, so every time you click “Take Snapshot” it […]

Read More
JavaScript

JavaScript: Fix FinalizationRegistry Cleanup Entries That Stop Arriving Entirely Because the Registry Instance Itself Was Only Reachable From One of Its Own Tracked Objects

- 01.11.26 - ErcanOPAK comment on JavaScript: Fix FinalizationRegistry Cleanup Entries That Stop Arriving Entirely Because the Registry Instance Itself Was Only Reachable From One of Its Own Tracked Objects

⚡ Fix FinalizationRegistry Cleanup Entries That Stop Arriving Entirely Because the Registry Instance Itself Was Only Reachable From One of Its Own Tracked Objects FinalizationRegistry lets you register a callback that runs sometime after an object it’s tracking becomes unreachable and gets garbage collected, which sounds like exactly the right primitive for cleaning up an […]

Read More
JavaScript

JavaScript: Fix a WeakMap-Based Cache Still Leaking Memory Because the Key Object Is Held Alive by an Unrelated Closure Elsewhere

- 30.10.26 - ErcanOPAK comment on JavaScript: Fix a WeakMap-Based Cache Still Leaking Memory Because the Key Object Is Held Alive by an Unrelated Closure Elsewhere

⚡ Fix a WeakMap-Based Cache Still Leaking Memory Because the Key Object Is Held Alive by an Unrelated Closure Elsewhere A WeakMap is specifically designed so its entries can be garbage collected the instant nothing else in the program still references the key object – the cache entry itself never keeps the key alive. That […]

Read More
JavaScript

JavaScript: Fix a WeakMap Entry Disappearing Early Because the Only Remaining Reference to Its Key Lived Inside a Closure the Engine Could Still Collect

- 23.10.26 - ErcanOPAK comment on JavaScript: Fix a WeakMap Entry Disappearing Early Because the Only Remaining Reference to Its Key Lived Inside a Closure the Engine Could Still Collect

⚡ Fix a WeakMap Entry Disappearing Early Because the Only Remaining Reference to Its Key Lived Inside a Closure the Engine Could Still Collect A WeakMap entry is supposed to stay alive for as long as something else in the program is still holding a strong reference to its key object – that’s the entire […]

Read More
C#

C#: Fix a Finalizer That Silently Keeps an Object Alive an Extra GC Generation Because SuppressFinalize Was Never Called

- 16.10.26 - ErcanOPAK comment on C#: Fix a Finalizer That Silently Keeps an Object Alive an Extra GC Generation Because SuppressFinalize Was Never Called

🔧 The Finalizer That Kept Resurrecting Objects an Extra Generation Any object with a finalizer (a C# destructor, ~MyClass()) isn’t collected the moment it becomes unreachable – the garbage collector instead moves it onto a special finalization queue, runs the finalizer later on a dedicated thread, and only then lets the object actually die on […]

Read More
C#

C#: Fix a Params Array Parameter That Silently Allocates a Brand New Array on Every Single Call

- 16.10.26 - ErcanOPAK comment on C#: Fix a Params Array Parameter That Silently Allocates a Brand New Array on Every Single Call

🔧 The params Array That Allocated on Every Single Call A params parameter is convenient at the call site – you just pass a comma-separated list of arguments – but the compiler has to put those arguments somewhere, and that somewhere is a freshly allocated array, built at every call site, every single time, even […]

Read More
JavaScript

JavaScript: Fix a WeakRef Target That Gets Collected Earlier Than Expected Because Nothing Else Holds a Strong Reference to It

- 11.10.26 - ErcanOPAK comment on JavaScript: Fix a WeakRef Target That Gets Collected Earlier Than Expected Because Nothing Else Holds a Strong Reference to It

⚡ The WeakRef That Let Go Sooner Than Expected A WeakRef deliberately holds a reference to its target that the garbage collector is free to ignore entirely, which is the whole point of using one, caching a large object without being the reason it stays alive. The part that catches people off guard is how […]

Read More
C#

C#: Fix a ConditionalWeakTable Entry That Disappears Because Nothing Else Keeps Its Key Alive

- 10.10.26 - ErcanOPAK comment on C#: Fix a ConditionalWeakTable Entry That Disappears Because Nothing Else Keeps Its Key Alive

🔧 The Cache Entry That Vanished With Its Own Key A ConditionalWeakTable is designed to attach extra data to an object without extending that object’s lifetime, which means the entry’s lifetime is tied entirely to whether something else still holds a strong reference to the key. When the key passed in is a short-lived wrapper […]

Read More
JavaScript

JavaScript: Why a WeakMap Silently Loses Its Entry the Moment the Key Object Gets Garbage Collected

- 04.10.26 - ErcanOPAK comment on JavaScript: Why a WeakMap Silently Loses Its Entry the Moment the Key Object Gets Garbage Collected

⚡ The WeakMap Entry That Vanished On Its Own A WeakMap holds its keys weakly on purpose – storing an object as a key does not keep that object alive, so once every other reference to it is gone, the garbage collector is free to reclaim it, and its entry disappears from the map along […]

Read More
C#

C#: Why a Finalizer Doesn’t Run the Moment an Object Becomes Unreachable, and Why That Matters for Handles

- 03.10.26 - ErcanOPAK comment on C#: Why a Finalizer Doesn’t Run the Moment an Object Becomes Unreachable, and Why That Matters for Handles

🔧 The Finalizer That Ran Whenever the GC Felt Like It A finalizer looks, on the page, like a guaranteed cleanup step that runs once an object is done – the syntax even echoes a destructor from languages where that’s true. In .NET it isn’t: a finalizer only runs after the garbage collector decides an […]

Read More
C#

C#: Find and Fix Memory Leaks From Event Handlers You Forgot to Unsubscribe

- 30.08.26 - ErcanOPAK comment on C#: Find and Fix Memory Leaks From Event Handlers You Forgot to Unsubscribe

🔧 Missing -= = Silent Memory Leak Your desktop or long-running C# app’s memory usage climbs steadily until it crashes with an OutOfMemoryException — but there’s no obvious “leak” in your code. The culprit is almost always an event handler that was never unsubscribed. 🐞 The Problem public class ReportWindow { public ReportWindow(DataService service) { […]

Read More
C#

C# Memory Management Deep Dive: GC, IDisposable, and Unmanaged Resources

- 19.08.26 - ErcanOPAK comment on C# Memory Management Deep Dive: GC, IDisposable, and Unmanaged Resources

Memory leaks, high GC pressure, and unmanaged resource leaks plague many C# applications. Understanding the memory management model is essential. 1. Garbage Collection Fundamentals: // GC Generations // Gen 0: Short-lived objects (collected frequently) // Gen 1: Buffer between Gen 0 and Gen 2 // Gen 2: Long-lived objects (collected rarely) // Large Object Heap […]

Read More
JavaScript

JavaScript: Advanced Memory Cleanup with WeakRef

- 01.03.26 - ErcanOPAK comment on JavaScript: Advanced Memory Cleanup with WeakRef

In complex SPAs, caching large objects can lead to ‘Slow Leaks’. WeakRef allows you to hold a reference to an object without preventing it from being Garbage Collected. let cache = new WeakRef(largeObject); // Later… const obj = cache.deref(); if (obj) { /* Use object */ } else { /* Re-fetch, it was cleaned up […]

Read More
JavaScript

JavaScript: Preventing Memory Leaks with WeakMap and WeakSet

- 21.02.26 - ErcanOPAK comment on JavaScript: Preventing Memory Leaks with WeakMap and WeakSet

When you store data in a regular Map, the reference prevents the Garbage Collector (GC) from cleaning up. Use WeakMap for private data. let privateData = new WeakMap(); class User { constructor(id) { privateData.set(this, { secret: ‘123’ }); } } When the ‘User’ object is deleted, its private data is automatically wiped from memory.

Read More
C#

C# Performance: Use Span and Memory to Eliminate Array Allocations

- 01.02.26 | 01.02.26 - ErcanOPAK comment on C# Performance: Use Span and Memory to Eliminate Array Allocations

Processing large arrays or strings eating up memory? Span<T> lets you work with slices of memory without allocating new arrays. The Problem – Traditional Approach: // BAD: Creates new array on every call public byte[] ProcessChunk(byte[] data, int start, int length) { byte[] chunk = new byte[length]; // Heap allocation Array.Copy(data, start, chunk, 0, length); […]

Read More
Asp.Net Core / C#

.NET Core: Fix Memory Leaks by Understanding IDisposable and Using Patterns

- 01.02.26 | 01.02.26 - ErcanOPAK comment on .NET Core: Fix Memory Leaks by Understanding IDisposable and Using Patterns

Your .NET application’s memory usage grows infinitely? You’re probably not disposing resources properly. Here’s the complete guide. The Memory Leak – Classic Example: // WRONG: Memory leak public async Task ProcessOrdersAsync() { var dbContext = new ApplicationDbContext(); var orders = await dbContext.Orders.ToListAsync(); // Process orders… // dbContext never disposed = connection stays open = memory […]

Read More
November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (981)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (609)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (547)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (981)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

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