Apache Ignite:ONHEAP_TIERED模式下驱逐策略未启用错误排查与解决
Hey there, let's break down this Apache Ignite warning/error you're encountering: Eviction policy not enabled with ONHEAP_TIERED mode for cache (entries will not be moved to off-heap store)
First, let's understand what the ONHEAP_TIERED memory mode does: this mode splits your cache's memory into two tiers—on-heap (JVM heap) and off-heap (direct memory outside the JVM). The goal is to automatically move less frequently used data from the on-heap tier to off-heap to free up precious heap space for hot, frequently accessed data.
However, this tiered migration relies entirely on an eviction policy to trigger the move. Without an eviction policy configured, Ignite has no rules to determine which entries should be moved off-heap, or when to do it. That's exactly why you're seeing this error: the system is warning you that the tiered functionality won't work as intended, and all data will stay stuck in the on-heap tier (which could lead to excessive heap usage or even OutOfMemoryErrors over time).
Fixing this is straightforward—you just need to enable and configure an appropriate eviction policy for your cache. Here are the steps and examples:
1. Choose and configure an eviction policy
Ignite provides several built-in eviction policies; the most common ones are:
LruEvictionPolicy: Least Recently Used (removes the least recently accessed entries first)LfuEvictionPolicy: Least Frequently Used (removes the least frequently accessed entries first)FifoEvictionPolicy: First In First Out (removes oldest entries first)
Example configuration (XML)
Add the eviction policy to your cache configuration:
<bean class="org.apache.ignite.configuration.CacheConfiguration"> <property name="name" value="yourCacheName"/> <!-- Enable ONHEAP_TIERED mode --> <property name="memoryMode" value="ONHEAP_TIERED"/> <!-- Configure LRU eviction policy --> <property name="evictionPolicy"> <bean class="org.apache.ignite.cache.eviction.lru.LruEvictionPolicy"> <!-- Set max number of entries to keep in on-heap tier --> <property name="maxSize" value="10000"/> <!-- Optional: Set max memory size (in bytes) for on-heap tier --> <!-- <property name="maxMemorySize" value="104857600"/> --> </bean> </property> </bean>
Example configuration (Java code)
If you're configuring the cache programmatically:
CacheConfiguration<Integer, YourDataType> cacheCfg = new CacheConfiguration<>("yourCacheName"); // Enable ONHEAP_TIERED memory mode cacheCfg.setMemoryMode(CacheMemoryMode.ONHEAP_TIERED); // Initialize and configure LRU eviction policy LruEvictionPolicy<Integer, YourDataType> evictionPolicy = new LruEvictionPolicy<>(); evictionPolicy.setMaxSize(10000); // Keep up to 10,000 entries in on-heap // evictionPolicy.setMaxMemorySize(104857600); // Optional: 100MB limit for on-heap // Attach the policy to the cache cacheCfg.setEvictionPolicy(evictionPolicy); // Start the cache with this configuration Ignite ignite = Ignition.start(); ignite.getOrCreateCache(cacheCfg);
2. Verify your memory mode setting
Double-check that your cache is indeed configured with ONHEAP_TIERED (not OFFHEAP_TIERED or ONHEAP). This error only triggers when the tiered on-heap mode is enabled but no eviction policy is set.
3. Tune eviction policy parameters
Adjust the maxSize or maxMemorySize values based on your application's needs:
maxSize: Controls the maximum number of entries allowed in the on-heap tier. Once this limit is hit, the eviction policy will start moving entries to off-heap.maxMemorySize: Controls the maximum amount of memory (in bytes) the on-heap tier can use. This is useful if your entries vary in size.
内容的提问来源于stack exchange,提问作者Taejoon Kim

