Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

java - A useful metric for determining when the JVM is about to get into memory/GC trouble

I have a scala data processing application that 95% of the time can handle the data thrown at it in memory. The remaining 5% if left unchecked doesn't usually hit OutOfMemoryError, but just gets into a cycle of major GCs that spikes the CPU, prevents background threads from executing and, if it does even finish, takes 10x-50x as long as when it has enough memory.

I've implemented system that can flush data to disk and treat the disk stream as if it was an in-memory iterator. It's usually an order of magnitude slower than memory, but sufficient for these 5% cases. I'm currently triggering by a heuristic of max size of a collection context that tracks the size of various collections involved in the data processing. This works, but really is just an adhoc empirical threshold.

I would much rather react to the JVM getting near the above bad state and flush to disk at that time. I've tried watching memory, but can't find the right combination of eden, old, etc. to reliably predict the death spiral. I've also tried just watching for frequency of major GCs but that also seems to suffer from having a wide range of "too conservative" to "too late".

Any resources for judging JVM health and detecting trouble states would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In addition to the MemoryMXBean notification mechanisms described in @Alla's link. You can use a combination of weak references and reference queues. This old but valid article has a good description of weak, soft, and phantom references and reference queues.

The basic idea is to create a large array (to reserve memory) create a weak or soft reference to it and when doing so, add it to a reference queue. When memory pressure triggers the collection of the weakly referenced array, you will get the reserve memory (breathing life into your application hopefully and giving it time). Have a thread polling the reference queue to determine when your reserve has been collected. You can then trigger the file streaming behavior of your application to finish the job. SoftReferences are more resilient to memory pressure than WeakReferences and make serve your purposes better.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...