UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

architecture

[DOTS] Tuning InternalBufferCapacity for Large Data Sets

Solution

dotsecsperformanceoptimizationmemory management

Unity 2022.3.x - Unity 6.3.x

Published 28 days ago

Issue

 Attempts to store massive quantities of InternalBufferCapacity data within Entity chunks often result in ArchetypeChunk overflow and performance bottlenecks due to the fixed 16KB size constraint of Unity ECS memory blocks.

Explanation

Entity chunks within Unity ECS are constrained to a 16KB memory limit. When the InternalBufferCapacity attribute is defined with a value greater than zero, elements of the DynamicBuffer are embedded directly within the ArchetypeChunk to improve cache locality.

If the total size of these elements exceeds the chunk capacity, it causes immediate overflow and forces the framework to allocate memory externally anyway, while wasting internal chunk space that could be used for other entities. Setting the capacity to zero ensures that the Entity footprint remains small and that all buffer data is handled by the heap.

  1. Define your buffer element struct implementing the IBufferElementData interface.
  2. Decorate the struct with the InternalBufferCapacity attribute, passing 0 as the argument.
  3. Access your buffer via EntityManager or within a SystemBase to verify heap-based allocation is active.

Additional Tips:

  • Setting InternalBufferCapacity to 0 is optimal when the number of elements is unknown or exceeds several hundred units.
  • This configuration prevents the “Internal Buffer” from consuming the 16KB chunk budget, allowing more entities per chunk.
  • Use BufferAccessor within an IJobEntity to process these external buffers efficiently across multiple entities.

TL;DR

To efficiently manage massive DynamicBuffer collections in Unity ECS, setting the InternalBufferCapacity to 0 forces external heap allocation, preventing ArchetypeChunk fragmentation.


Related Posts Haven't quite found a solution to your problem? We think these posts might help you.

Content inspired by a Unity discussion post.