Welcome back. 1
In this video we're going to discuss the layout of the virtual address space used by a process. In particular 2
we shall see, where the heap region resides, since this is the exact area we'll be working with in garbage 3
collectors class. 4
And what does memory allocation mean in terms of requesting extra memory from an operating system. 5
Now it turns out when we execute a process and operating system allocates for it what is called a virtual 6
address space or VAS for short. 7
Now this space is usually limited by the size of the machine word or the size of a pointer. For example 8
on a 32-bit machine 9
It is 2^32 10
that is 4GB 11
Now 1GB is usually reserved by the operating system itself 12
and this region is known as the Kernel space, and the 3GB are left to a process. 13
This actually depends on the implementation 14
For example on the Windows operating system by default only 2GB were available to a process 15
and the other 2GB were used by the operating system itself. 16
Now today our computers may have more physical memory than 4GB 17
However even previously when we had less memory, say for example just 500MB, a 18
process on a 32-bit machine still believed that it owned all your memory. 19
That is 4 addressable gigabytes. 20
Well, how is it possible? 21
Well, that's why this memory is called Virtual. 22
Now there is the process of mapping of virtual memory to physical addresses. 23
The Memory mapping process is also responsible for requesting or allocating more memory from an operating 24
system for a program. Will stop on the memory mapping process more closely 25
but for now we shall discuss the layout of the process memory in detail. 26
So let's see how an operating system partitions this address space to store all needed information to 27
run a process. 28
Now first of all the machine code of the process is loaded at the very low addresses of the virtual space. 29
Now these are actual machine instructions which are stored in the binary image of the file on disk and 30
loaded into memory 31
exactly at 32
this location on execution. Next, also at the lower area 33
we've got data segments. Now all static data known at compile time is stored here. 34
The data is stored also in the binary image and is loaded and these addresses. 35
Now there are two subsections: the area for the initialized variables, that is the variables defined 36
and assigned values 37
and for non-initialized variables, which will get the values later at runtime. 38
Okay. 39
So at this point we haven't actually started executing our program. 40
All we did is just load it is code and static data to memory. 41
Now we have to see how the arguments are passed to our process. 42
So let's take a look at this section as well. 43
Now the command line arguments and environment variables are passed and located at the very top addresses 44
of the virtual space. 45
And this area is followed by the program stack 46
the crucial area for the function executions. 47
Now recall from the previous lectures that the stack is used for procedure calls to store local variables 48
and past arguments. 49
Now the actual purpose and invention of the program Stack was to support recursive calls. When a function 50
is called, a new stack frame 51
also known as the Activation record, is allocated and pushed onto the stack. 52
When a function returns, the stack frame is deallocated. 53
Now, since the stack is located at the higher area it goes downwards. The top of the stack is determined 54
by the pointer known as the Stack pointer (usually hold by the ESP or RSP register), and the beginning 55
of each stack frame is pointed by the Base pointer or RBP register. 56
Now the fact that the state grows downwards can be seen in the negative offsets used in the code to 57
access needed parameters. Now somewhere here 58
there is also Stack limit pointer. And if the top of the stack meets it 59
we get the well-known Stack Overflow issue. 60
Now usually to prevent the exploiting stack and heap regions of a process, an operating system can add 61
random offsets excluding the constant addresses of those areas. 62
Now we should also say that this stack area is completely automatic. 63
There is nothing to manage here. 64
If you work in higher programming language starting from C you don't already deal with a stack allocation 65
and deallocation. In Assembly level 66
though we have to manipulate the Stack and Base pointers explicitly: this is usually known as a function 67
prologue and epilogue 68
when we create and destroy a stack frame. The real memory management happens in the area known as Heap 69
which we will be discussing next. And here, at the lower addresses we have our Heap 70
the area we're going to work with in our garbage collectors class. In contrast with the stack, the heap 71
grows upwards, and similarly to the stack pointer the top of the heap is determined by the pointer 72
known as Program break. 73
Now, allocating more memory from the operating system means bumping the program break as we will see 74
shortly. 75
however, a good heap allocator is usually more sophisticated than just trivially bumping the pointer. 76
Okay so what is then in between of the stack and the heap? 77
Now, this area is known as Memory mapping segment or Unmapped area. 78
Here the operating system maps different I/O devices, files, etc.. 79
And here it also allocates more dynamic memory for a process increase in the size of the heap. 80
And now let's see the memory mapping process itself in detail. As we said, the amount of physical memory 81
might be much smaller than the size of the virtual address space. 82
So let's take a look at the memory page mapping in action. As we see, some physical memory pages cover 83
the stack, some cover the heap, code segment, etc. And when there is no enough space in the physical memory 84
the operating system can use even disk space as a backend for the virtual addresses. 85
This is known as the Swap file in Unix and is completely transparent for the user program. 86
Now, the module called Memory management unit or MMU does the translation of the virtual pages to the 87
physical pages. 88
It works closely with a Translation lookaside buffer or TLB, which is the caching mechanism for this 89
translation. 90
So, at some point we can exhaust the whole currently mapped heap area, and at this point we can request 91
from an operating system to map more memory by increasing the program break pointer. 92
Once the memory is mapped it is available for the process to use. 93
Now, there are several system calls which allow manipulating the program break: brk, sbrk and the 94
most powerful -- mmap. 95
And on top of these system calls the well-known memory allocators such as `malloc` in C are built. 96
(yes, `malloc` is just a library function, and not a system call by itself) 97
OK. 98
Now when we know where the dynamic memory heap resides, and what the memory allocation means from the 99
operating system perspective, let's talk about Allocator, Mutator, and Collector: the main actors in 100
a garbage collected program. All of them operate exactly on the heap area which we will be discussing 101
in the next video.