196

What is a memory heap ?

H4cKL0rD
  • 4,775
  • 15
  • 47
  • 72

7 Answers7

259

Presumably you mean heap from a memory allocation point of view, not from a data structure point of view (the term has multiple meanings).

A very simple explanation is that the heap is the portion of memory where dynamically allocated memory resides (i.e. memory allocated via malloc). Memory allocated from the heap will remain allocated until one of the following occurs:

  1. The memory is free'd
  2. The program terminates

If all references to allocated memory are lost (e.g. you don't store a pointer to it anymore), you have what is called a memory leak. This is where the memory has still been allocated, but you have no easy way of accessing it anymore. Leaked memory cannot be reclaimed for future memory allocations, but when the program ends the memory will be free'd up by the operating system.

Contrast this with stack memory which is where local variables (those defined within a method) live. Memory allocated on the stack generally only lives until the function returns (there are some exceptions to this, e.g. static local variables).

You can find more information about the heap in this article.

Chuck Vose
  • 4,355
  • 21
  • 29
LeopardSkinPillBoxHat
  • 26,928
  • 14
  • 70
  • 108
  • 3
    How could the local variables live in a stack? A stack only allows to take one variable at a time in a very specific order. What if I need a local variable from somewhere lower on the stack? – CodyBugstein Jul 02 '15 at 21:07
  • 11
    @Imray - in a statically typed language, the sizes of the local parameters are known at compile time. Therefore the local variables can simply be accessed directly from the stack via an address offset. There is no need to pop the stack to do this. See [this answer](http://stackoverflow.com/a/9247682/22489) for more details. – LeopardSkinPillBoxHat Jul 03 '15 at 01:09
19

A memory heap is a location in memory where memory may be allocated at random access.
Unlike the stack where memory is allocated and released in a very defined order, individual data elements allocated on the heap are typically released in ways which is asynchronous from one another. Any such data element is freed when the program explicitly releases the corresponding pointer, and this may result in a fragmented heap. In opposition only data at the top (or the bottom, depending on the way the stack works) may be released, resulting in data element being freed in the reverse order they were allocated.

mjv
  • 67,473
  • 12
  • 100
  • 152
11

Heap is just an area where memory is allocated or deallocated without any order. This happens when one creates an object using the new operator or something similar. This is opposed to stack where memory is deallocated on the first in last out basis.

fastcodejava
  • 35,219
  • 24
  • 124
  • 181
9

A memory heap is a common structure for holding dynamically allocated memory. See Dynamic_memory_allocation on wikipedia.

There are other structures, like pools, stacks and piles.

Priyantha
  • 3,687
  • 4
  • 21
  • 40
Justicle
  • 13,619
  • 13
  • 66
  • 91
7

It's a chunk of memory allocated from the operating system by the memory manager in use by a process. Calls to malloc() et alia then take memory from this heap instead of having to deal with the operating system directly.

Ignacio Vazquez-Abrams
  • 699,552
  • 132
  • 1,235
  • 1,283
7

You probably mean heap memory, not memory heap.

Heap memory is essentially a large pool of memory (typically per process) from which the running program can request chunks. This is typically called dynamic allocation.

It is different from the Stack, where "automatic variables" are allocated. So, for example, when you define in a C function a pointer variable, enough space to hold a memory address is allocated on the stack. However, you will often need to dynamically allocate space (With malloc) on the heap and then provide the address where this memory chunk starts to the pointer.

Uri
  • 84,589
  • 46
  • 214
  • 312
-1

every running process has its own private fake virtual memory provided by the OS. the OS can map this to physical memory at any point as long as it is available otherwise it will map to disk and swap as needed. this virtual memory is logically divided into segments for organizing different kinds of data. the code segment holds the executable instructions. the data segment holds static data such as global or static variables. the stack holds local data that is automatically managed by called and returning functions. all of these segments are fixed size even the stack its just the portion used can grow or shrink and is reclaimed as functions returned. the only segment that is not preallocated at app startup and fixed size is the heap. the app can request from the OS at runtime new memory to be allocated and the OS will reserve a part of your apps virtual space and commit that to physical memory as needed. the OS will return a pointer to that newly allocated heap memory and that pointer holds the base or starting address of the new block. that pointer sits on the stack and when that stack space is reclaimed your pointer will be no longer in scope and therefore you have no means of access to that block of memory. and if you dont tell the OS you are done with it so it can reclaim it that is just zombie memory sitting there with no means of access and if your app keeps requesting memory while never giving it back it will crash when the system runs out of memory. so it is important to free or at least pass the pointer to another pointer external to the scope it was defined in so you can maintain an interface to that memory allocated in heap space. i would suggest looking into virtual memory further and understanding segments.