0

There is a global HashMap in my application which contains custom object as a key. A third party will call this as API and they will create a new custom object and if it is not already there in the HashMap they will put it into the map. How to manage the OutOfMemoryError in this case as HashMap size goes on increasing and after deployment it is tedious to increase the JVM memory.

4
  • 1
    The thing is, should you really be using HashMap to store gigabytes of data? Commented Jun 27, 2018 at 7:12
  • Following up on NiVeR, this is what you'd use a DB for Commented Jun 27, 2018 at 7:13
  • adding to Laurens - Once you are using a db, you may want to use a map as a cache. There are many caches available to do this. Set up good eviction policy. Commented Jun 27, 2018 at 7:15
  • My question was more specific to cache implementation in this case.Thanks a lot for pointing out the usage of DB :) I should have asked more clearly about the cache implementation. Commented Jun 27, 2018 at 13:27

1 Answer 1

1

If this map can increase indefinitely, you need a different solution than just a HashMap in memory. Even if you keep increasing the JVM's limits, the actual hardware you're using is finite.

You could instead keep these keys in a database (or file, for that matter), and query against it. To improve performance, you can decide to keep the N recently used keys in memory as a cache.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.