Skip to content

[GLUTEN-13044][CORE] Fix exceedsMaxMemoryUsage to use actual used heap instead of committed heap - #13045

Open
r7raul1984 wants to merge 1 commit into
apache:branch-1.7from
r7raul1984:GLUTEN-13044
Open

r7raul1984 wants to merge 1 commit into
apache:branch-1.7from
r7raul1984:GLUTEN-13044

Conversation

@r7raul1984

Copy link
Copy Markdown
Contributor

Fixes #13044

DynamicOffHeapSizingMemoryTarget.exceedsMaxMemoryUsage() previously used Runtime.getRuntime().totalMemory() (committed heap) as the on-heap memory pressure indicator. Committed heap is the memory the JVM has reserved from the OS, which includes a potentially large
amount of free pages that have not yet been returned. As a result, the check overstates memory pressure between Spark stages and triggers spurious OOMs.

Example of the failure:

  • Stage 0 runs with 2 concurrent tasks (~2 GB peak each); JVM commits ~8.9 GB of a 9 GB heap
  • Stage 0 finishes; actual heap usage drops to ~2 GB, but totalMemory() remains ~8.9 GB because the JVM has not yet returned the committed pages to the OS
  • Stage 1 starts and requests 8 MB: 8 MB + 0 + 8.9 GB >= 9 GB → OOM triggered
  • Actual free heap was ~6.5 GB (freeMemory()); the allocation should have succeeded

Fix:

Replace totalMemory() with totalMemory() - freeMemory() (actual used heap) at all four call sites of exceedsMaxMemoryUsage() — three in borrow() and one in shouldTriggerAsyncOnHeapMemoryShrink(). The parameter is renamed from totalOnHeapMemory to usedOnHeapMemory for
clarity.

// Before
exceedsMaxMemoryUsage(totalHeapMemory, usedOffHeapMemory, size, 1.0)

// After
exceedsMaxMemoryUsage(totalHeapMemory - freeHeapMemory, usedOffHeapMemory, size, 1.0)

Note: freeHeapMemory is already read from Runtime.getRuntime().freeMemory() at the top of borrow(), so no additional JVM calls are introduced.

How was this patch tested?

Existing unit tests in DynamicOffHeapSizingMemoryTargetTest (added in #12425) continue to pass. New tests covering the spurious-OOM scenario between stages will be added as a follow-up.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Sonnet 4.6, Anthropic

@github-actions github-actions Bot added the CORE works for Gluten Core label Sep 17, 2026
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

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

Labels

CORE works for Gluten Core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant