Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Whenever you open any application on your device, whether laptop or smartphone. You must have noticed that some of them open real quick, and some always open from the start, and the ones that open very quickly are the ones that are regularly used. And the less frequently used one takes much time to open. So what is the reason for that?
The explanation for the above phenomenon is the use of cache memory. In this, we store the data of some frequently used applications. We will learn about cache memory in detail further in the blog. So without wasting any time, let's get on with our topic.
Faster Access: Cache memory is faster than main memory, providing quicker access to frequently used data.
Limited Capacity: Cache memory has limited capacity compared to main memory, typically ranging from a few kilobytes to a few megabytes.
Hierarchical Structure: Cache memory often operates in a hierarchical structure, with multiple levels such as L1, L2, and sometimes L3 caches, each with varying speeds and sizes.
Costlier: Cache memory is more expensive than main memory per unit of storage, due to its faster access speeds and specialized design.
Volatility: Like main memory, cache memory is volatile and loses its contents when the power is turned off.
Levels of Memory
There are mainly four levels of memory that we are going to discuss now.
Register or Level 1: The type of memory in which data is immediately accepted and stored in the CPU. The most commonly used registers are Program Counter, accumulator, address register, etc.
Cache Memory or Level 2: It has the fastest access time, and it is the quickest memory where we need to access the temporarily stored data.
Main Memory or Level 3: It is also known as dynamic memory because it will be lost as soon as we switch off the device. It is the memory on which the computer is currently running.
Secondary memory or Level 4: It is external memory used for storage. It is not as fast as the main memory, but it always stays in the memory.
Types of Cache
There are mainly two types of Cache, and we are going to discuss each of them below.
Primary Cache: It is the type of Cache always located on the processor chip. Its access time is significantly less than the processor register and is very small in size.
Secondary Cache: It is placed between the rest of the memory and the cache memory. It is also known as Level 2 cache, and it is also placed on the processor.
There are three types of mapping that exist in Cache, and we will discuss them.
Direct Mapping
Associative Mapping
Set-Associative Mapping
1. Direct Mapping
As the name suggests, it directly maps each block of main memory into the suitable cache line. It is also the simplest of all mapping techniques. In this, we link one main memory block to the cache line, and if another main memory block already acquires that cache line, it will search for the new one. The address part is split into two parts tag field or index field. Only the tag field is stored in a cache line, and the rest is stored in the main memory. Its performance is directly proportional to the Hit Ratio.
i = j modulo m
where
i=cache line number
m=number of lines in the Cache
j= main memory block number
In this type of mapping, we use associative memory to store the address and content of the memory word. We can store any block in any line of Cache. Word id bits identify what bits are needed, and the tag becomes all the remaining bits. Because of this, any word can be placed anywhere in the cache memory. It is the most flexible and fastest mapping technique.
This is the upgraded form of direct mapping as it removes the drawbacks of direct mapping. It mainly targets the problem of thrashing, i.e., if the main memory block wants to point to some location of cache memory, but some other main memory block is already pointing to that. It solves this problem by following the method that instead of pointing one memory block line to one cache memory line, we make a set of some lines, and through it, we can point to the required cache line. It allow each word in the set can have more than one main memory location.
m = v * k
i= j mod v
where
i=cache set number
m=number of lines in the cache number of sets
j=main memory block number
v=number of sets
k=number of lines in each set
There are mainly two localities of reference, and here we are going to discuss both of them.
Temporal Locality of Reference:
Spatial Locality of Reference:
1. Temporal Locality of Reference: This LRU( Least Recent Algorithm) will be used. Whenever a web page fault happens inside a phrase, it will no longer best load phrase in the primary reminiscence. However, the entire web page fault might be loaded because the spatial locality of reference rule says that if you are referring to any phrase, the subsequent phrase might be referred to in its check-in, that’s why we load the entire web page desk so the entire block might be loaded.
2. Spatial Locality of Reference: In this, there is a chance that an element can be present in the close proximity of the reference point and will become closer and closer when searched again.
CPU Caches: Cache memory is commonly used as CPU caches, such as L1, L2, and L3 caches, to store frequently accessed instructions and data.
Web Caching: In web servers, cache memory is utilized to store frequently requested web pages and resources, reducing server load and improving response times.
Database Caching: Database systems often employ cache memory to store frequently accessed data and query results, enhancing query performance and reducing database server load.
Disk Caching: Operating systems use cache memory to temporarily store data read from and written to disk drives, reducing disk access times and improving overall system performance.
Advantages of Cache Memory
Faster Access: Cache memory provides quicker access to frequently used data compared to main memory, improving overall system performance.
Reduced Latency: By storing frequently accessed data closer to the processor, cache memory reduces memory access latency, enhancing system responsiveness.
Improved Efficiency: Cache memory reduces the need for frequent access to slower main memory, optimizing system resources and reducing memory bottlenecks.
Enhanced Throughput: Cache memory increases system throughput by minimizing the time spent waiting for data to be fetched from slower main memory.
Disadvantages of Cache Memory
Limited Capacity: Cache memory has limited capacity compared to main memory, resulting in the potential for cache thrashing and reduced effectiveness for large datasets.
High Cost per Byte: Cache memory is more expensive per unit of storage compared to main memory, making it economically impractical to have large cache sizes.
Complex Management: Cache memory requires complex management algorithms to ensure that the most relevant data is stored in the cache, which can introduce overhead and complexity.
Coherency Issues: In multiprocessor systems, maintaining cache coherency between multiple caches can be challenging and may lead to synchronization overhead and performance degradation.
Frequently Asked Questions
What is the principle of cache memory?
The principle of cache memory is based on the idea of storing frequently accessed data closer to the CPU for faster access. It exploits the locality of reference principle, where recently accessed data is likely to be accessed again in the near future.
What is an example of a cache memory?
An example of cache memory is the CPU cache, which stores frequently used instructions and data to reduce the latency of memory access. Another example is web browser cache, which stores recently accessed web pages and resources for faster loading upon subsequent visits.
What is the main purpose of cache memory?
The main purpose of cache memory is to improve the overall performance of a computer system by reducing the time taken to access frequently used data. By storing copies of frequently accessed data closer to the CPU, cache memory minimizes the latency associated with fetching data from slower main memory.
What are the two functions of cache memory?
The two primary functions of cache memory are:
Storing Frequently Accessed Data: Cache memory stores copies of frequently accessed instructions and data from main memory.
Providing Faster Access: By storing frequently accessed data closer to the CPU, cache memory reduces memory access latency and improves overall system performance.
What is cache memory and its features?
Cache memory is a small, high-speed memory unit located between the CPU and main memory. Its primary features include faster access times compared to main memory, limited capacity, hierarchical structure with multiple levels (e.g., L1, L2, L3 caches), and volatility, as it loses its contents when power is turned off.
Conclusion
In this blog, we learned about Cache memory in computer architecture. Cache memory plays a crucial role in computer architecture by bridging the gap between high-speed CPU and slower main memory. Its principle of storing frequently accessed data closer to the processor significantly improves system performance.
If you want to learn more about caching, you must refer to thisblog. Here you will get a different perspective about server-side and client-side caching and much more.