Skip to content

[wasm] Fix fragmentation caused by large objects allocation - #118099

Merged
pavelsavara merged 3 commits into
dotnet:mainfrom
BrzVlad:fix-wasm-los-fragmentation
Jul 30, 2025
Merged

pavelsavara merged 3 commits into
dotnet:mainfrom
BrzVlad:fix-wasm-los-fragmentation

Conversation

@BrzVlad

@BrzVlad BrzVlad commented Jul 27, 2025

Copy link
Copy Markdown
Member

For large objects (bigger than 8K), we have two different allocators. We either allocate them in LOS sections (a section has 1MB), if the object is smaller than 1MB in size, or allocate it directly from the OS. A LOS section has 1MB alignment. On wasm this huge alignment is problematic leading to memory fragmentation. This PR disables allocation from LOS sections, allocating each object independently.

Testing allocation for 10k sized objects with console-v8 this change showed the following results

  • allocation speed is about 20% faster
  • fragmentation becomes 0. Data obtained from GC.GetGCMemoryInfo:
    • BEFORE: Total Memory: 99.61 MB | HeapSizeBytes: 236.59 MB | FragmentedBytes: 141.00 MB
    • AFTER: Total Memory: 99.61 MB | HeapSizeBytes: 96.21 MB | FragmentedBytes: 634.73 KB

Fixes #118044

Copilot AI review requested due to automatic review settings July 27, 2025 19:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR addresses memory fragmentation issues in WebAssembly (WASM) builds by disabling the Large Object Space (LOS) sections allocator. Instead of using 1MB-aligned LOS sections for objects between 8K and 1MB, it allocates each large object independently from the OS, eliminating fragmentation while improving allocation speed by ~20%.

Key changes:

  • Introduces a compile-time flag to disable LOS sections allocator on WASM
  • Updates allocation and deallocation logic to handle direct OS memory allocation when sections are disabled
  • Adds early return in consistency checking when sections allocator is disabled

Comment thread src/mono/mono/sgen/sgen-los.c Resolved
@BrzVlad BrzVlad added the arch-wasm WebAssembly architecture label Jul 27, 2025
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @BrzVlad
See info in area-owners.md if you want to be subscribed.

BrzVlad added 2 commits July 27, 2025 22:10
Allocation in the segments is normally done for objects smaller than 1MB. The problem is that for los sections we require 1MB alignment and on wasm we end up allocating twice the memory in order to ensure the alignment. With this commit we reuse the same allocation code that was used for objects bigger than 1MB. This still uses the unfortunate alignment of page size.
…TION_OBJECT_LIMIT

On mono, objects need to have the alignment of 8 bytes. We allocate using this alignemnt directly for objects smaller than 1MB. This further decreases fragmentation and increases allocation speed.
@BrzVlad
BrzVlad force-pushed the fix-wasm-los-fragmentation branch from d118803 to 73b757a Compare July 27, 2025 19:10
@BrzVlad
BrzVlad requested a review from pavelsavara July 27, 2025 19:11
@BrzVlad

BrzVlad commented Jul 27, 2025

Copy link
Copy Markdown
Member Author

/azp run runtime-extra-platforms

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@pavelsavara

This comment was marked as resolved.

This will produce some waste, but 512 alignment is still quite small compared to the size of the objects here (> 8k)
@BrzVlad
BrzVlad force-pushed the fix-wasm-los-fragmentation branch from 15a68b0 to d995297 Compare July 29, 2025 11:54
@BrzVlad

BrzVlad commented Jul 30, 2025

Copy link
Copy Markdown
Member Author

@pavelsavara this seems green now

@pavelsavara

Copy link
Copy Markdown
Member

/azp run runtime-wasm

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@pavelsavara pavelsavara left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess that objects on LOS are never moved, so from this perspective it's no change and no perf impact.

I wonder if there is a allocation pattern for which this will cause more fragmentation than the previous state.

cc @kg

Comment thread src/mono/mono/sgen/sgen-los.c Resolved
@pavelsavara

Copy link
Copy Markdown
Member

/ba-g all the CI failures are unrelated

@BrzVlad

BrzVlad commented Jul 30, 2025

Copy link
Copy Markdown
Member Author

In my microbenchmark, allocation was actually faster with this change. However, allocation speed and fragmentation will depend on the posix_memalign allocator. I wouldn't be surprised if in some instances it would not perform great.

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

Labels

arch-wasm WebAssembly architecture area-GC-mono

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[wasm] High memory fragmentation on LOS allocations

3 participants