There is inconsistent pair of values between actual values(seen from gc) and MemoryMXBeans(MemoryUsage.used()).
The related code is:
G1MonitoringSupport::recalculate_sizes() {
...
_overall_used = _g1h->used_unlocked();
_eden_space_used = _g1h->eden_regions_used_bytes();
...
_old_gen_used = subtract_up_to_zero(_overall_used, _eden_space_used + _survivor_space_used);
...
}
and
G1CollectedHeap::retire_mutator_alloc_region() {
...
increase_used(allocated_bytes);
_eden.add_used_bytes(allocated_bytes);
...
}
subtract_up_to_zero(size_t x, size_t y) {
if (x > y) {
return x - y;
} else {
return 0;
}
subtract_up_to_zero() is used prevent followed assert failure because of inconsistent values.
Popped up during the review of JDK-8218049.
Kim Barrett suggested using Double buffering or GlobalCounter.