Analysing Linux series: How is the RAM and VRAM related?
Written by David Orejuela

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.

No alt text provided for this image

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:

No alt text provided for this image

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.


No alt text provided for this image

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?

  1. 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

No alt text provided for this image

Let's execute it and search for the PID with:

ps aux | grep main
No alt text provided for this image

Being 2109 the PID of our recently created C program, let's start to see what /proc/{id}/status has for us:

No alt text provided for this image

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.

No alt text provided for this image

[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.

No alt text provided for this image

If we add to our little program, the use of the heap now we can check that there's also a heap section

No alt text provided for this image
No alt text provided for this image

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

No alt text provided for this image

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.

No alt text provided for this image

Contrasting the results we can create a visual mapping on how is the memory handled:

No alt text provided for this image

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!


要查看或添加评论,请登录

David Orejuela的更多文章

  • How to use multi-threading to increase your app performance

    How to use multi-threading to increase your app performance

    Sometimes when we code the paradigm of using multiple threads can be a little scary, but multi-threading is a common…

  • Introduction to ELF

    Introduction to ELF

    What is ELF We are not talking about Christmas ELF, here we are talking about the Unix ELF, Executable and Linkable…

  • Analysing Linux series: The /proc Filesystem

    Analysing Linux series: The /proc Filesystem

    Every time we run Software in Linux a process is created and an ID is assigned to that process (starting from 1 and in…

  • Our experience launching Hovify

    Our experience launching Hovify

    “If you spend too much time thinking about a thing, you’ll never get it done.” – Bruce Lee Our project is a…

    1 条评论
  • What happens when you type https://www.holbertonschool.com in your web browser and press Enter

    What happens when you type https://www.holbertonschool.com in your web browser and press Enter

    First, let's stat that you are on your web browser and you type like this: https://www.holbertonschool.

  • Entering the IOT world

    Entering the IOT world

    “If you think that the internet has changed your life, think again. The IoT is about to change it all over again!” —…

  • Machine learning for everybody

    Machine learning for everybody

    “Machine learning will automate jobs that most people thought could only be done by people.” ~Dave Waters Machine…

  • Python the world of objects

    Python the world of objects

    Abstraction is one of those notions that Python tosses out the window, yet expresses very well. - Gordon McMillan…

  • Entering the world of Class and Instance attributes

    Entering the world of Class and Instance attributes

    In the one and only true way. The object-oriented version of 'Spaghetti code' is, of course, 'Lasagna code'.

  • Applying dynamic and static Libraries in C programming

    Applying dynamic and static Libraries in C programming

    Let's start by defining library, a library is a collection of modules stored in object format (1's and 0's) this…

社区洞察

其他会员也浏览了