Memory & How Computers Store Data

Β·

3 min read

Memory & How Computers Store Data

High-Level Overview

To understand the true value of Data structures, we have to dig a bit deeper into the way computer works at the fundamentals level.

In order to computer to run the program we wrote, it needs to keep track of things like variables (strings, number, array, boolean...) somewhere in memory.

βœ… These variables are stored in RAM (Random Access Memory)

πŸ‘‰ Data stored in RAM will be lost when the computer turns off.

βœ… In addition, we also have Storage

πŸ‘‰ Is the place to store files, images, videos, music files, documents...

πŸ‘‰ Data on storage is Permanent or Persistent

πŸ‘‰ When you turn off the laptop, the data still be there when you turn back on.

βœ… CPU (Central Processing Unit)

πŸ‘‰ think of it as a little worker that does all the calculations, actual work inside our computer.

πŸ€” πŸ€” Storage is great. Why don't we store data in Storage all the time?

In order to run our code, the CPU need access to data stored in the RAM and the Storage.

πŸ‘‰ But the CPU can access the information in the RAM A LOT FASTER. 🀩


Memory & How does the computer store data in the RAM?

πŸ‘‰ Data stored in Memory of our computer will be stored in form of Byte

πŸ‘‰ 1 Byte = 8 Bits

πŸ‘‰ Bit is a tiny electrical switch that can be turned On or Off. But instead of calling on / off, we call it 1 or 0

πŸ‘‰ Bit the smallest building block of storage.

πŸ‘‰ 8 bits - 1 byte can make 256 different patterns

πŸ‘‰ Mathematically: n bits yields 2n patterns (2 to the nth power)

howard phung

(Image source: ZTM)

Think of RAM in the computer as a massive storage area.

You can call it a bounded canvas.

Each shelf inside the canvas is called a Memory Slot.

We only have a limited amount of Memory Slot.

Each memory slot holds a number - it's a Memory Address.

Our computer uses this number to look for the value stored in the form of 8 bits or 1 byte.

The CPU is connected to the Memory Controller.

Memory Controller does the job of reading, and writing data to the memory.

The direct connection to the Memory Controller is really important because we can access the memory really fast.

The closer the information is to the CPU, the less it has to travel, and then the Faster the program can run. The computer is tuned to run faster when reading memory addresses that are close to each other.

Further optimization, our computer has CPU Cache (LRU Cache)

The CPU has a tiny memory space, where it stores a copy of stuffs that is really recent.

Next time when you want to lookup this piece of information you already visited recently, the process will be much faster.

How computers store a value in RAM

In modern computers, usually we represent an integer in Fixed-width integer (32bits, and 64bits).

For instance, we're using a computer with a 32-bit integer (4 bytes), thereby it's going to take 32 / 8 = 4 memory slots to store an integer inside our RAM, similarly, with 64-bit (8 bytes), it'll take 64 / 8 = 8 memory slots.

πŸ‘‰ From memory slot number 0 to 3, we stored the value of var a = 1
πŸ‘‰ From memory slot number 4 to 7, we stored the value of var b = 7

That's it. Have a great day, see you in the next one soon!

Β