What happens when a collection in Java exceeds its capacity?

I have a service that schedules all calls in memory because we don't want to lose data. At the same time, we need this service to fail because of any external dependencies (such as dB) Then periodically pick up these phased calls and process them in the background

If, for any reason, there are too many phones and our memory runs out, we need to be vigilant

So, to put it simply, the question is: what exceptions do I need to catch or monitor to notify me when the list addition fails due to insufficient resources? Will it lead to oom in the VM itself, or are there set level restrictions?

If there are no collection level restrictions, how would you advise me to monitor service usage? At present, we have heap usage and memory usage indicators Are those enough? In addition, the JVM is configured to terminate on an oom error (because the VM Manager then restarts any processes it manages on kill)

Solution

The exception to throw is OutOfMemoryException Once your collection occupies all available heap space, you can throw this exception in any part of the application

However, if you know that it may be thrown against a particular collection, the best way may be to prevent this from happening, that is, limit the collection or use the cache to evict and reload unused entities as needed For a lightweight cache implementation, I recommend using guava's cachebuilder

UPDATE

Since everyone suggests FS based storage, here is my lightweight plug-in proposal:

>Cachebuilder loads serialization data from NoSQL DB > kryo serializer converts your object to byte [] > MapDB storage (or any other embedded NoSQL solution you like)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>