2.3. Using Eviction
In Red Hat JBoss Data Grid, eviction is disabled by default. If an empty <
eviction
/> element is used to enable eviction without any strategy or maximum entries settings, the following default values are used:
- Strategy: If no eviction strategy is specified,
EvictionStrategy.NONE
is assumed as a default. - size: If no value is specified, the
size
value is set to-1
, which allows unlimited entries.
2.3.1. Initialize Eviction
To initialize eviction, set the eviction element's
size
attributes value to a number greater than zero. Adjust the value set for size
to discover the optimal value for your configuration. It is important to remember that if too large a value is set for size
, Red Hat JBoss Data Grid runs out of memory.
The following procedure outlines the steps to initialize eviction in JBoss Data Grid:
Procedure 2.1. Initialize Eviction
Add the Eviction Tag
Add the <eviction> tag to your project's <cache> tags as follows:<eviction />
Set the Eviction Strategy
Set thestrategy
value to set the eviction strategy employed. Possible values areLRU
,UNORDERED
andLIRS
(orNONE
if no eviction is required). The following is an example of this step:<eviction strategy="LRU" />
Set the Maximum Size to use for Eviction
Set the maximum number of entries allowed in memory by defining thesize
element. The default value is-1
for unlimited entries. The following demonstrates this step:<eviction strategy="LRU" size="200" />
Result
Eviction is configured for the target cache.
2.3.2. Eviction Configuration Examples
Eviction may be configured in Red Hat JBoss Data Grid programmatically or via the XML file. Eviction configuration is done on a per-cache basis.
A sample XML configuration for is as follows:
<eviction strategy="LRU" size="2000"/>
2.3.3. Utilizing Memory Based Eviction
Red Hat JBoss Data Grid 7 introduced memory based eviction, allowing eviction of entries based on memory usage of the entries instead of the number of entries. This can be particularly useful if the entries vary in size.
Key/Value Limitations
Only keys and values that are stored as primitives, primitive wrappers (such as java.lang.Integer
), java.lang.String
instances, or an Array
of these values may be used with memory based eviction.
Due to this limitation if custom classes are used then either
store-as-binary
must be enabled on the cache, or the data from the custom class may be serialized, storing it in a byte array.
Compatibility mode prevents serialization into byte arrays, and as such these two features are mutually exclusive.
Eviction Strategy Limitations
Memory based eviction is only supported with the LRU
eviction strategy.
Enabling Memory Based Eviction
This eviction method may be used by defining MEMORY
as the eviction type, as seen in the following example:
<local-cache name="local"> <eviction size="10000000000" strategy="LRU" type="MEMORY"/> </local-cache>
2.3.4. Eviction and Passivation
To ensure that a single copy of an entry remains, either in memory or in a cache store, use passivation in conjunction with eviction.
The primary reason to use passivation instead of a normal cache store is that updating entries require less resources when passivation is in use. This is because passivation does not require an update to the cache store.