title: EhCache using too much Heap Memory to store values
body: <p>I have used EhCache to cache 2.5 Gb of data. I have 32 files with varying size but total data volume is 2.5 Gb. I ran my code using 12G Heap memory but entire data set couldn't be accommodate in 12G of data, it overflows to disk. Could you please tell me what's wrong in configuration or EhCache always takes that much memory. All the caches I put Integer as key and List as value (record of a delimited file)</p>
  
  <p>JVM Arguments used <code>%JAVA_HOME%\bin\java.exe  -server -d64 -Xms12G -Xmx12G -XX:+UseG1GC -XX:-OmitStackTraceInFastThrow</code></p>
  
  <p>Below are the code snippet I have used for Cache</p>
  
  <pre><code>//Spring Configuration
  @Bean(destroyMethod = "shutdown",name="batchCache")
  public net.sf.ehcache.CacheManager ehCacheManager() {
      DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration();
      diskStoreConfiguration.setPath("C:\\DiskCache");
  
      net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
      config.setName("NAV_BATCH");
      config.addDiskStore(diskStoreConfiguration);
      //config.setMaxBytesLocalHeap(MemoryUnit.GIGABYTES.toBytes(10));
      net.sf.ehcache.CacheManager mgr = net.sf.ehcache.CacheManager.newInstance(config);
      mgr.clearAll();
      return mgr;
  }
  
  //Code to Obtain Cache manager
  private CacheManager cacheManager = (CacheManager) ApplicationContextProvider.getApplicationContext().getBean("batchCache");
  
  //Class level copy of Cache
  private Cache cache;
  
  protected Cache getCache(){
      if(null == cache){
          Cache managerCache = cacheManager.getCache(cacheName);
          if(null == managerCache){
              cache = createCache();
          }else{
              cache = managerCache;
          }
      }
      return cache;
  }
  
  //Key is Integer and Values is List of String always
  protected void putListRecordsInCache(Object key,List&lt;Object&gt; values){
      Element element = new Element(key, values);
      getCache().put(element);
  }
  
  public List&lt;T&gt; getValues(Object key){
      Element e  = getCache().get(key);
      List&lt;T&gt; dataList = new LinkedList&lt;&gt;();
      if(null == e){
          return dataList;
      }
  
      List&lt;String&gt; lines = (List&lt;String&gt;) e.getObjectValue();
  
      for(String line:lines){
          T t = getMapper().convertValuesToObject(line, null);
          dataList.add(t);
      }
  
      return dataList;
  }
  
  private Cache createCache(){
      Cache managerCache = cacheManager.getCache(cacheName);
      if(null == managerCache){
          managerCache = new Cache(cacheConfig(cacheName) );
          //managerCache = (Cache) cacheManager.addCacheIfAbsent(managerCache);
          cacheManager.addCache(managerCache);
      }
      return managerCache;
  }
  
  private CacheConfiguration cacheConfig(String name) {
      CacheConfiguration config = new CacheConfiguration();
      config.name(name)
  
      .memoryStoreEvictionPolicy("LRU")
      .eternal(true)
      .pinning(new PinningConfiguration().store(Store.LOCALMEMORY))
      .logging(false)
      .sizeOfPolicy(new SizeOfPolicyConfiguration().maxDepth(100000).maxDepthExceededBehavior("CONTINUE"))
      .persistence(new PersistenceConfiguration().strategy(Strategy.LOCALTEMPSWAP))
      .statistics(true);
      ;
  
      long size = -1;
      try {
          size = Files.size(FileSystems.getDefault().getPath(getDataFileLocation(), getFileName()));
          //System.out.println(new Date()+",Size of file "+getDataFileLocation()+"/"+getFileName()+" is "+size+" bytes");
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      if(size &gt; 0){
          long cachesize = size*4;
          config.maxBytesLocalHeap(cachesize, MemoryUnit.BYTES);
      }else{
  
          if(isValueGloballyCached){
              config.maxBytesLocalHeap(100, MemoryUnit.MEGABYTES);
          }else{
              config.maxBytesLocalHeap(500, MemoryUnit.MEGABYTES);
          }
      }
  
  
      return config;
  }
  </code></pre>
  
  <blockquote>
    <p>Below are the Memory and CPU Profile screenshot</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/s7130.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/s7130.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/D5oGl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D5oGl.png" alt="enter image description here"></a></p>