Skip to main content
Definition Website Computer and Technology

Heap Memory

Subject: Computer and Technology noun /hiːp ˈmɛm.ər.i/ 1 definition

  1. Definition (noun)

    Entry #142 · Updated

    Heap Memory (commonly referred to simply as the heap) is a pool of a process's virtual address space reserved for dynamic memory allocation when data sizes or lifetimes are unknown at compile time or must outlive a single function call. Unlike stack memory, which automatically allocates and reclaims scoped frames in last-in, first-out (LIFO) order, heap allocations remain valid across function boundaries until explicitly released by application code or reclaimed by a garbage collector. Coordinated by a memory allocator or language runtime that requests backing pages from the operating system, heap memory enables flexible, shared, and variable-sized data structures at the cost of allocator metadata overhead, synchronization, fragmentation, and the risk of memory leaks or dangling pointers.

    Common Forms

    • Manually Managed Heap: Controlled explicitly by the developer through allocation and deallocation primitives—such as malloc/free in C or new/delete in C++—providing deterministic memory release and minimal runtime overhead at the risk of memory leaks, double frees, or use-after-free bugs.

    • Garbage-Collected Heap: Managed automatically by a language runtime—such as the Java Virtual Machine (JVM), Go runtime, or Python interpreter—which periodically traces live object graphs or reference counts to reclaim unreachable memory, preventing dangling pointers in exchange for collector CPU overhead and occasional latency pauses.

    • Region-Based (Arena) Heap: Groups objects with shared lifetimes into a contiguous pre-allocated memory arena where individual allocations advance a pointer rapidly and the entire region is reclaimed in one step, minimizing fragmentation and per-object deallocation cost.

    • Thread-Local Heap: Partitions heap storage into per-thread caches or sub-heaps—used by allocators like jemalloc and tcmalloc—so concurrent threads can satisfy frequent small allocations without contending on a global heap lock.

    Example

    When an image-processing service written in C receives a 24 MB PNG upload, it calls malloc() to reserve a contiguous buffer in heap memory because the decoded pixel array is too large for the call stack and must remain accessible across multiple filter stages. Once the transformed image is written back to the HTTP socket, the worker calls free() to return that heap chunk to the allocator for reuse by subsequent requests.