How to use MemoryPoolMXBean
We develop a server and we want to program congestion control into it. What we want to do is detect when free heap is below a certain threshold开发者_如何学编程 and stop accepting new data until free memory goes up again. Our first approach used runtime.freeMemory. This caused false positives as free heap went below the threshold before GC kicked in. I found a similar question on this site and the answer was to use MemoryPoolMXBean. This looks like the right way since we can get a notification when free memory AFTER GC is below a thershold. But... what pool to monitor? I don't want my implementation to be dependent on the type of GC the JVM decided to use. One option would be to sum the usage of all heap memory pools and use that as a metric. Is this a good solution?
Thanks, Doron
We tried the approach I described in my question: we are monitoring all the heap type memory pools, summing up their free space. Once the total free space goes below the threshold we go into congestion mode. We did extensive testing and it works great. So I guess I can mark this as answered.
精彩评论