skip to Main Content

I am using hashmap as a cache to store id and name. because it is frequently used and it lives through the lifetime of the application.
for every user using the application, around 5000 and more (depends on workspace) ids and names get stored in hashmap. At some point java.lang.OutOfMemoryError exception gets thrown. since I am saving a lot of (id, name) in hashmap.

I don’t want to clear my hashmap cache value. but I know to be efficient we have to clear cache using the LRU approach or other approaches.

Note: I don’t want to use Redis, Memcached, or any in-memory key-value store.

Usecase: slack will return id in place of the user name in every
message.

for eg: Hello @john doe = return Hello @dxap123.

I don’t want an API hit for every message to get the user name.

Can somebody provide me an alternate efficient approach or correct me if I am doing something wrong in my approach.?

3

Answers


  1. For 5000 key-value pairs, It should not through OutOfMemoryException. If It is throwing the same you are not managing the HashMap properly. If you have more than 5000 items and want an alternate for hashmap you can use ehcache, A widely adopted Java cache with tiered storage options instead of going with in-memory cache technologies.

    The memory areas supported by Ehcache include:

    On-Heap Store: Uses the Java heap memory to store cache entries and shares the memory with the application. The cache is also scanned by the garbage collection. This memory is very fast, but also very limited.

    Off-Heap Store: Uses the RAM to store cache entries. This memory is not subject to garbage collection. Still quite fast memory, but slower than the on-heap memory, because the cache entries have to be moved to the on-heap memory before they can be used.

    Disk Store: Uses the hard disk to store cache entries. Much slower than RAM. It is recommended to use a dedicated SSD that is only used for caching.

    You can find the documentation here. http://www.ehcache.org/documentation/

    If you are using spring-boot you can follow this article to implement the same.

    https://springframework.guru/using-ehcache-3-in-spring-boot/

    Login or Signup to reply.
  2. If the "names" are not unique, then try with calling on it String.intern() before inserting the "name" to the map, this would then reduce the memory usage.

    Login or Signup to reply.
  3. Like others have said 5000 shouldn’t give you out of memory, but if you don’t keep a limit on the size of the map eventually you will get out of memory error. You should cache the values that are most recently used or most frequently used to optimize the size of the map.

    Google guava library has cache implementations which i think would fit your usecase

    https://github.com/google/guava/wiki/CachesExplained

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search