CS 100 (Learn)CS 100 (Web)Module 02


Primary Memory

Every computer requires some primary memory (or main memory) to work properly. Primary memory has also traditionally been known as the RAM (Random Access Memory) of the computer. The primary memory is the "working space" of a computer and everything that is being "done" on the computer (executing, displaying, calculating) is stored in the primary memory.

The CPU cannot access secondary storage directly. Instead, the information is copied from the secondary storage to primary memory, where it can be "used". For example, because your application programs are stored in your secondary storage, when you launch an application the operating system first copies the program into your primary memory and then starts the program. This is why there may be a delay when you launch an application.

Similarly, when you open a file (or document), the file is copied from secondary storage to primary memory. When you save a file, it copies the document from primary memory back to secondary storage.

The save floppy disk

Fun Fact: Even in modern software, the image used to represent saving your data back to secondary storage is often a floppy disk (one of the earliest secondary storage devices).

Saving your information back to secondary storage is important because primary memory is not permanently saved. If your computer loses power or crashes, the information in primary memory is lost. Many programs such as Microsoft Excel will "autosave" your data back to secondary storage in case there is a crash or power loss. Some operating systems (e.g., Windows) can "hibernate", causing the contents of the primary memory to be saved to secondary storage before the power is lost.

When you turn on your computer, the primary memory is initially empty (or blank). The computer then "boots up" by running a tiny program known as a boot loader. This boot loader is stored on a separate chip known as ROM (read-only memory). This boot loader then copies (loads) the operating system from secondary storage into primary memory so it can be run. Eventually, the operating system and start-up programs are loaded into primary memory and the computer becomes usable.

To help understand how the CPU, primary memory and secondary storage interact, we will use an analogy.

Consider a person (Cooper) who's brain is the CPU. Unfortunately, Cooper suffers from chronic amnesia and cannot remember anything that is not in front of him. Cooper is sitting at a desk representing the primary memory. Cooper can only see the pieces of paper placed on his desk (and he can't cheat by stacking papers). Down the hall, in another room is a filing cabinet filled with pieces of paper representing the secondary storage. Cooper can't access the filing cabinet directly. If Cooper needs a piece of paper from the cabinet, he has his assistant (Aaron) make a copy of the page in the cabinet and then Aaron places the paper on Cooper's desk.

The 'desk' memory model

At the start of each day, the desk is empty. To begin work, Cooper has Aaron copy several pages from the filing cabinet and place them on his desk. Cooper's can now "see" those pages and "work" on them. Cooper can read the pages, follow instructions on them, and even make changes to the pages by writing on them. However, at the end of the day all of the papers on his desk are destroyed. If Cooper wants to "save" any of his work, he has Aaron make copies of his (modified) pages and then place them in the filing cabinet, possibly replacing the old pages.

This analogy holds up surprisingly well and highlights the significant differences between primary memory and secondary storage:

  1. Capacity. Primary memory is much smaller than secondary storage. You can store a lot more information in a filing cabinet than on a desk. Primary memory is typically a few gigabytes (whereas secondary storage is measured in terabytes).
  2. Speed. Primary memory is significantly faster than secondary storage. It is a lot faster to view information on a desk than retrieve it from a filing cabinet. Even the fastest SSDs are 50 times slower than primary memory and hard drives can be 1000 times slower.
  3. Connectivity. The CPU can communicate directly and "see" the information in primary memory (the desk) but not secondary storage.
  4. Permanency. Imagine that the filing cabinet (secondary storage) is secure and fireproof, but the desk is completely cleared whenever Cooper leaves (shuts down) for the day.

Consider the size (capacity) of Cooper's desk and how that affects his productivity. With a large desk, he can have several "projects" going on at the same time and can access them all, but with a small desk he can only manage a few projects.

What if Cooper has run out of space on his desk, but he needs to look at another piece of paper? In this situation, Cooper will simply shred one of the pages to make room. Of course, Cooper is not careless and will shred one of the pages he knows already exists in the filing cabinet. If Cooper needs that page again, he can simply have Aaron make a new copy for him again later. In a really bad scenario where there is very little space on the desk, Cooper may need to shred a page he has modified (written on), in which case he can first have Aaron copy the page and put it in the filing cabinet before he shreds it.

The size of Cooper's desk illustrates how having more primary memory can significantly speed up a computer. With more primary memory, more programs can be running at the same time. In addition, more of each program can be loaded into the primary memory to avoid having to access the slow secondary storage. If you have ever seen a computer slow down because of too many programs running at the same time, it is because the primary memory has filled up, and the computer has had to resort to access the secondary storage more often. In our analogy, when a computer really slows down it may be because Cooper is continuously shredding the same pages over and over again because he just doesn't have enough room on his desk to get the job done, meanwhile Aaron is going crazy copying the same pages for Cooper over and over again.

Earlier, we mentioned that some CPUs have a cache. A cache is like a fast "short-term" memory built in to the CPU. It's neither primary memory nor secondary storage because it's built in to the CPU. Cache can be thought of as a type of "super" memory that is approximately 100 times faster than the primary memory (which in turn, is much faster than secondary storage).

In our previous analogy, Cooper had chronic amnesia and couldn't remember anything. He had to keep looking at his desk (primary memory) frequently. A cache would correspond to Cooper having a few post-it notes stuck to his arm. If Cooper can look at his arm without having to look at it on his desk, he will be much faster and productive. A larger CPU cache (more post-it notes) means that the CPU can perform more work before having to look information up in slower primary memory.