0

Just wondering if the system pre-allocate any space for empty guava cache? I am new to guava cache, can someone help me to understand this problem?

Also I test it with code below:

    long preMemUsage = Runtime.getRuntime().totalMemory() -Runtime.getRuntime().freeMemory();
    Cache<String, CountObject> cache = CacheBuilder.newBuilder()
        .maximumSize(12000)
        .expireAfterAccess(7200,TimeUnit.SECONDS)
        .concurrencyLevel(10)
        .recordStats().build();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    long postMemUsage = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
    System.out.println("Before: " + preMemUsage);
    System.out.println("After: " + postMemUsage);

The weird thing is the used memory is decreased after..... no idea why. Can someone explain this.

Thanks a lot : )

Nathan
  • 95
  • 1
  • 2
  • 10
  • Mysterious are the ways of garbage collector. It's pretty much possible and you should call garbage collector explicitly before trying to calculate used memory. `System.gc()` – JiriS Nov 29 '16 at 21:34

1 Answers1

1

Mysterious are the ways of garbage collector. It's pretty much possible and you should call garbage collector explicitly before trying to calculate used memory. Use System.gc() right before calculating preMemUsage and postMemUsage and you should get much more accurate results.

JiriS
  • 6,870
  • 5
  • 31
  • 40