Skip to content

Conversation

BrzVlad
Copy link
Member

@BrzVlad BrzVlad commented Jul 27, 2025

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 Copilot AI review requested due to automatic review settings July 27, 2025 19:08
Copy link
Contributor

@Copilot Copilot AI left a comment

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

@BrzVlad BrzVlad added the arch-wasm WebAssembly architecture label Jul 27, 2025
Copy link
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
Copy link
Member Author

BrzVlad commented Jul 27, 2025

/azp run runtime-extra-platforms

Copy link

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
Copy link
Member Author

BrzVlad commented Jul 30, 2025

@pavelsavara this seems green now

@pavelsavara
Copy link
Member

/azp run runtime-wasm

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Member

@pavelsavara pavelsavara left a comment

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

@pavelsavara
Copy link
Member

/ba-g all the CI failures are unrelated

@BrzVlad
Copy link
Member Author

BrzVlad commented Jul 30, 2025

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
2 participants