Skip to content

Memory Leak When Loading Scenes #12746

@LordRatte

Description

@LordRatte

Bevy version

bevy = "0.13.0"

[Optional] Relevant system information

  • cargo 1.78.0-nightly (7065f0ef4 2024-03-12)
  • MacOS 14.4 (23E214)
  • Apple M1 CPU
  • AdapterInfo { name: "Apple M1", vendor: 0, device: 0, device_type: IntegratedGpu, driver: "", driver_info: "", backend: Metal }
  • Also tested on Wasm in Firefox with the same result.

What you did

I have been having a problem with a memory leak in my app. Here is a minimal example that reproduces the bug.

use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .insert_resource(Objects::default())
        .add_systems(Startup, setup)
        .add_systems(Update, update)
        .run();
}

#[derive(Resource, Default)]
struct Objects {
    mesh: Handle<Mesh>,
    material: Handle<StandardMaterial>,
    scene: Handle<Scene>,
}

#[derive(Component)]
struct Piece;

fn setup(
    mut commands: Commands,
    mut objects: ResMut<Objects>,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
    asset_server: Res<AssetServer>,
) {
    commands.spawn(Camera3dBundle {
        transform: Transform::from_xyz(7., 15., -9.5).looking_at(Vec3::ZERO, Vec3::Y),
        camera: Camera {
            clear_color: ClearColorConfig::Custom(Color::rgb(0., 0.7, 0.89)),
            ..default()
        },
        ..default()
    });

    objects.mesh = meshes.add(Cuboid::new(1.0, 1.0, 1.0));
    objects.material = materials.add(Color::rgb_u8(124, 144, 255));
    objects.scene = asset_server.load(format!("tree_1.glb#Scene0"));
}

fn update(mut commands: Commands, objects: Res<Objects>, delete_query: Query<Entity, With<Piece>>) {
    for entity in delete_query.iter() {
        commands.entity(entity).despawn_recursive();
    }

    for i in 1..1000 {
        commands
            .spawn(PbrBundle {
                mesh: objects.mesh.clone(),
                material: objects.material.clone(),
                transform: Transform::from_xyz(0.0, 0.0, i as f32),
                ..default()
            })
            .insert(Piece);

        // The previous expression doesn't seem to have an effect
        // but the memory steadily increases when the following expression is included.
        commands
            .spawn(SceneBundle {
                scene: objects.scene.clone(),
                transform: Transform::from_xyz(0.0, 1., i as f32).with_scale(Vec3::splat(0.05)),
                ..Default::default()
            })
            .insert(Piece);
    }
}

In other words, entities are cleared and then added again, repeatedly. The problem still happens if there is some time between runs (e.g. once every second) but it just happens a bit slower.

What went wrong

The app memory usage steadily increases.

Additional information

It may be that some part of the loaded scene is not being properly deleted. I am not sure.

Potentially related to #9035 but it seems to not be an exclusively MacOS issue, given that I tried it on Wasm as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ScenesSerialized ECS data stored on the diskC-BugAn unexpected or incorrect behaviorP-HighThis is particularly urgent, and deserves immediate attention

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions