简体   繁体   中英

Handling OutOfMemory Error in java

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM