Skip to main content

Heap Memory

In Turkish
Heap Belleği
Pronunciation
HEEP MEM-uh-ree
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/heap-memory

In short

Heap memory is the region for data a program allocates at runtime, whose size or lifetime isn't known in advance and can outlive the function that made it.

What is heap memory?

Unlike the stack, the heap has no fixed order. A program asks for a block of memory when it needs one, with malloc in C or new in C++ and Java, or implicitly whenever it creates an object, list or string in languages such as Python and JavaScript, and the block stays valid until it is released. That makes the heap the place for large data, data whose size changes, and data shared between functions.

Someone has to free that memory. In C and C++ the programmer does, with free or delete, and forgetting causes memory leaks, while freeing too early causes use-after-free bugs, a major source of security holes. Rust tracks ownership at compile time to free memory safely, and languages such as Java, C#, Python, Go and JavaScript use a garbage collector that finds and frees objects no longer reachable.

Heap allocation is slower than stack allocation: the allocator has to find a free block of the right size, keep track of what is in use, and deal with fragmentation, where free memory is split into gaps too small to use. Performance-sensitive code therefore reuses objects, allocates in batches or keeps small, short-lived values on the stack.

A common misconception is that the heap memory region has something to do with the heap data structure. They only share a name: the memory heap is a pool managed by an allocator, while the heap data structure is a tree used for priority queues.

Key takeaways

  • The heap holds data allocated at runtime with flexible size and lifetime.
  • Objects that outlive a function call live on the heap.
  • C and C++ free it manually; Rust uses ownership; others use garbage collection.
  • Heap allocation is slower than the stack and can fragment.
  • The memory heap is unrelated to the heap data structure.

Example

Stack versus heap in Cc
#include <stdlib.h>
#include <string.h>

char *make_greeting(const char *name) {
    char local[16] = "Hello, ";              // stack: gone when the function returns
    size_t len = strlen(local) + strlen(name) + 1;

    char *greeting = malloc(len);            // heap: survives the return
    if (!greeting) return NULL;
    strcpy(greeting, local);
    strcat(greeting, name);
    return greeting;                         // the caller now owns this memory
}

int main(void) {
    char *g = make_greeting("Ada");
    /* ... use g ... */
    free(g);                                 // forget this and you have a memory leak
    return 0;
}

Readers ask

When is memory allocated on the heap?

When its size isn't known at compile time, when it is large, or when it must outlive the function that created it. In managed languages such as Java, Python and JavaScript, most objects are heap-allocated automatically.

What is a memory leak on the heap?

Heap memory that is no longer needed but never freed, because the program forgot to release it or still keeps a reference to it. The program's memory use then grows over time.

Is the heap the same as the heap data structure?

No. The memory heap is a region of memory managed by an allocator. The heap data structure is a kind of tree that keeps the smallest or largest element at the top. They simply share a name.

Often compared

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings