0

I have tried to put in cache some objects.

With Hazelcast I have an Out of memory when I put around 30 objects.

However, with ehCache 3 I can put 1000 without problems.

Am I using properly Hazelcast?

HAZELCAST:

     Config config = new Config().addCacheConfig(new 
     CacheSimpleConfig().setName("testCache"));
     config.setInstanceName("hzInstanceTest");
     HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);

     List<CaLpgDataRowDto<CaBigNumber>> bigList = lpgDatasource.getDataRows();

     while (bigList.size() <= 5000000)
     {
        bigList.addAll(bigList);
     }

     lpgDatasource.setDataRows(bigList);

     ICacheManager hazelcastCacheManager = instance.getCacheManager();
     ICache<String, CaLpgDataCollectionDto<CaBigNumber>> cache = hazelcastCacheManager.getCache("testCache");

     System.out.println("Free memory before (bytes): " + Runtime.getRuntime().freeMemory());

     for (int i = 0; i < 33 ; i++)
     {
        cache.put("objectTest"+i, lpgDatasource);
     }

     System.out.println("Free memory after  (bytes): " + Runtime.getRuntime().freeMemory());

EHCACHE 3:

    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
           .withCache("ehInstanceTest",
                 CacheConfigurationBuilder
                       .newCacheConfigurationBuilder(String.class, CaLpgDataCollectionDto.class, ResourcePoolsBuilder
                             .heap(10)))
           .build();

     cacheManager.init();

     Cache<String, CaLpgDataCollectionDto> testCache = cacheManager.createCache("testCache",
           CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, CaLpgDataCollectionDto.class, ResourcePoolsBuilder.heap(1000)));

     List<CaLpgDataRowDto<CaBigNumber>> bigList = lpgDatasource.getDataRows();

     while (bigList.size() <= 5000000)
     {
        bigList.addAll(bigList);
     }

     lpgDatasource.setDataRows(bigList);

     System.out.println("Free memory before (bytes): " + Runtime.getRuntime().freeMemory());

     for (int i = 0; i < 1000 ; i++)
     {
        testCache.put("objectTest"+i,lpgDatasource);
     }

     System.out.println("Free memory after  (bytes): " + Runtime.getRuntime().freeMemory());
Hossain
  • 31
  • 6

1 Answers1

0

This line is to blame:

bigList.addAll(bigList);

Try adding unique items to the list to build it up, rather than add it to itself.

Otherwise it's a list of 5,000,000 references to significantly less than 5,000,000 objects. Different cache managers will handle this differently, depending whether or not they serialize the list.

Neil Stevenson
  • 3,060
  • 9
  • 11
  • Yes. This is the objective. To store some objects with big list. The question is if really I am using properly hazzelcast in embedded mode. – Hossain Aug 14 '19 at 07:13
  • How many objects ? It's possibly a flawed test. EhCache does *store-by-reference* by default, Hazelcast does *store-by-value*. Change EhCache to *store-by-value*, [see](https://www.ehcache.org/documentation/3.0/serializers-copiers.html) and memory usage will be comparable. – Neil Stevenson Aug 14 '19 at 08:01
  • Hi. Yes. I am comparing some cache technologies in order to select the best for my use case. Do you know if it is possible to change Hazelcast to store-by-reference? – Hossain Aug 14 '19 at 09:09
  • It has to be *store-by-value* as storage is usually on a different JVM. So the problem here is that the default serializer doesn't recognise the same object appears more than once in the list and duplicates it. This you could solve by supplying a custom serializer. Whether this is worthwhile depends if you need the extra features that Hazelcast provides over EhCache. – Neil Stevenson Aug 14 '19 at 09:44
  • Other question. Do you know an example in ehcache 3.8.0 where I can see how to configure it to store by value? In my case we need a local cache. The application adn cache will be in the same JVM. I thinkk that in this case I can use store by reference. – Hossain Aug 14 '19 at 09:55