Monday, March 17, 2014

To truly understand the stack and heap. It is helpful to fully look at the entirety of the memory allocation for any given process. And since there are several good sources for this information, in this article I hope to simply address the basics and summarize what others have previously explained.

Where in Memory is the Stack and Heap?

For a given process, a block of addresses are virtually assigned by the operating system for the stack, heap, data, and text of the code.

Per our diagram here is a summary of what each memory segment does:

Text/Code: This is total instructions for running a process and does not change during the runtime. This is where your compiled code resides in memory (traditionally, this would be the assembly code as it was written for the application). It does contain the code for the functions, but is not where memory is stored for the runtime of the function. This segment will not change in size during the runtime of the process.

Data/Global/Static: “Data” can be a confusing term for this segment since both the stack and the heap do contain in-memory data. However, this segment is specifically reserved for data that will be maintained for the lifetime of the process. In many programing languages this is known as global or static data. It should be noted that the primitive values, structs and object pointers are stored in this segment, but the dynamic data for an object is still stored in the heap (more on this later).

Stack: This contains a series of stack frames. Each stack frame contains addresses for a function’s return value, arguments, local variables, and back-pointers to the previous function code.

Heap: All object values that are referenced in the static and stack memory.

This is a little over simplified, since a, b, and f are stored on the stack as variable addresses that point to their corresponding values stored on the heap. However, both c and e are values stored directly on the stack. Also, it should be noted that even though YearsWithCompany is an int (primitive) value it is still stored in the heap since it is value of the Manager and Employee objects.