#Questions about ECS memory allocation

1 messages · Page 1 of 1 (latest)

obtuse rover
#

Hi guys, Im trying to implement a system to enable/disable rendering, changing the renderingFilter.ShadowCastingMode to ShadowCastingMode.On and ShadowCastingMode.ShadowsOnly.
For this Im using a tag system to call the exchange, called NotVisibleTag.
But using the AddComponent and RemoveComponent functions it seems to get more and more memory allocation.
I wanted to see if there is any alternative to avoid this.

safe bronze
#

IEnablableComponents

obtuse rover
#

Thanks @safe bronze!

obtuse rover
#

Hi, another question!

Seems like when accessing DynamicBuffer<Child> generates memory allocation, there's right?

Entities.
        WithBurst().
        ForEach((Entity entity, int entityInQueryIndex, in DynamicBuffer<Child> children)
{
        foreach (var child in children) // here generates memory allocation
}```

If rights, in what ways I can circumvent this and iterate?
safe bronze
#

but iterating over buffer is indeed not super fast, so if collection is big - you can foreach over children.AsNativeArray()

obtuse rover
#

so... I think the parallelEcb generates instead:

var nativeChildren = children.ToNativeArray(Allocator.Temp);

int i = 0;
foreach (var child in nativeChildren)
{
    parallelEcb.SetComponentEnabled<ObjectVisibleComponentData>(i, child.Value, isVisible);
    parallelEcb.SetComponentEnabled<ChangeRenderTag>(i, child.Value, true);
    i++;
}

nativeChildren.Dispose();
obtuse rover
#

sorry, change to AsNativeArray
var nativeChildren = children.AsNativeArray();

#

confirming, using parallelEcb.SetComponentEnabled or ecb in child makes memory allocation

#

removing these lines its not generate memory allocation

safe bronze
#

it's command buffer

#

and you are writing commands

obtuse rover
#

ok, but I finally figured it out...
SetCollisionFilter generates a huge memory allocation.
this called for another system but triggered on that ChangeRenderTag.
I use this to deactivate some collisions detection

#
RenderSystem.cs

foreach (var (changeRenderTag entity) in 
         SystemAPI.Query<RefRO<ChangeRenderTag>>().WithEntityAccess())
{
    RenderFilterSettings renderFilterSettings = state.EntityManager.GetSharedComponent<RenderFilterSettings>(entity);
    bool hasVisibility = state.EntityManager.IsComponentEnabled<ObjectVisibleComponentData>(entity);
    var collider = state.EntityManager.GetComponentData<PhysicsCollider>(entity);
    
    // Starts here
    if (!hasVisibility)
    {
        collider.Value.Value.SetCollisionFilter(_notVisibleCollisionFilter);
    }
    else
    {
        var baseColor = state.EntityManager.GetComponentData<URPMaterialPropertyBaseColor>(entity);
        collider.Value.Value.SetCollisionFilter(baseColor.Value.w >= 1f ? _visibleCollisionFilter : _alphaVisibleCollisionFilter);
    }
    // Ends here
    
    renderFilterSettings.ShadowCastingMode = hasVisibility ? ShadowCastingMode.On : ShadowCastingMode.ShadowsOnly;
    
    ecb.SetSharedComponent(entity, renderFilterSettings);
    ecb.SetComponentEnabled<ChangeRenderTag>(entity, false);
}
#

theres another way to change the filter without generating memory allocation or cleaning after changing it?

#

or other way to deactivate and reactive collision? Disabled tag maybe?

safe bronze
#

why memory allocation is a problem at all?

#

I highly doubt it's huge anyway

obtuse rover
#

well, when I use the app for a seconds, hes increasing 300 MB of memory to the Android phone

#

when I comment the SetCollisionFitler lines, its works normally

#

this huge memory allocation its causing a crash for OutOfMemory

safe bronze
#

🤔

obtuse rover
#

yes this is giving me a headache

silk gorge
safe bronze
#

since those commands

#

are not even allocating anything

#

they just write values to existing memory

#

and for sure they won't be allocating 300mb

silk gorge
obtuse rover
obtuse rover
obtuse rover
#

Thanks @safe bronze and @silk gorge for your time

silk gorge