【问题标题】:EHcache memory clarificationEHcache内存说明
【发布时间】:2022-12-14 08:30:08
【问题描述】:

我正在为我的项目做 poc。我打算使用 EH-cache。 我创建了下面的代码来检查不同层的分配/使用大小

爪哇:11

ehcache 版本:<ehcache3.version>3.7.0</ehcache3.version>

代码 :

public class BasicProgrammatic {
    private static final Logger LOGGER = getLogger(BasicProgrammatic.class);

    //https://www.ehcache.org/documentation/3.4/getting-started.html
    //https://www.ehcache.org/documentation/2.8/code-samples.html#creating-caches-programmatically
    public static void main(String[] args) {
        LOGGER.info("Creating cache manager programmatically");

        StatisticsService statisticsService = new DefaultStatisticsService();
        PersistentCacheManager persistentCacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                .using(statisticsService)
                .with(CacheManagerBuilder.persistence(new File("/tmp", "jktestcache")))
                .withCache("threeTieredCache",
                        CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
                                ResourcePoolsBuilder.newResourcePoolsBuilder()
                                        .heap(1, EntryUnit.ENTRIES)
                                        .offheap(2, MemoryUnit.MB)
                                        .disk(1, MemoryUnit.GB)
                        )
                ).build(true);

        Cache<String, String> threeTieredCache = persistentCacheManager.getCache("threeTieredCache", String.class,
                String.class);

        LOGGER.info("Putting to cache");
        threeTieredCache.put("on1", "da one!");
        threeTieredCache.put("on2", "da towo!");
        threeTieredCache.put("on3", "da 3!");
        threeTieredCache.put("on4", "da 4!");
        String value = threeTieredCache.get("on4");
        LOGGER.info("Retrieved '{}'", value);

        CacheStatistics ehCacheStat = statisticsService.getCacheStatistics("threeTieredCache");

        LOGGER.info("getOccupiedByteSize on-heap " + ehCacheStat.getTierStatistics().get("OnHeap").getOccupiedByteSize());
        LOGGER.info("getOccupiedByteSize off-heap " + ehCacheStat.getTierStatistics().get("OffHeap").getOccupiedByteSize());
        LOGGER.info("getAllocatedByteSize Disk  " + ehCacheStat.getTierStatistics().get("Disk").getAllocatedByteSize());
        LOGGER.info("getOccupiedByteSize Disk  " + ehCacheStat.getTierStatistics().get("Disk").getOccupiedByteSize());


        LOGGER.info("Closing cache manager");
        persistentCacheManager.close();

    }

}

结果 :

2022-11-15 15:22:55,875 [main] INFO org.ehcache.sample.BasicProgrammatic - Creating cache manager programmatically
2022-11-15 15:22:56,172 [main] INFO org.terracotta.offheapstore.paging.UpfrontAllocatingPageSource - Allocating 2MB in chunks
2022-11-15 15:22:56,231 [main] INFO org.ehcache.core.EhcacheManager - Cache 'threeTieredCache' created in EhcacheManager.
2022-11-15 15:22:56,254 [main] INFO org.ehcache.sample.BasicProgrammatic - Putting to cache
2022-11-15 15:22:56,272 [main] INFO org.ehcache.sample.BasicProgrammatic - Retrieved 'da 4!'
2022-11-15 15:22:56,272 [main] INFO org.ehcache.sample.BasicProgrammatic - getOccupiedByteSize on-heap -1
2022-11-15 15:22:56,272 [main] INFO org.ehcache.sample.BasicProgrammatic - getOccupiedByteSize off-heap 0
2022-11-15 15:22:56,273 [main] INFO org.ehcache.sample.BasicProgrammatic - getAllocatedByteSize Disk  16640
2022-11-15 15:22:56,273 [main] INFO org.ehcache.sample.BasicProgrammatic - getOccupiedByteSize Disk  320
2022-11-15 15:22:56,273 [main] INFO org.ehcache.sample.BasicProgrammatic - Closing cache manager
2022-11-15 15:22:56,322 [main] INFO org.ehcache.core.EhcacheManager - Cache 'threeTieredCache' removed from EhcacheManager.

问题 :

  1. 我为磁盘缓存分配了 1 GB,但日志仅显示16640.不管我给它什么,它只显示 16640

  2. 我有 4 个元素,所以一些数据应该已经移到堆外,因为我的堆大小是 1 个条目。如果是这种情况,为什么堆外大小为空

    3.为什么堆大小是-1

    还有其他要求吗?

    我试图查看从堆上、堆外和磁盘中的每一层获取元素所花费的时间,例如 [1ms vs 2ms vs 3ms]。这样我就可以计算权衡

    谢谢 jk

【问题讨论】:

    标签: caching ehcache ehcache-3


    【解决方案1】:

    我在堆上统计信息中看到了同样的问题。让我想知道是否甚至使用了堆上。你找到解决办法了吗?

    【讨论】:

      猜你喜欢
      • 2012-12-12
      • 2014-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-21
      • 2011-02-16
      • 2018-11-08
      • 1970-01-01
      相关资源
      最近更新 更多