Lucia Rodriguez
3 min readSep 1, 2020

--

Making room: The virtual memory

Taken from https://www.youtube.com/watch?v=qlH4-oHnBb8

This time the topic for the blog post at Holberton School’s System Programming & Algorithms specialization hits near home: I love Linux and Petronilo — my 12 old PC — is really slow. I now understand why.

Even if hardware and software development is skyrocketing often, resources are not infinite and this strained computer’s performance for a long time. Virtual memory is a technique based in an idealized abstraction of the storage resources available in a machine, which allows making use of a much larger amount of memory than the actual main one using indirection.

If a program memory addresses are mapped in the Random Access Memory (RAM) on that exact way, opening another program or process would be impossible if the first is consuming all the available RAM. On the other hand, using this approach every program addresses would need to be together, so if a program required amount of memory is not altogether in the RAM, the program may not launch, even if the total size of memory available can handle it. Moreover, the overlapping of two programs in the same address in memory can be a security threat for both processes so it’s a situation to avoid.

Virtual memory offers solutions for these three issues. Launched at boot, virtual memory handles two types of memory addresses: the virtual (the memory that the program “sees”) and the physical ones (actual memory direction on physical resources). Virtual memory spaces are chunked in smaller pieces -equivalent to 4 kb, 1024 words- called pages and they are the basic unit for mapping memory on the widely used pagination technique, which allows managing memory addresses with lesser mapping process than going word by word. Each page is an entry in the page table, the map between virtual and physical addresses.

Translating process is fast but a complex process, but here is the star power of the virtual memory way of life: if RAM is not enough, bring the disk to the scene! Besides virtual memory architecture and its implications about offsets and sizes, translating takes place because of a combined work of hardware and software: a memory manager unit (MMU) on the hardware side and the operating system on the software one. On Unix and Unix based operating systems, the software component that handles this process is the kernel.

While the MMU does the translation, the control of the effective assignment of this translation is in charge of the kernel. While RAM is the main resource for assigning memory, when it is full, the kernel will choose the oldest page frame (equivalent to the page but in physical memory) and paging it into the hard disk. If this memory address is required again and RAM has space, the memory address will be moved into it and its physical direction will be updated in the page table. Otherwise, the memory address will be reached on the disk, but this method has a disadvantage: accessing physical address placed on disk is a thousand times slower than accessing it on RAM.

Paging memory into the hard disk can be done through two methods: using a swap file or a swap partition. Accessing memory from a swap partition is faster than doing it from the swap file. You can configure a swap partition on Linux when you’re installing it from scratch but it’s not that easy when you have all instaled at Linux and you want to create a swap partition, you might need to rearrange your disk partitions and usage and sometimes it requires to format and reinstall all. Resizing the swap file is way easier (It is created, accessed, modified and deleted automatically)

TL; DR:

Taken from https://www.support.com/how-to/how-to-fix-a-slow-windows-pc-12850

If your PC is slow, ̶ ̶c̶l̶o̶s̶e̶ ̶p̶r̶o̶g̶r̶a̶m̶s̶,̶ ̶u̶s̶e̶ ̶l̶o̶w̶ ̶r̶e̶q̶u̶i̶r̶e̶m̶e̶n̶t̶s̶ ̶O̶S̶, ̶add more RAM!

Sources:

What is virtual memory?

Virtual Memory and Address Translation

Page- Wikipedia entry

Memory management Unit — Wikipedia

Virtual Memory — Introduction to Computer Architecture (Video series)

--

--