2

プロジェクトで 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 と同じ量のメモリを使用しています。

4

1 に答える 1

1

Ehcache は、生成されるガベージの量を減らすことはありません。実際には、より多くの作業を行っているため、より多くの量を生成する可能性があります (特に Java シリアル化を使用する場合)。 Full GC の後でしか見ることができない (例System.gc())

于 2012-07-09T08:10:12.827 に答える