此内容没有您所选择的语言版本。
Chapter 2. Eviction and Expiration
Eviction and expiration are strategies for preventing OutOfMemoryError exceptions in the Java heap space. In other words, eviction and expiration ensure that Red Hat JBoss Data Grid does not run out of memory.
2.1. Overview of Eviction and Expiration 复制链接链接已复制到粘贴板!
- Eviction
- Removes unused entries from memory after the number of entries in the cache reaches a maximum limit.
- The operation is local to a single cache instance. It removes entries from memory only.
- Executes each time an entry is added or updated in the cache.
- Expiration
- Removes entries from memory after a certain amount of time.
- The operation is cluster-wide. It removes entries from memory across all cache instances and also removes entries from the cache store.
-
Expiration operations are processed by threads that you can configure with the
ExpirationManagerinterface.
2.2. Configuring Eviction 复制链接链接已复制到粘贴板!
You configure Red Hat JBoss Data Grid to perform eviction with the <memory /> element in your cache configuration. Alternatively, you can use the MemoryConfigurationBuilder class to configure eviction programmatically.
2.2.1. Eviction Types 复制链接链接已复制到粘贴板!
Eviction types define the maximum limit for entries in the cache.
COUNT- Measures the number of entries in the cache. When the count exceeds the maximum, JBoss Data Grid evicts unused entries.
MEMORY- Measures the amount of memory that all entries in the cache take up. When the total amount of memory exceeds the maximum, JBoss Data Grid evicts unused entries.
2.2.2. Storage Types 复制链接链接已复制到粘贴板!
Storage types define how JBoss Data Grid stores entries in the cache.
| Storage Type | Description | Eviction Type | Policy |
|---|---|---|---|
|
| Stores entries as objects in the Java heap. This is the default storage type. |
| TinyLFU |
|
|
Stores entries as |
| TinyLFU |
|
|
Stores entries as |
| LRU |
The BINARY and OFF-HEAP storage types both violate object equality. This occurs because equality is determined by the equivalence of the resulting bytes[] that the storage types generate instead of the object instances.
Red Hat JBoss Data Grid includes the Caffeine caching library that implements a variation of the Least Frequently Used (LFU) cache replacement algorithm known as TinyLFU. For OFFHEAP JBoss Data Grid uses a custom implementation of the Least Recently Used (LRU) algorithm.
2.2.3. Adding the Memory Element 复制链接链接已复制到粘贴板!
The <memory> element controls how Red Hat JBoss Data Grid stores entries in memory.
For example, as a starting point to configure eviction for a standalone cache, add the <memory> element as follows:
<local-cache ...> <memory> </memory> </local-cache>
<local-cache ...>
<memory>
</memory>
</local-cache>
2.2.4. Specifying the Storage Type 复制链接链接已复制到粘贴板!
Define the storage type as a child element under <memory>, as follows:
OBJECT<memory> <object/> </memory>
<memory> <object/> </memory>Copy to Clipboard Copied! Toggle word wrap Toggle overflow BINARY<memory> <binary/> </memory>
<memory> <binary/> </memory>Copy to Clipboard Copied! Toggle word wrap Toggle overflow OFFHEAP<memory> <offheap/> </memory>
<memory> <offheap/> </memory>Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.2.5. Specifying the Eviction Type 复制链接链接已复制到粘贴板!
Include the eviction attribute with the value set to COUNT or MEMORY.
OBJECT<memory> <object/> </memory>
<memory> <object/> </memory>Copy to Clipboard Copied! Toggle word wrap Toggle overflow TipThe
OBJECTstorage type supportsCOUNTonly so you do not need to explicitly set the eviction type.BINARY<memory> <binary eviction="COUNT"/> </memory>
<memory> <binary eviction="COUNT"/> </memory>Copy to Clipboard Copied! Toggle word wrap Toggle overflow OFFHEAP<memory> <offheap eviction="MEMORY"/> </memory>
<memory> <offheap eviction="MEMORY"/> </memory>Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.2.6. Setting the Cache Size 复制链接链接已复制到粘贴板!
Include the size attribute with a value set to a number greater than zero.
-
For
COUNT, thesizeattribute sets the maximum number of entries the cache can hold before eviction starts. -
For
MEMORY, thesizeattribute sets the maximum number of bytes the cache can take from memory before eviction starts. For example, a value of10000000000is 10 GB.
Try different cache sizes to determine the optimal setting. A cache size that is too large can cause Red Hat JBoss Data Grid to run out of memory. At the same time, a cache size that is too small wastes available memory.
OBJECT<memory> <object size="100000"/> </memory>
<memory> <object size="100000"/> </memory>Copy to Clipboard Copied! Toggle word wrap Toggle overflow BINARY<memory> <binary eviction="COUNT" size="100000"/> </memory>
<memory> <binary eviction="COUNT" size="100000"/> </memory>Copy to Clipboard Copied! Toggle word wrap Toggle overflow OFFHEAP<memory> <offheap eviction="MEMORY" size="10000000000"/> </memory>
<memory> <offheap eviction="MEMORY" size="10000000000"/> </memory>Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.2.7. Tuning the Off Heap Configuration 复制链接链接已复制到粘贴板!
Include the address-count attribute when using OFFHEAP storage to prevent collisions that might decrease performance. This attribute specifies the number of pointers that are available in the hash map.
Set the value of the address-count attribute to a number that is greater than the number of cache entries. By default address-count is 2^20, or 1048576. The parameter is always rounded up to a power of 2.
<memory> <offheap eviction="MEMORY" size="10000000000" address-count="1048576"/> </memory>
<memory>
<offheap eviction="MEMORY" size="10000000000" address-count="1048576"/>
</memory>
2.2.8. Setting the Eviction Strategy 复制链接链接已复制到粘贴板!
Eviction strategies control how Red Hat JBoss Data Grid performs eviction. You set eviction strategies with the strategy attribute.
The default strategy is NONE, which disables eviction unless you explicitly configure it. For example, here are two configurations that have the same effect:
<memory/>
<memory/>
<memory> <object strategy="NONE"/> </memory>
<memory>
<object strategy="NONE"/>
</memory>
When you configure eviction, you implicitly use the REMOVE strategy. For example, the following two configurations have the same effect:
<memory> <object/> </memory>
<memory>
<object/>
</memory>
<memory> <object strategy="REMOVE"/> </memory>
<memory>
<object strategy="REMOVE"/>
</memory>
2.2.8.1. Eviction Strategies 复制链接链接已复制到粘贴板!
| Strategy | Description |
|---|---|
|
| JBoss Data Grid does not evict entries. This is the default setting unless you configure eviction. |
|
| JBoss Data Grid removes entries from memory so that the cache does not exceed the configured size. This is the default setting when you configure eviction. |
|
|
JBoss Data Grid does not perform eviction. Eviction takes place manually by invoking the |
|
|
JBoss Data Grid does not write new entries to the cache if doing so would exceed the configured size. Instead of writing new entries to the cache, JBoss Data Grid throws a |
2.2.9. Configuring Passivation 复制链接链接已复制到粘贴板!
Passivation configures Red Hat JBoss Data Grid to write entries to a persistent cache store when it removes those entries from memory. In this way, passivation ensures that only a single copy of an entry is maintained, either in-memory or in a cache store but not both.
For more information, see Setting Up Passivation.
2.3. Configuring Expiration 复制链接链接已复制到粘贴板!
You configure Red Hat JBoss Data Grid to perform expiration at either the entry or cache level.
If you configure expiration for the cache, all entries in that cache inherit that configuration. However, configuring expiration for specific entries takes precedence over configuration for the cache.
You configure expiration for a cache with the <expiration /> element. Alternatively, you can use the ExpirationConfigurationBuilder class to programmatically configure expiration for a cache.
You configure expiration for specific entries with the Cache API.
2.3.1. Expiration Parameters 复制链接链接已复制到粘贴板!
Expiration parameters configure the amount of time entries can remain in the cache.
lifespan-
Specifies how long entries can remain in the cache before they expire. The default value is
-1, which is unlimited time. max-idle-
Specifies how long entries can remain idle before they expire. An entry in the cache is idle when no operation is performed with the key. The default value is
-1, which is unlimited time. interval-
Specifies the amount of time between expiration operations. The default value is
60000.
While expiration parameters, lifespan and max-idle, are replicated across the cluster, only the value of the lifespan parameter is replicated along with cache entries. For this reason, you should not use the max-idle parameter with clustered caches. For more information on the limitations of using max-idle in clusters, see Red Hat knowledgebase workaround.
2.3.2. Configuring Expiration 复制链接链接已复制到粘贴板!
Configure Red Hat JBoss Data Grid to perform expiration for a cache as follows:
Add the
<expiration />element<expiration />
<expiration />Copy to Clipboard Copied! Toggle word wrap Toggle overflow Configure the
lifespanattribute.Specify the amount of time, in milliseconds, that an entry can remain in memory as the value, for example:
<expiration lifespan="1000" />
<expiration lifespan="1000" />Copy to Clipboard Copied! Toggle word wrap Toggle overflow Configure the
max-idleattribute.Specify the amount of time, in milliseconds, that an entry can remain idle as the value, for example:
<expiration lifespan="1000" max-idle="1000" />
<expiration lifespan="1000" max-idle="1000" />Copy to Clipboard Copied! Toggle word wrap Toggle overflow Configure the
intervalattribute.Specify the amount of time, in milliseconds, that Red Hat JBoss Data Grid waits between expiration operations, for example:
<expiration lifespan="1000" max-idle="1000" interval="120000" />
<expiration lifespan="1000" max-idle="1000" interval="120000" />Copy to Clipboard Copied! Toggle word wrap Toggle overflow TipSet a value of
-1to disable periodic expiration.
2.3.3. Expiration Behavior 复制链接链接已复制到粘贴板!
Red Hat JBoss Data Grid cannot always expire entries immediately when they reach the time limit. Instead, JBoss Data Grid marks entries as expired and removes them when:
- Writing entries to the cache store.
- The maintenance thread that processes expiration identifies entries as expired.
This behavior might indicate that JBoss Data Grid is not performing expiration as expected. However it is the case that the entries are marked as expired but not yet removed from either the memory or the cache store.
To ensure that users cannot receive expired entries, JBoss Data Grid returns null values for entries that are marked as expired but not yet removed.