Computers store data at different levels throughout the system. The memory hierarchy is a pyramid or triangular structure that helps to visualize the relation between size and speed of different types of physical memory. The top of the hierarchy being the smallest and fastest CPU registers and the bottom being the largest and slowest external storage devices.
When a processor needs a particular piece of data it will start at the top of the hierarchy and work it’s way down level by level until it finds that data. The top three tiers of the hierarchy, registers, caches, and main memory are called volatile memory because they rely on power to retain data. When the power source is removed or turned off they will lose the data they are storing. The bottom levels are considered permanent storage because they will retain the data with or without power.
This is just a high level overview of the different types of physical memory involved in the computers we use and the programs we write as developers. Each one of these levels can be expanded upon, if you are interested dive deeper into understanding how each work and interact with one another. Use this as a starting point or a refresher to dive deeper into understanding how the systems we use store and transfer data.
The fastest and smallest of all memory. These are Static RAM that exists on the processor that allow practically instant access. They usually hold a single word (64 or 128 bits). Physical space is highly limited on the processor. This reduces the number of registers as each one takes up a certain amount of space on the processor. Only the most important information is contained in the registers.
Different manufactures will put different registers on their processors. Systems designed for complex instruction sets have fewer registers as they are designed for accessing main memory whereas reduced instruction sets have many more registers. The program counter is a register common to all processors. It keeps track of where the next instruction to be run in a program is located. A few other common registers include a register for decision making and an accumulator for math operations.
The next fastest after registers, cache can be found on the processor or on another chip closer to the processor than main memory. It is larger than a register but smaller than main memory. Cache contains chunks of data from main memory that are used frequently. The more often a piece of data is called from main memory the higher it’s likelihood of being copied into the cache.
From the view of a program or app the cache doesn’t really exist. Programs request information from the main memory and if it’s stored in the cache because of frequent use then it is readily available and takes less time to access. Cache is divided into levels based on size, location, and usage. Single core processors typically have up to two levels of cache whereas multi-core will have 3-4 levels, two per core and then one or two shared levels.
Random Access Memory (RAM)
Significantly slower than the processor’s memory and generally stored a distance, for electrons, away from the processor the main memory is much larger than memory stored on the processor. Each time the processor has to access main memory it slows the running process down. At the time of this recording it comes in the Gigabyte range.
Main memory or RAM contains most of the data and instructions necessary for the programs that are currently running. When there is excess RAM some Operating Systems will “cache” data of common programs to make loading them faster. There are two types of RAM. DRAM (Dynamic RAM) is the most common used in a computer’s main memory. It is made up of a circuit of cells each containing a transistor and a capacitor. SRAM (Static RAM) is less common and consists of four to six transistors.
Power is required to maintain state in main memory. If the power is lost then the information stored in RAM is lost. DRAM requires an electric charge to be refreshed every few milliseconds whereas SRAM needs a constant electrical current making it faster but more expensive.
The way main memory is built it is not able to retain data when there is no power to the system. Secondary memory is where data is stored long term for retrieval between power downs. It consists of solid-state drives or hard-disk drives. SSDs are faster than HDD because they do not have to wait for a disk to spin up. This makes them more expensive, therefore it may be cheaper if you need a large amount of storage, in the terabyte range, to purchase an HDD.
SSDs also wear out faster than HDD because of the way data is written to them. Data is written in pages but removed in blocks. When a block of data is changed such as altering a document then the old page is marked as invalid and a new page is created for that data. When there are no more new pages the invalid ones are overwritten. This process causes extra writes on the disk which leads to more wear as each write wears the cells of the SSD.
Secondary memory does not require power to retain data. It is also more dense memory as it has much higher storage capacity with less cost than the previous forms of memory. Data stored in secondary memory is loaded into main memory before it can be used for a running program. This accounts for most of the load time when starting an application or when retrieving data.
Since RAM is limited, yet required for running programs, if you do not have enough to run all the active program on a system the operating system will use a feature called Virtual Memory. This compensates for the shortage of RAM by transferring some of the less frequently used data to disk storage. Virtual memory blends main and secondary memory. When RAM is running low the virtual memory transfers data from main memory to a paging file on secondary memory. If virtual memory is low then either the paging file size needs to be increased or more RAM needs to be added so less virtual memory is used.
The transfer of data from RAM to the paging file on secondary memory is called swapping. It is a temporary solution that is designed to allow the RAM and secondary memory to work together. Virtual memory use slows down the programs being run because it costs time to write to the secondary memory and retrieve the data when needed.
External storage refers to any form of data storage external to the system. This includes external hard drives, flash drives, CD/DVD drives, floppy disks, magnetic tape, etc. External storage devices can be removable from the system as the data is expected to be moved around from machine to machine.
Removable storage devices tend to be even slower than secondary memory. USB 3.2 and Thunderbolt, however have gotten speeds similar to some secondary memory. Online storage is a unique form of external storage that is cloud based and done through storage on a server, either your own or someone elses. This could be through a NAS or a storage solution like Dropbox, Sharepoint, or One Drive./p>
Tricks of the Trade
There are different levels of optimization. Use the right one, rather than always opting for the most optimized form. There is a tradeoff between overall system performance per unit of cost and the performance of all the components of the system.