The class
WeakLruCache says that it is a Least Recently used cache, but it is not recently used cache. Infact its some random well optimized cache that depends on the behavior of GC.
And follows no pattern on which object is removed (under need for memory) to make place for a new one.
The implementation depends on ReferenceQueue, which is polled to see if at all it contains elements that are no longer strongly reachable. And this has nothing to do with Lru.
I ran a sample test below:
public class CacheTest {
public static void main(String... args) {
CacheTest test = new CacheTest();
test.go();
}
WeakLruCache<EntityInteger> cache;
private void go() {
cache = new WeakLruCache<EntityInteger>("Weak Cache");
test();
}
private void test()
{
for(int i =0;i<Integer.MAX_VALUE;i++)
{
cache.put(new EntityInteger(i));
}
}
}
class EntityInteger implements EntityWithSize {
private long id;
private int size;
public EntityInteger(long id) {
this.id = id;
}
public long getId() {
return id;
}
public void setRegisteredSize(int size) {
this.size = size;
}
public int getRegisteredSize() {
return size;
}
public int size() {
return size;
}
@Override
public String toString() {
return id + "";
}
}
And the output was://I put a println statement inside pollClearedValues() method of WeakLruCache.java
8983
3028
73841
60766
70691
11692
78490
94369
89437
68891
7963
50444
62778
15800
24992
2413
50961
61873
33931
95545
22094
2103
13650
.... with no fixed pattern
Am I gettign ti wrong or does lru mean somethign else here?
Best Regards,
Jatin Puri