EHCache消耗的内存与HashMap相同

时间:2012-07-09 06:40:01

标签: java ehcache heap-memory

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

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

HashMap Program ..

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计划:

Cache.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相同的内存量!

1 个答案:

答案 0 :(得分:1)

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