Java EHCache消耗的内存与HashMap相同

Java EHCache消耗的内存与HashMap相同,java,ehcache,heap-memory,Java,Ehcache,Heap Memory,我们正在评估EHCache在我们项目中的使用情况。我们使用一个简单的hashMap进行测试,由于大小可能超过堆大小,我们希望确保能够控制它。。因此,EHCache。但我无法理解这一点 如果我将500000个条目放入HashMap,则消耗的内存约为114MB。如果我使用EHCache并将堆中的条目数限制为10,将本地磁盘限制为500000,则会消耗98MB。我看不出有多大区别。我想我应该只能看到少量的堆,因为堆中只有10个条目。下面是我正在运行的程序 HashMap程序 import java.u

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

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

HashMap程序

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;
            }

        }
import java.util.HashMap;
导入java.util.Map;
公开课考试{
公共静态void main(字符串[]args){
System.out.println((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freemory())/1024/1024);
NastIDAccountID accountIDNastID=新的NastIDAccountID();

对于(int i=0;iEhcache)来说,它不会减少产生的垃圾量,事实上,因为它做了更多的工作,它可以产生更多的垃圾(特别是如果使用Java序列化),它为您做的是减少保留的垃圾量,这是只有在完全GC之后才能看到的(例如
System.GC()

<?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";
        }
    }
}