Analysing Linux series: How is the RAM and VRAM related?
RAM stands for Random Access Memory, let's break this with a common example, there are some chips that are used for microcontroller, to extend the capacity of bytes that can be stored.
Microchip 23K256 RAM memory chip (a) and related packaging scheme with the description of the pins' functions (b).
This kind of RAM can store up to 256 KB of data, the Microcontroller communicates with it using the SPI protocol, but what's important is that we can control in what address we want to Read/Write information, data transmitted between a unit capable of following instructions and the RAM can be seen in the following diagram:
The RAM is a device that can be used to store/retrieve information on the desired memory position, let's call this kind of RAM Physical RAM.
In this case:
- The Microcontroller is just storing information for one task
- Using additional techniques to handle the Physical RAM can reduce the access time to the information, hence is no needed for small systems.
Physical memory was used this way even for the first computers with O.S like DOS, using it this way is not impossible but as programs get bigger we are going to face issues like:
- Lack of security since information is not isolated
- Inefficiency sharing resources since data is not labeled
- Handling the memory when creating software gets harder
How RAM works in actual computers?
Now if we have an operative system, we are going to handle a lot of tasks at the same time, (Kernel dependencies, graphic interface, applications, drivers, etc) imagine that every program writes data without some kind of system to organize the information. it would get messy in no time, and it can get slow since some memory couldn't be re-assigned without affecting other programs.
There's where Virtual RAM (VRAM) comes in handy, using this memory management technique we are giving to every software application a section that the software understands as its RAM that can be used by them for the task in need.
So that the memory of one process does not interfere with the other, and the process can know that all the memory in its used is after optimize the access time of information, for the apps is transparent that they are using virtual memory instead of the physical memory.
The Virtual RAM (VRAM) technique
Who's in charge of handling this task?
The Kernel has the responsibility to interact with the hardware in a way that the O.S can use its resources
How does the virtual memory map into the RAM?
- VRAM is divided into pages, Page tables (the structured used to store information) is created in order to translate Addresses from the VRAM to the Physical RAM
The content of the page tables differs a lot depending on how the VRAM technique is being applied, let's analyzed the content of a C software using the /proc filesystem in Ubuntu 14.04.
To get more information about the /proc filesystem you can read my previous blog here
Let's execute it and search for the PID with:
ps aux | grep main
Being 2109 the PID of our recently created C program, let's start to see what /proc/{id}/status has for us:
As we can see for the empty program, the least of VRAM designated was 4kB (This depends on the kernel) There's a lot of sections in the VRAM, but let's focus on the most common data (VmData 44 kB), stack (VmStk 132 kB), and text segments (VmExe 4kB) getting the information in this format is fast and easy to read.
[vdso] and [vsyscall] are resources needed by the kernel in every process to run in the system. The /proc/{id}/maps show us access to lib regions, that were put into the software as there's a linking step when compiling even an empty c code, we have reference to other programs.
If we add to our little program, the use of the heap now we can check that there's also a heap section
As we can see for the empty program, the least of VRAM designated was 4kB (This depends on the kernel) There's a lot of sections in the VRAM, but let's focus on the most common data (VmData 44 kB), stack (VmStk 132 kB), and text segments (VmExe 4kB) getting the information in this format is fast and not accurate, but easier to read.
What about the .data, .bss and .text segment? let's use a little trick for that
Using the extern variables, etext, edata, and end we can have information about the first address above the program text, the initialized data region, and the uninitialized data region, let's compare this with our mapping.
Contrasting the results we can create a visual mapping on how is the memory handled:
It's really interesting from the mapping that there's no need to have continuous addresses so that our software can effectively run, the kernel has a lot of logic to perform to be able to generate the VRAM for this software.
All the RAM memory handling is done by the kernel, if a software is desired to be running the kernel will check if the VRAM required to run that process is possible to be given, there are configuration options that can be applied to the kernel if there's a desire to make changes, that will affect the performance of running processes.
Thank you for reading!