通过以下代码测试发现,二者所占内存差不多,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>

4001

被折叠的 条评论
为什么被折叠?



