Memory Overview
Similar to the human brain, computers use memory to store and remember information and use this information to perform operations. It's very important to have an idea of how it works and its impact on it while coding.
Memory comes in some flavors of size and speed and provides the processor with the information needed to run programs. But why do we need to care about it? Because...
There is no unlimited memory.
And problems managing the memory could lead to low performance, freezing apps, and even devices.
Processor & Memory
When you run a program, the magic happens inside the processor and it's orchestrated by the Operation System (OS). The processor can perform one piece of information at a time, so just one part of the program runs inside the processor at a time.
- The OS takes some piece of data from the memory;
- Delivers this piece of data to the processor;
- The processor execs the instructions and generates results;
- The OS deals with the results, takes the instruction from the processor, puts it back in the memory, and loads new instructions to be processed.
Principle of locality
The locality principle rules the memory architecture and could be divided into two separated parts:
- Temporal locality: If an Item was referenced, it will tend to be referenced again soon. Ex: Loops in the programs.
- Spatial locality: if an item is referenced, items whose addresses are close by will tend to be referenced soon. Ex: instructions are accessed sequentially - natural spatial locality.
Memory Hierarchy
The different types of memory are organized in a hierarchy, with multiple levels with different speeds, sizes. Data between levels are presented in blocks/lines.
Main memory types:
Magnetic: It's a Non-volatile memory type of memory - that can maintain the data without power. Reads and Writes the data in a magnetic disk with moving mechanical parts during disk spins. It's the slowest and cheaper memory.
Flash: Also a Non-volatile memory - The data is electrically erased and reprogrammed. It's faster than magnetics once it accesses the data address directly, without mechanical moving parts, but slower than RAM memories. This type of memory is used in Solid State Drives.
DRAM: Dynamic Random Access Memory - It's a volatile memory. Due to electrical leaks in the capacitor, this type of memory requires a refresh circuit that periodically rewrites the data in memory (consumes more power). That is the computer's main memory.
SRAM: Static Random Access Memory - The fastest and most expensive type of memory. It has operation speeds close to the processor speeds. It's a volatile memory - but needs lower power to maintain the data, and does not need refresh circuit. It is normally used as Cache and it's the first level that provides data for the processor.
Data Flow
Memory hierarchy has multiple levels, but the data copy occurs just between two adjacent levels at a time. If the needed data is in Disk, it's necessary to copy to the upper level, and so on successively until it reaches the processor.
If the block the processor needs is in the upper level - HIT
If the block the processor needs is NOT in the upper level - MISS
When a MISS occurs, look for the block in the lower level.
HIT Time:
- Time to look for the block in the upper level and access it.
MISS Penalty:
- Time to replace the block in the upper level;
- + Time to deliver this block to the processor.
Coding
Because there is no unlimited memory, we should care about it while programming. It's important to understand how the language or frameworks works in order to avoid:
- Remove the data so early from the memory generating MISS Penalties;
- Referencing data that is not in the memory anymore;
- Retain old data that is not used anymore. (some languages has the support of Garbage collectors)
Mainly when you have performance in mind, it's extremely important to take these points into consideration while coding. If the users that will use your program don't notice issues while using It (lagging, freezing, etc), that means you did a good job.