CPU AND MEMORY BASICS

1. CPU and Memory Interaction

The relationship between the Central Processing Unit (CPU) and memory is fundamental to system performance.



  • Execution Cycle: Every instruction executed by the CPU requires access to memory.



  • Primary Memory (RAM): This holds the program instructions and data currently in use by the CPU.



  • Secondary Storage: Used for long-term data retention; data must be moved from secondary storage into primary memory before the CPU can execute it.



2. The Memory Hierarchy

Computer storage is organized into a pyramid structure based on speed, capacity, and cost.

computer memory hierarchy pyramid, AI generated

Shutterstock



  • Registers: Located inside the CPU; smallest capacity but the fastest access times (0.3ns–2ns).



  • Cache (L1, L2, L3): High-speed memory that sits between the CPU and main memory to speed up data access.



  • Main Memory (RAM): Slower than cache (30ns–70ns) but offers much larger capacity for running applications.



  • Online/Secondary Storage: Includes Solid-State Disks (SSD) and Fixed Rigid Disks (HDD).



  • Tertiary/Off-line Storage: Includes slower, high-capacity media like Optical Disks, Magnetic Tapes, and USB Flash Drives.



3. Understanding Cache Memory

Cache is designed to minimize the time the CPU spends waiting for data from the slower main memory.



  • Performance: Hit ratios (finding data in the cache) of 90% and above are common, which can improve execution speed by over 50%.


  • Caching Levels:


    • L1 Cache: Smallest and fastest, usually built directly into the CPU core.



    • L2/L3 Cache: Larger and slightly slower than L1, but still much faster than RAM.



4. The Principle of Locality

Caching is effective because of "Locality of Reference," which suggests that programs tend to reuse data and instructions they have used recently or that are stored nearby.



  • Temporal Locality: Recently accessed data elements are likely to be accessed again soon (e.g., variables in a loop).



  • Spatial Locality: If a data item is accessed, items stored at nearby memory addresses are likely to be accessed soon (e.g., elements in an array).



  • Sequential Locality: Instructions tend to be accessed in a linear sequence.



  • Data Blocks: Because of these principles, when a cache "miss" occurs, an entire block of data is copied into the cache rather than just a single byte.



5. Key Concepts in Cache Systems


  • Inclusive Cache: A design where data in the higher-level cache (L1) must also exist in the lower-level cache (L2).



  • Exclusive Cache: Data exists in only one level of the cache at a time to maximize total storage capacity.