Skip to content

Memory leak in bevy_pbr #20804

@ed-2100

Description

@ed-2100

Bevy version and features

94bb6e1 w/ default features.

What I did

I made a basic bevy app with default plugins and ran it.

What went wrong

I witnessed unbounded memory growth in my task manager and then proceeded to debug bevy using the following steps:

  1. valgrind --tool=massif --time-unit=ms target/debug/my_app
  2. massif-visualizer $MY_MASSIF_FILE
  3. Found that process_queue() in crates/bevy_render/src/render_resource/pipeline_cache.rs was processing pipelines without me doing anything.
  4. I proceeded to use lldb-dap to trace the calls from process_queue() to queue_atmosphere_probe_pipelines.

In crates/bevy_pbr/src/atmosphere/environment.rs:

pub fn queue_atmosphere_probe_pipelines(
    pipeline_cache: Res<PipelineCache>,
    layouts: Res<AtmosphereProbeLayouts>,
    asset_server: Res<AssetServer>,
    mut commands: Commands,
) {
    let environment = pipeline_cache.queue_compute_pipeline(ComputePipelineDescriptor {
        label: Some("environment_pipeline".into()),
        layout: vec![layouts.environment.clone()],
        shader: load_embedded_asset!(asset_server.as_ref(), "environment.wgsl"),
        ..default()
    });
    commands.insert_resource(AtmosphereProbePipelines { environment });
}

This function is getting called in the Render schedule in the Queue phase.

In crates/bevy_pbr/src/atmosphere/mod.rs:

            .add_systems(
                Render,
                (
                    configure_camera_depth_usages.in_set(RenderSystems::ManageViews),
                    queue_render_sky_pipelines.in_set(RenderSystems::Queue),
                    prepare_atmosphere_textures.in_set(RenderSystems::PrepareResources),
                    prepare_probe_textures
                        .in_set(RenderSystems::PrepareResources)
                        .after(prepare_atmosphere_textures),
                    prepare_atmosphere_probe_bind_groups.in_set(RenderSystems::PrepareBindGroups),
                    queue_atmosphere_probe_pipelines // Problematic line here
                        .in_set(RenderSystems::Queue)
                        .after(init_atmosphere_probe_layout),
                    prepare_atmosphere_transforms.in_set(RenderSystems::PrepareResources),
                    prepare_atmosphere_bind_groups.in_set(RenderSystems::PrepareBindGroups),
                ),
            )

I'm not really familiar with this code, so I don't really know how to fix it, but here's an issue so somebody who knows how to can.

I also saw some other suspicious calls in massif, but one step at a time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-RenderingDrawing game state to the screenC-BugAn unexpected or incorrect behaviorC-PerformanceA change motivated by improving speed, memory usage or compile timesS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions