Hazelcast:如果内存不足会发生什么

时间:2013-06-07 00:53:40

标签: hibernate memory queue hazelcast

我们计划使用Hazelcast排队Apache Camel消息以及作为辅助Hibernate缓存。

假设我们在没有支持映射的情况下配置我们的queue to use Integer.MAX_VALUE(可能不适合内存):如果正在使用所有内存,会发生什么?我在文档中找不到任何关于这种情况的内容。

<hazelcast>
...
<queue name="tasks">
    <!--
        Maximum size of the queue. When a JVM's local queue size reaches the maximum,
        all put/offer operations will get blocked until the queue size
        of the JVM goes down below the maximum.
        Any integer between 0 and Integer.MAX_VALUE. 0 means Integer.MAX_VALUE. Default is 0.
    -->
    <max-size-per-jvm>0</max-size-per-jvm>

    <!--
        Name of the map configuration that will be used for the backing distributed
        map for this queue.
    -->
    <backing-map-ref></backing-map-ref>
</queue>

与地图相同:如果将no TTL and the eviction policy设置为NONE会发生什么,所以整个地图迟早都不适合内存?

<hazelcast>
...
<map name="default">
    <!--
        Number of backups. If 1 is set as the backup-count for example,
        then all entries of the map will be copied to another JVM for
        fail-safety. Valid numbers are 0 (no backup), 1, 2, 3.
    -->
    <backup-count>1</backup-count>

    <!--
        Maximum number of seconds for each entry to stay in the map. Entries that are
        older than <time-to-live-seconds> and not updated for <time-to-live-seconds>
        will get automatically evicted from the map.
        Any integer between 0 and Integer.MAX_VALUE. 0 means infinite. Default is 0.
    -->
    <time-to-live-seconds>0</time-to-live-seconds>

    <!--
        Maximum number of seconds for each entry to stay idle in the map. Entries that are
        idle(not touched) for more than <max-idle-seconds> will get
        automatically evicted from the map.
        Entry is touched if get, put or containsKey is called.
        Any integer between 0 and Integer.MAX_VALUE.
        0 means infinite. Default is 0.
    -->
    <max-idle-seconds>0</max-idle-seconds>

    <!--
        Valid values are:
        NONE (no extra eviction, <time-to-live-seconds> may still apply),
        LRU  (Least Recently Used),
        LFU  (Least Frequently Used).
        NONE is the default.
        Regardless of the eviction policy used, <time-to-live-seconds> will still apply. 
    -->
    <eviction-policy>NONE</eviction-policy>

    <!--
        Maximum size of the map. When max size is reached,
        map is evicted based on the policy defined.
        Any integer between 0 and Integer.MAX_VALUE. 0 means
        Integer.MAX_VALUE. Default is 0.
    -->
    <max-size policy="cluster_wide_map_size">0</max-size>

    <!--
        When max. size is reached, specified percentage of
        the map will be evicted. Any integer between 0 and 100.
        If 25 is set for example, 25% of the entries will
        get evicted.
    -->
    <eviction-percentage>25</eviction-percentage>
   <!--
        Specifies when eviction will be started. Default value is 3. 
       So every 3 (+up to 5 for performance reasons) seconds 
       eviction will be kicked of. Eviction is costly operation, setting 
       this number too low, can decrease the performance. 
   -->
  <eviction-delay-seconds>3</eviction-delay-seconds>
</map>
</hazelcast>

虽然这是一个相当愚蠢的配置,但这只是内存不足时的一个例子。

2 个答案:

答案 0 :(得分:11)

默认情况下,Hazelcast会立即终止内存不足的节点。请参阅DefaultOutOfMemoryHandler

如果您希望更改默认行为,可以实施OutOfMemoryHandler

public class MyOutOfMemoryHandler extends OutOfMemoryHandler {

     public void onOutOfMemory(OutOfMemoryError oom, HazelcastInstance[] instances) {
        // handle oom
    }
}

并通过

注册

Hazelcast.setOutOfMemoryHandler(new MyOutOfMemoryHandler());

答案 1 :(得分:6)

对于队列,如果你不能以你生产的速度消耗,那么最终你可能会耗尽内存,这意味着当你尝试创建新对象时JVM会抛出OutOfMemoryException。如果每个可能都有积压,那么为队列设置最大大小通常是一种很好的做法。

如果您尝试在地图中插入太多条目,则内存耗尽的地图也是如此。

问题类似于&#34;我有一个本地java.util.HashMap,如果我尝试在其中插入10亿个条目会怎样?如果所有内存都被使用会发生什么&#34;。因此,Hazelcast并不是那么具体。如果我在问题中遗漏了任何内容,请告诉我。

-talip