Add accumulative allocation tracking to allocators #3056
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Prerequisites
Description
Fixes #2850
This pull request introduces a new accumulative allocation tracking system to the
MemoryAllocatorinfrastructure, allowing for a configurable cap on the total memory allocated over the allocator's lifetime. It adds new configuration options, implements tracking logic, and updates all relevant allocators to enforce and release this new accumulative allocation limit. Other minor improvements include enhanced documentation and some code style refinements.Accumulative Allocation Tracking and Limits
MemoryAllocator, configurable via the newAccumulativeAllocationLimitMegabytesproperty inMemoryAllocatorOptions. This limit is enforced across all allocations, with platform-specific defaults (2GB for 32-bit, 8GB for 64-bit processes). [1] [2]MemoryAllocatorto track total allocated bytes, reserve/release allocation, suppress tracking during batch operations, and wrap allocations with a tracking owner that releases the reservation upon disposal. [1] [2]SimpleGcMemoryAllocator,UnmanagedMemoryAllocator, andUniformUnmanagedMemoryPoolMemoryAllocatorto enforce the accumulative allocation limit and to properly track and release allocations. [1] [2] [3] [4] [5]Configuration and API Improvements
MemoryAllocatorOptionswith the newAccumulativeAllocationLimitMegabytesproperty, including validation and documentation. [1] [2]Interface and Code Style Enhancements
IMemoryGroup<T>explicitlypublicfor consistency and clarity. [1] [2] [3]These changes collectively provide stronger safety against excessive memory consumption, offer more configurability for consumers, and improve maintainability and clarity of the memory allocation subsystem.