I'm using hazelcast 3.12.2 version. Below is the code snippet that I used;
package com;
import com.hazelcast.config.Config;
import com.hazelcast.config.EvictionPolicy;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.MaxSizeConfig;
import com.hazelcast.core.EntryEvent;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.map.listener.EntryEvictedListener;
public class HazelcastMaxSizeTest {
private static final String GROUP_NAME = "TEST";
private static final String MAP_NAME = "test";
private static final String MAP_NAME1 = "test1";
private static final String MAP_NAME2 = "test2";
private static MaxSizeConfig POLICY_THAT_DOES_NOT_WORK = new MaxSizeConfig(
20,
MaxSizeConfig.MaxSizePolicy.FREE_HEAP_PERCENTAGE);
public static void main(String[] args) throws Exception {
HazelcastInstance instance = startHazelcast("hazelcast1", POLICY_THAT_DOES_NOT_WORK);
System.out.println("started " + instance.getName());
IMap<Long, byte[]> map = createMap(instance, MAP_NAME, 10000000);
System.out.println("map size: " + map.size());
IMap<Long, byte[]> map1 = createMap(instance, MAP_NAME1, 15000000);
System.out.println("map1 size: " + map1.size());
IMap<Long, byte[]> map2 = createMap(instance, MAP_NAME2, 20000000);
System.out.println("map2 size: " + map2.size());
instance.shutdown();
}
private static IMap<Long, byte[]> createMap(HazelcastInstance instance, String mapname, int bytes) {
IMap<Long, byte[]> map = instance.getMap(mapname);
map.addEntryListener(new EntryEvictedListener<Long, byte[]>() {
@Override
public void entryEvicted(EntryEvent<Long, byte[]> event) {
System.out.println("evicted " + event.getName() + ": " + event.getKey());
}
}, false);
for (long i = 1; i <= 10; i++) {
map.set(i, new byte[bytes]);
}
return map;
}
private static HazelcastInstance startHazelcast(String instanceName, MaxSizeConfig maxSizeConfig) {
MapConfig mapConfig = new MapConfig(MAP_NAME);
mapConfig.setMaxSizeConfig(maxSizeConfig);
mapConfig.setStatisticsEnabled(false);
mapConfig.setEvictionPolicy(EvictionPolicy.LRU);
mapConfig.setMinEvictionCheckMillis(0L);
mapConfig.setBackupCount(1);
MapConfig mapConfig1 = new MapConfig(MAP_NAME1);
mapConfig.setMaxSizeConfig(maxSizeConfig);
mapConfig.setStatisticsEnabled(false);
mapConfig.setEvictionPolicy(EvictionPolicy.LRU);
mapConfig.setMinEvictionCheckMillis(0L);
mapConfig.setBackupCount(1);
MapConfig mapConfig2 = new MapConfig(MAP_NAME2);
mapConfig.setMaxSizeConfig(maxSizeConfig);
mapConfig.setStatisticsEnabled(false);
mapConfig.setEvictionPolicy(EvictionPolicy.LRU);
mapConfig.setMinEvictionCheckMillis(0L);
mapConfig.setBackupCount(1);
Config config = new Config(instanceName);
config.addMapConfig(mapConfig);
config.addMapConfig(mapConfig1);
config.addMapConfig(mapConfig2);
config.getGroupConfig().setName(GROUP_NAME).setPassword(GROUP_NAME);
return Hazelcast.getOrCreateHazelcastInstance(config);
}
}
When I try to run the above code with 450mb heap space it is ending up with 'Out of Memory' error. After debugging I knew that eviction is not working even if the heap space used is 80%. Please let me know if I'm configured incorrectly. Any suggestion will be genuinely helpful.