Example: Declarative Configuration

Note: The following description focuses on code snippets from the full code example, which is available at the /code-samples/src/ location in the installed product kit.

You can configure BigMemory Max declaratively, using an XML configuration file, or programmatically via the fluent configuration API. This sample shows how to configure a basic instance of BigMemory Max declaratively with the XML configuration file.

To configure BigMemory Max declaratively with an XML file, create a CacheManager instance, passing the a file name or an URL object to the constructor.

The following example shows how to create a CacheManager with an URL of the XML file in the classpath at /xml/ehcache.xml.

CacheManager manager = CacheManager.newInstance( 
                          getClass().getResource("/xml/ehcache.xml")); 
try { 
  Cache bigMemory = manager.getCache("BigMemory"); 
  // now do stuff with it... 
} finally { 
  if (manager != null) manager.shutdown(); 
}

Here are the contents of the XML configuration file used by this sample:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" 
         name="config"> 
  <cache name="BigMemory" 
         maxBytesLocalHeap="512M" 
         maxBytesLocalOffHeap="32G" 
         copyOnRead="true" 
         statistics="true" 
         eternal="true"> 
  </cache> 
</ehcache>

The configuration element maxBytesLocalOffHeap lets you set how much off-heap memory to use. BigMemory Max’s unique off-heap memory storage lets you use all of the memory available on a server in a single JVM—from gigabytes to multiple terabytes—without causing garbage collection pauses.