Memory
RAM, ROM, cache and virtual memory.
Do this lesson in the simulatorA processor needs somewhere to keep the program it is running and the data it is working on. That is primary memory: memory the CPU can reach directly and quickly. There are several kinds, each with a different job. This lesson sorts out RAM, ROM, cache and virtual memory, and builds a model of what happens when RAM runs out.
RAM
RAM, random access memory, holds the programs that are running and the data they are using right now: the operating system, your Python program, its variables, the latest camera frame.
- It can be read and written.
- It is volatile: everything in it is lost when the power goes off.
- More RAM means more programs and data can be open at once without slowing down.
When you turn BugBot off, the program in RAM is gone. That is why the program you upload is also saved to storage and loaded back into RAM when the robot starts.
ROM
ROM, read only memory, holds instructions that must be there the moment the power comes on, before anything else has loaded: the boot program (in a PC, the BIOS) that starts the computer and loads the operating system.
- It can be read but not normally changed.
- It is non-volatile: it keeps its contents without power.
- It is small, because it only holds the start-up instructions.
| RAM | ROM | |
|---|---|---|
| Volatile? | yes, cleared when power is off | no, keeps its contents |
| Can be changed? | read and written all the time | read only |
| Holds | running programs and their data | the boot program |
| Size | large (gigabytes) | small |
Cache
Cache was in lesson F9.5: a small amount of very fast memory in or right next to the CPU, holding copies of what the CPU is using most. The order from fastest and smallest to slowest and largest is:
registers → cache → RAM → secondary storage
Each step is bigger and cheaper per gigabyte, but slower. Computers use a little of the fast kinds and a lot of the slow ones.
Virtual memory
What happens when RAM is full and you open another program? The operating system moves some of what is in RAM, usually the parts not used for a while, out to secondary storage such as the SSD. That area of storage is virtual memory. The new program then fits in RAM.
If a program that was moved is needed again, it has to be moved back, and something else moved out. Storage is far slower than RAM, so if this happens a lot, the computer slows right down. Adding more RAM is the fix.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
ram_size = 8 # GB
in_ram = ["operating system", "browser"]
sizes = {"operating system": 2, "browser": 3, "game": 4, "music": 1}
def used():
return sum(sizes[name] for name in in_ram)
for app in ["game", "music"]:
print("opening", app, "- RAM used:", used(), "of", ram_size, "GB")
if used() + sizes[app] > ram_size:
print(" RAM full: move browser to virtual memory")
in_ram.remove("browser")
in_ram.append(app)
print("in RAM:", in_ram)
Task: out of RAM
A computer has 4 GB of RAM, and the operating system always uses 1 GB. The apps in apps are opened in order. For each one, while it does not fit, move the app that has been in RAM longest to virtual memory and print moved <name> to virtual memory. Then load it and print <name> -> RAM. At the end, print RAM used: <n> GB and in virtual memory: <names>, the names joined with ,.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
ram_size = 4
os_size = 1
apps = [("browser", 1.5), ("game", 2.0), ("music", 0.5), ("editor", 1.0)]
Challenges
- Change
ram_sizeto 8. How many moves to virtual memory are there now? - Reopen the browser at the end. What has to move out for it to come back?
- Why does a robot's boot program live in ROM rather than RAM?