org.neo4j.kernel.impl.cache.WeakLruCache is not truly Least Recently Used.

40 views
Skip to first unread message

Jatin

unread,
Aug 3, 2012, 3:21:00 AM8/3/12
to ne...@googlegroups.com
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

Johan Svensson

unread,
Aug 10, 2012, 8:39:21 AM8/10/12
to ne...@googlegroups.com
Hi Jatin,

You are right, eviction is controlled by GC (and it is not LRU).

We will change the name to WeakRefCache and update the documentation as needed.

Thanks,
Johan

Jatin Puri

unread,
Aug 10, 2012, 8:46:17 AM8/10/12
to ne...@googlegroups.com, jo...@neotechnology.com
Hello Johan,

Thanks for the reply. Just adding, it is so even for SoftLruCache also.

In the coming versions different cache implementation should be tried. Because this does the job in current case, but can be a lot better with some kind of internal buckets crudely implementing LRU if not completely. It should hugely impact.

Best Regards,
Jatin Puri
--
Jatin Puri
Senior Undergraduate,
Bits Goa
http://www.jatinpuri.com


Reply all
Reply to author
Forward
0 new messages