You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot集成Hazelcast缓存内存溢出问题及配置咨询

Great question! Let's break down your two concerns about Hazelcast caching in Spring Boot 1.5.2.RELEASE:

1. Default Cache Behavior & Lack of Default Eviction Policy

First, let's clarify the default behavior:

  • By default, Hazelcast Maps (which back your @Cacheable/@CacheEvict caches) do not have any size limits or eviction policies enabled. This means cached entries will stay in memory indefinitely until explicitly evicted via @CacheEvict, removed programmatically, or the cluster restarts. As a result, if your cache keeps growing without bounds, it will eventually consume all available heap memory and trigger an OutOfMemoryError—which is exactly what you're seeing.

Why no default LRU/LFU?

  • Hazelcast intentionally doesn't enable eviction by default because different applications have wildly different caching requirements. For example:
    • Some systems need to guarantee that cached data is always available (e.g., critical configuration data) and can't tolerate automatic eviction.
    • Others prioritize memory efficiency over data retention.
    • Enabling a default eviction policy could lead to unexpected data loss for users who don't realize it's active. Hazelcast leaves this decision to you, so you can tailor the policy to your specific use case.
2. Configuring Global Map Limits (Max Capacity, TTL, etc.) in hazelcast.xml

To set default rules for all your caches (backed by Hazelcast Maps), you can use the <map-defaults> element in your hazelcast.xml configuration. This will apply to every Map unless you override the settings for a specific Map.

Here's a complete example configuration:

<?xml version="1.0" encoding="UTF-8"?>
<hazelcast xmlns="http://www.hazelcast.com/schema/config"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.hazelcast.com/schema/config
           http://www.hazelcast.com/schema/config/hazelcast-config-3.8.xsd">

  <!-- Global defaults for all Maps -->
  <map-defaults>
    <!-- Set maximum size: PER_NODE means limit per cluster node, PER_PARTITION per data partition -->
    <max-size policy="PER_NODE">10000</max-size>
    <!-- Eviction policy: LRU (Least Recently Used), LFU (Least Frequently Used), NONE, RANDOM -->
    <eviction-policy>LRU</eviction-policy>
    <!-- Time-to-live: entries expire after this many seconds since creation -->
    <time-to-live-seconds>3600</time-to-live-seconds>
    <!-- Max idle time: entries expire if not accessed for this many seconds -->
    <max-idle-seconds>1800</max-idle-seconds>
    <!-- Enable eviction when max size is reached -->
    <eviction-percentage>20</eviction-percentage>
  </map-defaults>

  <!-- Optional: Override defaults for a specific cache/Map (e.g., "userCache") -->
  <map name="userCache">
    <max-size policy="PER_NODE">20000</max-size>
    <time-to-live-seconds>7200</time-to-live-seconds>
  </map>

</hazelcast>

Key details about the configuration parameters:

  • <max-size>: Controls the maximum number of entries allowed. The policy attribute defines how the limit is applied:
    • PER_NODE: Limit is per cluster node (most common for single-node setups).
    • PER_PARTITION: Limit per data partition (for distributed clusters).
    • USED_HEAP_SIZE: Limit based on heap memory used by the Map.
    • USED_HEAP_PERCENTAGE: Limit based on percentage of heap used by the Map.
  • <eviction-policy>: Choose the eviction strategy when the max size is hit—LRU is a safe default for most cases.
  • <time-to-live-seconds>: Ensures entries are automatically removed after a fixed period, even if they're still accessed.
  • <max-idle-seconds>: Removes entries that haven't been read/written to in the specified time, which is useful for stale data.
  • <eviction-percentage>: The percentage of entries to evict when the max size is reached (default is 20%).

Once you apply this configuration, your Spring Boot cache (backed by Hazelcast Maps) will automatically evict entries based on the rules you set, preventing memory overflow.

内容的提问来源于stack exchange,提问作者italktothewind

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:07:26