EHCache消耗的内存与HashMap相同



我们正在评估EHCache在我们的项目中的使用。我们使用一个简单的hashMap进行了测试,由于大小可能会超过堆大小,我们希望确保我们可以控制它。因此,EHCache。但我无法理解这一点..

如果我将 500,000 个条目放入 HashMap 中,消耗的内存约为 114MB。如果我使用 EHCache 并将堆中的条目数限制为 10,将本地磁盘限制为 500000,则消耗 98MB。我看不出有很大的区别。我在想我应该只能看到使用的少量堆,因为堆中只有 10 个条目。这是我运行的程序。

哈希图程序..

import java.util.HashMap;
import java.util.Map;
public class Test {
    public static void main(String[] args) {
        System.out.println((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/1024/1024);
        NastIDAccountID accountIDNastID=new NastIDAccountID();
        for(int i=0;i<500000;i++){
            System.out.println(accountIDNastID.getFromCache(String.valueOf(i)));
        }
        System.out.println((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024);
    }
    public static class NastIDAccountID{
        private final Map<String,String> cache;
        public NastIDAccountID() {
            this.cache = new HashMap<String, String>();
        }
        public String getFromCache(String key){
            if(cache.containsKey(key)){
                return cache.get(key);
            }else{
                final String value = key + "abcdefghijklmnopqrstuvwxyz";
                cache.put(key, value);
                return value;
            }
        }

EHCache计划:

缓存.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="false" monitoring="autodetect"
         dynamicConfig="false">
    <diskStore path="/Users/temp/ehcachepersist"/>
    <cache name="nastIDMossoIDMappingCache"
           maxEntriesLocalHeap="10"
           maxEntriesLocalDisk="500000"
           eternal="true"
           overflowToDisk="true"
           diskPersistent="true"
           maxElementsOnDisk="1000000"
      />
</ehcache>

程序:

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.constructs.blocking.CacheEntryFactory;
import net.sf.ehcache.constructs.blocking.SelfPopulatingCache;

public class EHCacheTester {
    public static void main(String[] args) {
        System.out.println((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/1024/1024);
        final CacheManager cacheManager = CacheManager.create(EHCacheTester.class.getResource("ehcache.xml"));
        final Cache nastIDMossoIDMappingCache = cacheManager.getCache("nastIDMossoIDMappingCache");
        NastIDAccountID accountIDNastID=new NastIDAccountID(nastIDMossoIDMappingCache);
        for(int i=0;i<500000;i++){
            System.out.println(accountIDNastID.getFromCache(String.valueOf(i)));
        }
        System.out.println("nastIDMossoIDMappingCache.calculateInMemorySize() = " + nastIDMossoIDMappingCache.calculateInMemorySize());
        System.out.println("nastIDMossoIDMappingCache.calculateOnDiskSize() = " + nastIDMossoIDMappingCache.calculateOnDiskSize());
        System.out.println((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024);
        cacheManager.shutdown();
    }
    public static class NastIDAccountID{
        private final Ehcache cache;
        public NastIDAccountID(Ehcache cache) {
            this.cache = new SelfPopulatingCache(cache, new OurCacheEntryFactory());
        }
        public String getFromCache(String key){
            return (String)cache.get(key).getValue();
        }
    }
    public static class OurCacheEntryFactory implements CacheEntryFactory{
        private int counter;
        @Override
        public Object createEntry(Object o) throws Exception {
            counter++;
            System.out.println(counter);
            return o.toString()+ "abcdefghijklmnopqrstuvwxyz";
        }
    }
}

我已经打印了缓存大小。内存中的缓存大小仅为 2960 字节。但是 Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory() 报告的堆大小告诉我一些不同的东西。

底线是EhCahe使用的内存量与HashMap相同!

Ehcache 不会减少产生的垃圾量,事实上,因为它正在做更多的工作,它可以产生更多(特别是如果你使用 Java 序列化) 它为您做的是减少保留的数量,这只有在完整 GC 之后才能看到(例如 System.gc()

相关内容

最新更新