Computer Memory
Computer memory refers to the hardware devices used for storing data and programs within a computer.
In a computer system, the memory hierarchy is typically arranged from fastest to slowest. Memory closer to the CPU offers faster speeds but smaller capacities, while memory closer to external interfaces operates slower but provides larger capacities. By effectively utilizing the memory hierarchy, the performance and efficiency of the computer system can be enhanced.
Memory Hierarchy
- CPU internal general-purpose registers
- Cache
- Main memory
- Online disk storage
- Offline optical disks and disk storage
Classification of Memory
Classification based on Location
Based on location, memory is divided into main memory (internal) and auxiliary memory (external).
- Main Memory: Also known as primary memory or Random Access Memory (RAM), this is the temporary storage used in computers for running programs and data. Main memory offers extremely fast read/write speeds but has relatively small capacity compared to external storage. Furthermore, all data stored in main memory is lost when the computer is powered off or shut down. Main memory can be categorized into two types: Static RAM (SRAM) and Dynamic RAM (DRAM).
- Auxiliary Memory: This serves as long-term storage in computers for user-created data and programs, as well as operating system and other system files. The capacity of auxiliary memory is typically much larger than that of main memory, but its read/write speed is relatively slower. Auxiliary memory includes various types such as disk storage, optical disk storage, and flash memory. Data stored in auxiliary memory can be preserved for long periods and remains unchanged even after the computer is powered off or shut down.
Classification based on Constituent Materials
Based on constituent materials, memory is classified into magnetic memory, semiconductor memory, and optical memory.
- Magnetic Memory: This type of memory uses a magnetic medium to store data. Its basic principle involves recording magnetic fields on the magnetic medium to represent binary values, which are then read by a head and converted back into binary values. Magnetic memory includes hard drives, floppy disks, and tape drives. Compared to other types of memory, magnetic memory offers advantages such as high storage density, low cost, erasability, and rewritability. However, it also suffers from disadvantages like slower read/write speeds, susceptibility to magnetic interference, and a shorter lifespan.
- Semiconductor Memory: This is a type of memory constructed using semiconductor materials, including DRAM, SRAM, and flash memory. Compared to magnetic memory, semiconductor memory offers advantages such as fast read/write speeds, strong anti-interference capabilities, and a longer lifespan. However, it also has disadvantages such as lower storage density, higher cost, and susceptibility to electromagnetic wave interference.
- Optical Memory: This is a type of memory that uses optical media to store data. Its basic principle involves recording and reading information on an optical medium using lasers. Optical memory includes CD, DVD, and Blu-ray discs. Compared to other types of memory, optical memory offers advantages such as high storage density, large capacity, erasability (for rewritable discs), and rewritability. However, it also suffers from disadvantages like slower read/write speeds and susceptibility to scratches or contamination on the disc. In recent years, with technological advancements, the capacity and speed of optical memory have continuously improved, offering significant advantages in certain fields such as long-term storage and data backup.
High-Speed Cache
Address Mapping Methods
High-speed cache is a type of memory located between the CPU and main memory, designed to improve the speed at which the CPU accesses main memory. Due to the limited capacity of high-speed cache, an effective method is required to determine which data should be stored in it. This necessitates the use of address mapping methods.
Address mapping is a technique that maps main memory addresses to cache addresses. When the CPU accesses main memory, the cache maps this address to a corresponding cache line. If the data corresponding to this address is already cached, the cache returns the data to the CPU; otherwise, it reads the data from main memory and stores it in the corresponding cache line.
Common address mapping methods include the following three:
- Direct Mapping: This is a simple address mapping method that maps a main memory address directly to a unique location in the cache. For example, if the cache has 8 lines, then main memory address
00000FFFcan be mapped to cache line 0,10001FFFto cache line 1, and so on. This method is easy to implement but may lead to conflicts among cache lines. - Fully Associative Mapping: This is a flexible address mapping method that allows any main memory address to map to any cache line. This method requires maintaining a tag table in the cache to record the main memory addresses stored in each cache line. Fully associative mapping effectively avoids conflicts but requires more hardware resources to implement.
- Set Associative Mapping: This is a method that lies between direct mapping and fully associative mapping. It divides the cache into multiple sets, where each set contains multiple cache lines. For example, if the cache has 8 sets with 2 cache lines per set, then the two lines within each set can map to any main memory address. This method can avoid conflicts to a certain extent while also reducing the size of the tag table.
Replacement Policies
Replacement policies in high-speed cache refer to the process of replacing some cache lines to make space for new data when the cache is full. Common replacement policies include the following:
- Random Replacement: This is a simple replacement policy that randomly selects a cache line for replacement. This method is easy to implement but may result in stored data in the cache not being utilized effectively.
- First-In-First-Out (FIFO) Replacement: This is a replacement policy based on time order, meaning the data that entered the cache earliest is replaced first. This method ensures that the cache stores the most recent data but may lead to frequent replacement of commonly used data.
- Least Recently Used (LRU) Replacement: LRU is a replacement policy based on data usage frequency, meaning the data least recently used is replaced first. This method ensures that the cache stores the most frequently used data but requires additional hardware support to record the last use time of each cache line.
- Least Frequently Used (LFU) Replacement: LFU is a replacement policy based on data usage frequency, meaning the data least frequently used is replaced first. This method requires recording the number of times each data has been accessed, thus necessitating additional hardware support.
The above are common high-speed cache replacement policies. Different policies have varying pros and cons in practical applications, and the appropriate one should be selected based on specific application scenarios.
Virtual Memory
Virtual memory is an important concept in computer organization theory, referring to a technology that combines main memory and auxiliary memory for use. Virtual memory can store infrequently used data and programs in auxiliary memory, thereby freeing up space in main memory and improving the system's memory utilization rate.
Implementing virtual memory requires support from the operating system and typically includes the following components:
- Page: A page is the smallest unit of storage in virtual memory, typically 4KB or 8KB. The operating system divides main memory into pages of equal size, allowing different programs and data to share the same portion of main memory.
- Page Table: A page table is a table that maps main memory pages to auxiliary memory pages. The operating system maintains a page table in memory, recording the auxiliary memory address corresponding to each main memory page.
- Page Replacement: Since the primary purpose of virtual memory is to expand the capacity of main memory, when main memory is insufficient, the operating system moves infrequently used pages from main memory to auxiliary memory and brings required pages from auxiliary memory into main memory. Common page replacement algorithms include Least Recently Used (LRU) and First-In-First-Out (FIFO).
- Page Fault: When a program accesses a page not present in main memory, the operating system generates a page fault interrupt, notifying the program that it needs to load that page from auxiliary memory into main memory. At this point, the operating system maps the page to a free page in main memory according to the page table and loads the page from auxiliary memory into main memory.
Virtual memory technology allows programs to run with more space than the actual physical memory available, thereby improving system efficiency. The implementation of virtual memory requires support from the operating system; therefore, different operating systems may have different implementation methods and optimization strategies.
Virtual Memory Management Methods
Common virtual memory management methods include the following:
- Paging: Main memory and auxiliary memory are divided into pages of fixed size, with each page being equal in size. During execution, programs convert virtual addresses to physical addresses. The operating system implements this mapping through the page table. When a program accesses a page not present in main memory, the operating system generates a page fault interrupt, notifying the program to load that page from auxiliary memory into main memory.
- Segmentation: Programs are divided into several logical segments, where each segment represents an independent part of the program, such as a code segment or data segment. Each segment can have a different size. During execution, programs convert virtual addresses to physical addresses. The operating system implements this mapping through the segment table. When a program needs to access a segment not present in main memory, the operating system loads the entire segment from auxiliary memory into main memory.
- Segmentation with Paging: Programs are divided into several logical segments, and each segment is further divided into pages of fixed size. During execution, programs first convert virtual addresses to offsets within the segment, and then convert the segment number to a physical address. The operating system implements this mapping through both the segment table and the page table. When a program needs to access a page not present in main memory, the operating system loads the entire page from auxiliary memory into main memory.
- Segmentation: Programs are divided into several logical segments, where each segment represents an independent part of the program, such as a code segment or data segment. Each segment can have a different size. During execution, programs convert virtual addresses to physical addresses. The operating system implements this mapping through the segment table. When a program needs to access a segment not present in main memory, the operating system loads the entire segment from auxiliary memory into main memory.
- Global Management: The entire program is managed as a whole without considering its logical structure. During execution, programs convert virtual addresses to physical addresses. The operating system implements this mapping through a global table. When a program needs to access a page not present in main memory, the operating system loads the entire program from auxiliary memory into main memory.
Each of these virtual memory management methods has its own advantages and disadvantages. Appropriate methods should be selected based on specific application scenarios and hardware resources to improve the system's memory utilization rate and operational efficiency.