Is heap memory taken from RAM?


#1

When we talk about heap , do we refer to RAM ?


#2

Yup!


#3
Mostly yes...

“Mostly” because heap allocations are only defined to be “whatever the operating system gives back when your program asks for more memory”, and the part of modern operating systems that manages these dynamic memory allocations is full of black magic.

For example, the OS may not actually allocate RAM when you ask for a memory block, but only when you start accessing it.

The OS may also, depending on configuration, merrily give you more “memory” than there is available RAM, and compensate by silently moving data to and from disk.

In the end, you always end up reading from and writing to RAM when you access your heap allocations. But quite a lot may have happened under the hood before that.


#4

Perhaps a better way to describe it as “dynamic memory management”, or just “dynamic memory”. That captures the essence (dynamicism) without going into specifics too much.


#5

It’s not the only thing that takes up a program’s RAM, but it is the bulk of it in large programs.


#6

It is also worth noting the word “heap” means different things in different contexts.