Virtual Memory, Physical Memory, and Their Calculations

Tomoharu Tsutsumi
2 min readAug 30, 2023

Many software engineers assert that understanding memory is crucial for excelling in the field. A deep knowledge of memory allows engineers to write code that is efficient and doesn’t waste computational resources. I had the opportunity to study both physical memory and virtual memory during my college years. Here’s a brief summary of what I learned.

Memory needs to be consecutive

For data structures like arrays, hashes, and lists to function properly, memory must appear to be consecutive to the programmer. Imagine how confusing it would be if, when writing code, the index following 3 was 10. However, the physical memory in a computer is limited, making it challenging to allocate it consecutively. In reality, for optimal computer performance, memory allocation often happens randomly. Therefore, it’s essential that memory at least appears consecutive to us, even if it’s not.

Page table was developed

To address this discrepancy, the concept of the page table was introduced. Here’s a simple illustration: Virtual memory 0 points to 2 in the page table, which then corresponds to 2 in the physical memory. Other colors follow the same pattern. From a programmer’s perspective, virtual memory appears consecutive (like 0, 1, 2), but the actual physical memory might be allocated non-sequentially (like 2, 5, 8).

How to find the location of virtual memory and physical memory

While not essential for understanding memory, I found the method of mapping virtual memory to physical memory fascinating. Assume one unit of memory requires 1024 bits. A location in virtual memory might be represented as <1, 333>. Here, 1 points to 8 in the page table. The corresponding physical memory location (or address) is calculated as 1024 * 8 + 333, which equals 8525. Conversely, a physical memory location represented by the number 5140 can be broken down as 5140 / 1024, which gives 5 with a remainder of 20. Thus, the address in virtual memory is <2, 20>.

Feel free to reach out to me on LinkedIn, which you can find below. Looking forward to connecting!

https://www.linkedin.com/in/tomoharu-tsutsumi-56051a126/

--

--

Tomoharu Tsutsumi

Senior Software Engineer at two industry-leading startups ( Go | Ruby | TypeScript | JavaScript | Gin | Echo | Rails | React | Redux | Next)