Understanding memory partitions
Now that we know that computer programs, no matter what language/framework they are written in, become processes in the computer memory when executed, the next step is to understand the structure of these processes when loaded into memory for execution.
As shown in the diagram below, the memory (RAM) associated with a process is partitioned into several sections when it is loaded for execution.
The structure of a process in memory
Each section has its purpose and is responsible for storing different types of information related to a program when it is executing. The memory of a process is organised into the following key sections:
Heap memory
Heap memory is a region of a process's memory used for dynamic allocation, where data can be created and sized at runtime rather than at compile time. It is a large pool of memory that allows variables, objects, and data structures to persist beyond the scope of the function in which they were created, making it ideal for dynamically sized or long-lived data. Depending on the type of language, heap memory is managed differently.
Low level languages
In low-level languages like C and C++, heap memory is manually managed, i.e., programmers must explicitly allocate it using functions like malloc or new and release it with free or delete. Failure to free memory after use results in a memory leak, where the OS treats the memory as still in use, making it unavailable for other tasks. Data items created on the heap are accessible anywhere in the program.
Liking the course? Check our discounted plans to continue learning.