Random Access Memory

Random Access Memory

I dare you not to learn Data Structures before understanding how RAM works. Thank me later.

ยท

2 min read

Overview

Let's invent Data structures as per our needs from scratch, truly from scratch.
Yeah that's correct. Right from how RAM works.

Don't worry I will not throw bunch of theory on you, which you might have studied in your CS degree.

throwing.gif

Random Access Memory

When code is running on a computer ๐Ÿ–ฅ, it needs to store the variables.
These variables are stored in RAM (or working memory).

You might be thinking why not store in Hard drives ?
we will get to that point in later but in short understand that RAM has random access to data while in hard drives or even solid state drives we don't have direct access to each and every data point.

let's get back to work.
Assume RAM as a very tall shelf, each shelf can hold some data.

ram-empty-shelf.png

Assume same picture billions of time.

ram-address-shelf.png Each shelf is numbered sequentially, from 0 to infinite (not infinite but many).
These numbers are addresses.

ram-address-bits-shelf.png Each shelf can hold 8-bits of data, each bit can be high or low, but we right it as
1 -> high
0 -> low

CPU & Memory Controller

The real work needs to be done by CPU and the bridge between CPU and RAM is something what we called as Memory Controller.
Task of Memory Controller is to write and read data to and from RAM.

ram-processor-memorycontroler.png Each shelf is directly connected to Memory Controller that means you can directly access any shelf in no time (this is why RAM instead of hard drive or SSD).
You can access the 900 shelf then 1890 in no time as every shelf is directly connected to Memory Controller.
But by default computers are designed such that you memory addresses that are nearer to each other are accessed easily and faster than if they were far.

Cache

Let me introduce a new term 'cache'.
Yeah you might have heard about clearing up cache.
If Ram is fast to access, then cache is super fast to access.

cache .png

Whenever CPU requests data from RAM, Memory Controller sends bunch of near by data from what shelf.
Like if 900 was request then 901, 902, ....915 would have been sent to CPU which will be stored in cache for later super fast access.

To sum up what I have discussed in one picture image.png

tired.gif

Stay tuned for our next blog on Arrays up in this series. Link to Arrays Simplified.

ย