When I began my career in Computer Science, one of the technologies that amazed me most was how the memory of computers work and how they store data. As my career progressed I was constantly being introduced to several types of memory. I had several questions in my mind such as
- How does memory work on computers?
- Why there are so many types of memory?
- Can’t a single memory device be used for all purposes?
- If ROM is read-only then how come we are able to write data to our hard disks, USB flash drives, and SSDs?
- If I can access any file I need at any time from my SSD, how is it not Random Access?
As I kept learning about memory, the puzzle pieces in my mind started to fit together to give a more complete picture. This article tries to answer all the above questions, through simple analogies and explanations by going through its working principle, classification and applications of different types of memory keeping a beginner level reader in mind.
So let’s begin!
How does memory work on computers? Computer stores our data as bits. A bit can take only 2 values, either a Zero or One. This can be implemented using microscopic switches (electronic devices called transistors), logically, not very different from the one we use every day to turn off our lights.
Millions of such switches are used together to store useful information.
For example, the letter ‘a’ in ASCII format is the decimal 97, which can be written in binary as
“a” -> 97 -> 01100001
Similarly, b,c,d, and all the letters are implemented using different combinations of zeros and ones. 8 of these switches can have 256 combinations from all zeros to all ones.
Thus a single letter needs 8 of such switches to store its memory. So to store the letter “a” the computer takes a row of 8 switches and then flips the 2nd, 3rd and 8th switch in ON state and keeps the rest in OFF state. Computers have the capability to see which switch is ON and which is OFF, in other words, they can read the data stored in the memory. This is the way computer memory works on its lowest level.
To rewrite a byte of memory, the computer can simply flip all the 8 switches OFF (or ON) and then reflip (a.k.a reprogram) them to the necessary order.
What are memory addresses? Computer memory is implemented as a list of bytes, each with a unique address like in the picture above. A good analogy is a set of shelves, each with a unique name/number. And each shelf is big enough to store ‘X’ number of books.
A 32bit operating system uses 32bit addresses and thus can address 232 bytes.
232 = 4,294,967,296 bytes or 4GB of RAM. That’s the reason most computers these days use 64bit addressing to improve its RAM handling capabilities since 264 = 18,446,744,073,709,551,616 or 18,446,744,073GB of RAM, which is considered more than enough addressing capability for the coming decades!
Classification of Memory
What are the types of computer memory? Memory can be broadly classified into 2 types namely
- Primary memory &
- Secondary memory.
The real difference between primary and secondary memories is the speed/volatility tradeoffs.
What is primary memory? Primary memory is very fast, but it cannot hold data without power. Once you plug out the power the memory contents are erased. The popular name for Primary memory is, yes you guessed it correct, RAM (stands for Random Access Memory).
What is secondary memory? Secondary memory, on the other hand, does not care if the power is present or not, it can happily hold its contents. But compared to Primary memory, they are relatively slow. Popular implementations include the Hard disks, SSDs, USB Flash drives and SD cards.
Primary vs Secondary Memory
If RAM is volatile why do we still use it?
The reason is speed! The speed of the latest primary storage tech (DDR4 RAM) is about 25GB/s, while the latest secondary storage tech (NVMe SSD) is 4GB/s, hence the primary storage is roughly 6 times faster!
How is more speed more useful if the memory is volatile?
The processor is the fastest part of any computer system. The latest processors (Intel core i9-9900k) can handle easily 39GB/s of data. So it needs a fast memory to make proper use of its processing power.
As the processor crunches through our data, it needs a place to store the intermediary result. And that’s where RAMs come in.
You can theoretically build a computer and make it work using just the hard disk, but it will be incredibly slow and even with the latest SSD and a Processor, the performance will be slower than that of a 15-year-old PC with a proper RAM.
Primary and Secondary memory, working together!
Consider a situation, your boss gives you a simple task to add 4 numbers, say 1288,3216,5168 and 9090, you will do something like this on a rough piece of paper.
4504 and 9672 are the intermediate results here which is of no interest to your boss, so you will probably put the result on a nice report, print it out and give that to your boss. (you will probably just tell him verbally, but stay with me, I am just trying to make a point here..!)
Compare the above story to this analogy
Scribbling Pad: RAM (Both are short term holders of the result)
Report: Hard disk/ROM/Secondary memory (Both are long term records)
So once the processor completes the calculation using the Primary memory, it stores the results on the secondary memory.
I hope you managed to connect the dots… And that’s the reason we need a primary and secondary memory!
RAM and ROM, The naming convention/confusion!
The earliest computers had 2 memory chips, a RAM chip, and a ROM chip. These ROM chips were programmed at the manufacturing phase and cannot be reprogrammed. (They are also called as Masked ROMs). These ROMs were used to store the programs the computer can execute. The results are usually displayed to the user or printed on a paper. A famous example of this type of computer you can see today is a simple calculator!
Hence the earliest computer books used RAM and ROM as synonyms for Primary and Secondary memory and an entire generation learned it, and even though today’s primary and secondary storage are characterized by their speed/volatility tradeoffs, they still use the terms RAM and ROM to refer to primary and secondary memories time to time.
Random Access vs Sequential Access
RAM, as the name suggests, the memory locations can be accessed in any order.
To understand the reasoning behind this name, lets first have a look at its counterpart the “Sequential Access Memory”
A familiar example of this type of memory is magnetic cassette tapes that were used to store songs in the early 1990s like in the picture below. Here if you want to go to song number 10, you first need to go through songs 1 through 9 before getting to 10. (In case you never used one, You don’t have to listen to songs 1 through 9, you can just push the Forward Button!!)
If I can access any file I need at any time from my SSD, how is my Secondary memory not Random Access? The answer is, indeed they have the random access capability and technically they belong to a special category of memory device called the hybrid devices. They have both Random Access Capability (similar to RAMs)and they have a nonvolatile memory (similar to ROMs).
Hope this post was helpful and you got a basic understanding of how computer memory works.
If you liked the post, feel free to share this post with your friends and colleagues!