Compare the Difference Between Similar Terms

Difference Between Cache Memory and Virtual Memory

Cache Memory vs Virtual Memory
 

The difference between cache memory and virtual memory exists in the purpose for which these two are used and in the physical existence. Cache memory is a type of memory used to improve the access time of main memory. It resides between the CPU and the main memory, and there can be several levels of caches such as L1, L2 and L3. The type of hardware used for cache memory is much costlier than the RAM (Random Access Memory) used for main memory because cache memory is much faster. For this reason, the capacity of cache memory is very small. Virtual memory is a memory management technique used to efficiently use RAM (main memory) while providing a separate memory space for each program that is even larger than the actual physical RAM (main memory) capacity. Here the hard disk is used to expand the memory. The items in the physical RAM are transferred back and forth with the hard disk.

What is Cache Memory?

Cache memory is a type of memory that lies between the CPU (Central Processing Unit) and the RAM (Random Access memory). The purpose of cache memory is to reduce the memory access time of the CPU from the RAM. The cache memory is much faster than RAM. So access time on cache is much lesser than the access time on RAM. But the cost of memory used for cache memory is much higher than the cost of memory used for RAM, and hence, the capacity of cache memory is very small. The type of memory used for cache memory is called SRAM ( Static Random Access Memory).

Whenever the CPU wants to access memory, it first checks whether what it needs resides in the cache memory. If yes, it would be able to access it with the least latency. If it does not reside in cache, then the requested content would be copied from RAM to the cache and then only the CPU will access it from the cache. Here, when copying content from the cache, not only the content in the requested memory address but also the nearby content is copied to cache. So, the next time there is a high probability for a cache hit to occur as most computer programs access nearby data or last accessed data most of the times. So due to the cache, the average memory latency is reduced.

In CPU, there are three types of caches: Instruction cache to store program instructions, Data cache to store data items, and the Translation Look-aside Buffer to store memory mappings. For data cache, generally, there are multi-level caches. That is, there are several caches as L1, L2 and L3. L1 cache is the fastest but smallest cache memory that is closest to the CPU. L2 cache is slower than L1, but larger than L1 and resides after the L1 cache. Because of this hierarchy a better average memory access time can be achieved at a less cost.

What is Virtual Memory?

Virtual memory is a memory management technique used in computers systems. There is no hardware called virtual memory, but it is a concept that uses RAM and the hard disk to provide a virtual address space for programs. First RAM is divided into chunks called pages and they are identified by physical memory addresses. In the hard disk, a special portion is reserved where, in Linux, it is called the swap and, in Windows, it is called a page file. When a program is started, it is given a virtual address space that can be even larger than the actual physical memory. Virtual memory space is also divided into chunks called pages and each of this virtual memory page can be mapped to a physical page. The table called page table keep track of this mapping. When the physical memory runs out of space, what is done is, certain physical pages are pushed to that special portion in the hard disk. When any page pushed to the hard disk is needed again, it is brought to the physical memory by putting another selected page from the physical memory to the hard disk.

What is the difference between Cache Memory and Virtual Memory?

• Cache memory is a type of memory used for improving the main memory access time. It is a faster type of memory that resides between CPU and RAM to reduce the average memory access latency. Virtual memory is a memory management method where it is a concept that lets programs get its own virtual memory space, which is even larger than the real physical RAM available.

• Cache memory is a type of hardware memory that actually exists physically. On the other hand, there is no hardware called virtual memory as it is a concept that uses RAM, hard disk, Memory management unit, and software to provide a virtual type of memory.

• Cache memory management is done fully by hardware. Virtual memory is managed by the operating system (software).

• Cache memory lies between RAM and the processor. Data transfers involve RAM, cache memory, and the processor. Virtual memory, on the other hand, involves transfer of data between RAM and hard disk.

• Cache memories take small sizes such as Kilobytes and Megabytes. Virtual memory, on the other hand, involves huge sizes that take gigabytes.

• Virtual memory involves data structures such as page tables that store mapping between physical memory and virtual memory. But this type of data structures is not necessary for cache memory.

Summary:

Cache Memory vs Virtual Memory

Cache memory is used for improving the main memory access time while virtual memory is a memory management method. Cache memory is an actual hardware, but there is no hardware called virtual memory. RAM, hard disk, and various other hardware together with the operating system produces the concept called virtual memory to provide large and isolated virtual memory spaces to each program. The content in the cache memory is managed by hardware while the content in the virtual memory is managed by the operating system.

 

Images Courtesy:

  1. Cache memory process by Hellisp (CC BY-SA 3.0)
  2. Virtual memory by Ehamberg (CC BY-SA 3.0)