The Magic of LRU Cache (100 Days of Google Dev)

preview_player
Показать описание
Here’s a common problem that I’m sure you’ve run into: It’s time to load a new bitmap for your apps’ social media stream, or whatever, but you're out of memory. You can’t load in the new bitmap, without first destroying one of the existing bitmaps already in memory. But, which one should you get rid of?

Well, as Colt McAnlis, points out, you could learn 60 years of history of Cache replacement algorithms; that’ll help.. or, you could just use Android’s LRUCache container.

This container keeps a list of objects, and ranks them based upon how many times they’ve been accessed. When it’s time to evict one of them (to make space for a new object) the LRUCache already knows which ones to get rid of. All done without you having to worry about any of it.

100 Days of Google Dev / 100 developer videos over 100 days / #GoogleDev100

Рекомендации по теме
Комментарии
Автор

LRU is a good replacement policy only when the expectation is that items evicted from the cache will not be reused.

When your cache must serve as a backing for slower memory (most cases) and the expectation is the cache items will be reused shortly, the best eviction policy is LRU-MRU or LRU followed by Most-Recently-Used.

This was commonly discovered in the early PC GPU days (1990s) when the texture cache in GPU-memory was used to render real-time graphics like in Quake and the graphics engine would essentially thrash through texture memory if naive LRU replacement was used for the GPU texture cache.

YeshuasBro
Автор

I just want to point a little mistake in this video in 03:14 the ThumbnailCache class should extend "LruCache<String, Bitmap>" not "LruCache(String, Bitmap)" ^^

anis.lounis
Автор

At 1:10 in the example shouldn't the used image just move to the front and the others move a position back, instead of swapping the new front with the old front?
So, at begging it was: 4, 3, 2, 1
In the end of the video segment: 4, 1, 2, 3
But it should be: 4, 2, 1, 3

EduardoSoares
Автор

Colt McAnlis i am a big fan of yours this one was awesome make more such great content Hope i would meet you one day ;)

prathamkesarkar
Автор

you should probably use and not getByteCount(). otherwise, if anywhere you reused bitmaps, it will return the size used instead of the actual size taken.

ItayBia
Автор

Nice video. Didn't knew this gem was already in Android!

TahmidSadik
Автор

I think what he is talking about is Least Freaquently Used algo and not Least Recently Used algo.

rizanamatya
Автор

I have been asked to code one in an interview.

bkzlab
Автор

According to getMemoryClass(): "This gives you an idea of how hard a memory limit you should impose on your application to let the overall system work best" - could you explain dependency between mem limit and "work best"?

andreyfom
Автор

Great !! Nice and precise explanation !!

RakhiDhavale
Автор

This guy needs to be an actor. Hollywood! Hire this man! :P

VictorZamanian
Автор

How do I make cache persistent? I want to cache api calls for limited time like 6hr

AlimNaizabek
Автор

Every thing 's good, except your voice speed.

thangletruongquoc
Автор

why multiply getmemoryclass() by 1024 * 1024, then divide it by 8 later?

pyrolowryder
Автор

Is data will be stored in LRU Cache after i close the App. anyone, please

abhishektripathi