Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating cache memory based on LRU algorithm

Tags:

lru

paging

Assuming i have 4 blocks of cache memory, Using the LRU (Least Recently Used) replacement algorithm on this following sequence of access to memory blocks: 1 2 3 4 5 2 5 4 1 5 2 3 :

1   2   3   4   5   2   5   4   1   5   2   3

1   1   1   1   5   5   5   5   5   5   5   5
    2   2   2   2   2   2   2   2   2   2   2
        3   3   3   3   3   3   1   1   1   1
            4   4   4   4   4   4   4   4   3

So in the end, the cache memory will contains this memory blocks : "5 2 1 3"

But the correct result is "1 5 2 3"

Please tell me what am I doing wrong here!

Edit:

I will be honest, I'm doing an excercise and can't get help from anywhere but here, and may be I misread the question, so this is the original question :

enter image description here

like image 489
f855a864 Avatar asked Jun 23 '26 13:06

f855a864


2 Answers

In a straightforward cache, the order doesn't matter. And the LRU algorithm is simple enough that you don't need to run the whole simulation. Just look at the last 4 numbers in your sequence:

... 1 5 2 3
like image 153
MSalters Avatar answered Jun 25 '26 21:06

MSalters


So is my simulation of the LRU wrong? Honestly i still don't get it!

The simulation goes awry in column 5 where you add the newest data to the oldest position.

Here's the correction:

            1   2   3   3   3   2   2   4   1  <-- Oldest accesses
        1   2   3   4   4   2   5   4   1   5
    1   2   3   4   5   2   5   4   1   5   2
1   2   3   4   5   2   5   4   1   5   2   3  <-- Newest accesses
  • Notice that the newest row is always equal to your input sequence.
  • The other elements age one row at a time unless they are brought to the front with a new access
like image 23
Raymond Hettinger Avatar answered Jun 25 '26 19:06

Raymond Hettinger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!