天天看點

HashMap和Ehcache占用記憶體比較

通過以下代碼測試發現,二者所占記憶體差不多,50萬條資料插入HashMap占用記憶體114MB,而使用Ehcache,并裝置存在記憶體資料大小為10,占用記憶體為98MB,以下是測試代碼。

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

        }
	}
}
           

2.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";
        }
    }
}
 
           
緩存配置檔案:
           
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>