Skip to content

8372543: Shenandoah: undercalculated the available size when soft max takes effect#28622

Closed
rgithubli wants to merge 5 commits intoopenjdk:masterfrom
rgithubli:8372543
Closed

8372543: Shenandoah: undercalculated the available size when soft max takes effect#28622
rgithubli wants to merge 5 commits intoopenjdk:masterfrom
rgithubli:8372543

Conversation

@rgithubli
Copy link
Contributor

@rgithubli rgithubli commented Dec 3, 2025

Detailed math and repro see https://bugs.openjdk.org/browse/JDK-8372543.

Currently in shenandoah, when deciding whether to have gc, how we calculate available size is:

available = (Xmx * (100 - ShenandoahEvacReserve) / 100) - used
soft_tail = Xmx - soft_max
if (available - soft_tail < ShenandoahMinFreeThreshold * soft_max) // trigger gc 

The if condition available - soft_tail will be reduced to: -(ShenandoahEvacReserve/100) * Xmx - used + soft_max, which means when soft max is the same, the larger Xmx is, the less free size the app would have and the more gc it would have, which does not make sense, especially for the case where the app is mostly idle. This caused one of our internal customers experienced frequent gc with minimal workload, when soft max heap size was set way lower than Xmx.

Suggested fix: when deciding when to trigger gc, use logic similar to below:

mutator_soft_capacity = soft_max * (100 - ShenandoahEvacReserve) / 100;
available = mutator_soft_capacity - used;
if (available < mutator_soft_capacity) // trigger gc

Tests:

  • Ran the repro app StableLiveSet.java in https://bugs.openjdk.org/browse/JDK-8372543. Without fix, tip had ~2910 times gc in 20 sec with -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive -XX:SoftMaxHeapSize=512m -Xmx31g jvm args. With the fix, only 18 times in 20 sec.
  • GHA passed.

This change also improved gc logging:

Before:

[6.831s][info][gc          ] Trigger: Free (52230K) is below minimum threshold (52428K)
[6.831s][info][gc,free     ] Free: 1587M, Max: 1024K regular, 1539M humongous, Frag: 2% 
external, 18% internal; Used: 352M, Mutator Free: 1940 Collector Reserve: 103M, Max: 1024K; Used: 0B

After:

[8.358s][info][gc          ] Trigger: Free (Soft mutator free) (51498K) is below minimum threshold (52428K)
[8.358s][info][gc,free     ] Whole heap stats: Total free: 1509M, Total used: 401M, Max free in a single region: 
1024K, Max humongous: 1490M; Frag stats: External: 0%, Internal: 21%;  Mutator freeset stats: Partition count: 
1911, Reserved: 1509M, Max free available in a single region: 1024K; Collector freeset stats: Partition count: 
122, Reserved: 102M, Max free available in a single region: 1024K;

Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8372543: Shenandoah: undercalculated the available size when soft max takes effect (Bug - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/28622/head:pull/28622
$ git checkout pull/28622

Update a local copy of the PR:
$ git checkout pull/28622
$ git pull https://git.openjdk.org/jdk.git pull/28622/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 28622

View PR using the GUI difftool:
$ git pr show -t 28622

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/28622.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Dec 3, 2025

👋 Welcome back rgithubli! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Dec 3, 2025

@rgithubli This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8372543: Shenandoah: undercalculated the available size when soft max takes effect

Reviewed-by: wkemper, kdnilsen

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 207 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@earthling-amzn, @kdnilsen) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk openjdk bot added hotspot-gc hotspot-gc-dev@openjdk.org shenandoah shenandoah-dev@openjdk.org labels Dec 3, 2025
@openjdk
Copy link

openjdk bot commented Dec 3, 2025

@rgithubli The following labels will be automatically applied to this pull request:

  • hotspot-gc
  • shenandoah

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@rgithubli rgithubli marked this pull request as ready for review December 3, 2025 07:27
@openjdk openjdk bot added the rfr Pull request is ready for review label Dec 3, 2025
@mlbridge
Copy link

mlbridge bot commented Dec 3, 2025

Webrevs

Copy link
Contributor

@earthling-amzn earthling-amzn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nits. Thank you for adding a test case for this!

size_t min_threshold = min_free_threshold();
if (available < min_threshold) {
log_trigger("Free (%zu%s) is below minimum threshold (%zu%s)",
log_trigger("Free (Soft mutator free) (%zu%s) is below minimum threshold (%zu%s)",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing this will break some log parsers, do we really need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talked offline. Free is overloaded in logs. Sometimes it means soft free, sometimes it means total free. Make it as Free (Soft) here.

assert(max_capacity() >= ShenandoahHeap::heap()->soft_max_capacity(), "Max capacity must be greater than soft max capacity.");
size_t soft_tail = max_capacity() - ShenandoahHeap::heap()->soft_max_capacity();
return (available > soft_tail) ? (available - soft_tail) : 0;
size_t ShenandoahGlobalGeneration::soft_available_exclude_evac_reserve() const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two questions:

  • How is this override different from the default implementation now?
  • Should we not also take the minimum of this value and free_set()->available() as we do elsewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Good call. No functional differences except for a safety assert: assert(max_capacity() >= soft_max, which isn't that necessary since the app wouldn't start if this wasn't true: code. Will remove the override.
  • I don't think it's needed for global. free_set()->available() (space reserved for mutators) could be smaller than mutator_soft_max when there's an old gen taking space. If there's no generation, the mutator_soft_max should always be less or equal than free_set()->available().

size_t ShenandoahGeneration::soft_available() const {
size_t result = available(ShenandoahHeap::heap()->soft_max_capacity());
size_t ShenandoahGeneration::soft_available_exclude_evac_reserve() const {
size_t result = available(ShenandoahHeap::heap()->soft_max_capacity() * (100.0 - ShenandoahEvacReserve) / 100);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little uncomfortable with this approach. It's mostly a question of how we name it. The evac reserve is not always this value. In particular, we may shrink the young evac reserves after we have selected the cset. Also of concern is that if someone invokes this function on old_generation(), it looks like they'll get a bogus (not meaningful) value.

I think I'd be more comfortable with naming this to something like "mutator_available_when_gc_is_idle()". If we keep it virtual, then OldGeneration should override with "assert(false, "Not relevant to old generation")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talked offline. Rename this to soft_mutator_available

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

size_t min_threshold = min_free_threshold();
if (available < min_threshold) {
log_trigger("Free (%zu%s) is below minimum threshold (%zu%s)",
log_trigger("Free (Soft) (%zu%s) is below minimum threshold (%zu%s)",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to use log format macros PROPERFMT & PROPERFMTARGS here. Will update.

size_t ShenandoahGeneration::soft_available() const {
size_t result = available(ShenandoahHeap::heap()->soft_max_capacity());
size_t ShenandoahGeneration::soft_available_exclude_evac_reserve() const {
size_t result = available(ShenandoahHeap::heap()->soft_max_capacity() * (100.0 - ShenandoahEvacReserve) / 100);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

Copy link
Contributor

@earthling-amzn earthling-amzn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Dec 16, 2025
@rgithubli
Copy link
Contributor Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Dec 16, 2025
@openjdk
Copy link

openjdk bot commented Dec 16, 2025

@rgithubli
Your change (at version 8dea516) is now ready to be sponsored by a Committer.

@pengxiaolong
Copy link

Looks good, thanks!

/sponsor

@openjdk
Copy link

openjdk bot commented Dec 16, 2025

Going to push as commit b1e8c4e.
Since your change was applied there have been 207 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Dec 16, 2025
@openjdk openjdk bot closed this Dec 16, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels Dec 16, 2025
@openjdk
Copy link

openjdk bot commented Dec 16, 2025

@pengxiaolong @rgithubli Pushed as commit b1e8c4e.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hotspot-gc hotspot-gc-dev@openjdk.org integrated Pull request has been integrated shenandoah shenandoah-dev@openjdk.org

Development

Successfully merging this pull request may close these issues.

4 participants