我们正在评估我们项目中 EHCache 的使用。我们使用简单的 hashMap 进行了测试,因为大小可能会超出堆大小,我们希望确保我们可以控制它......因此是 EHCache。但我无法理解这一点..
如果我将 500,000 个条目放入 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;
}
}
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 相同!