Memory Leak
Subject: Computer and Technology noun /ˈmɛm.ə.ri liːk/ 1 definition
- memory-management
- software-engineering
- systems-programming
- performance-debugging
-
Definition (noun)
Entry #146 · UpdatedA Memory Leak is a software defect in which a program dynamically allocates heap memory for objects or buffers and fails to release that memory after those allocations are no longer needed. In manually managed languages such as C and C++, leaks typically occur when pointers to allocated blocks are overwritten or go out of scope without a matching
freeordelete, leaving orphaned memory unreachable yet still reserved by the process. Even in garbage-collected runtimes such as Java, Python, or JavaScript, memory leaks arise when long-lived structures, global caches, or active event listeners retain unintended references to obsolete objects, preventing the collector from reclaiming them. Over time, sustained leaks deplete available memory, trigger excessive paging or garbage-collection pauses, and cause out-of-memory crashes—distinguishing a leak from normal, bounded memory growth or intentional caching with explicit eviction.Common Types
Lost-Pointer Allocations: Occur in manually managed languages when the only pointer to a heap allocation is reassigned, overwritten, or falls out of scope before
free()ordeleteexecutes, stranding the block until process termination.Unintended Reference Retention: Happens in garbage-collected runtimes when static fields, closures, or registered callbacks continue pointing to unused objects, keeping them reachable from garbage-collection roots.
Reference-Counting Cycles: Arise in reference-counted memory models (such as Swift Automatic Reference Counting or C++
std::shared_ptrgraphs) when two or more objects hold mutual strong references, preventing their reference counts from ever dropping to zero.Unbounded Caches and Resource Handles: Develop when in-memory caches, memoization maps, or native resource wrappers grow monotonically without capacity limits, time-to-live (TTL) eviction, or explicit disposal calls.
Example
In a long-running Node.js WebSocket service, each incoming connection registers a closure on a shared global
EventEmitterwithout callingoff()when the socket closes. Over days of traffic, the emitter's listener array keeps every disconnected client's session buffer alive in the V8 heap, causing memory usage to climb monotonically until the process crashes with an out-of-memory error.