Skip to content

Entries are not cleaned when using weakValues #111

@tepitebson

Description

@tepitebson

We faced a memory leak after upgrading caffeine from 2.2.7 to 2.3.1. It seems that cache entries are not evicted when using weak values. Following test is working on 2.2.7 but eats all memory with 2.3.1:

@Test
public void testWeakValuesEviction() throws InterruptedException {

    Cache<Long, Object> cache = Caffeine.newBuilder().executor(e -> e.run()).weakValues().recordStats().build();
    ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
    AtomicLong versionCounter = new AtomicLong(1L);
    exec.scheduleAtFixedRate(() -> {
        int idCounter = 1;
        while (idCounter++ < 200_000) {
            cache.put(versionCounter.getAndIncrement(), new Object());
        }

    } , 0L, 500L, TimeUnit.MILLISECONDS);
    for (;;) {
        Thread.sleep(10000);
        CacheStats stats = cache.stats();
        System.out.println(stats.toString());
        // cache.cleanUp();
        System.gc();

    }
}

Eviction count stays at zero on 2.3.1 while growing fast on 2.2.7. If cache is cleaned up explicitly from outside, entries are evicted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions