Memory leak when translating/transforming Mesh2d and Sprites on iOS
- Dominant language
- Rust
- Stars
- 48.2k
- Forks
- 4.8k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 171
Description
## Bevy version and features
`bevy = {version = "0.18.1", default-features = false, features = ["2d"]}`
Building for iOS version 26.0 on a macbook air with M4 chip.
## What you did
I'm working on a game that is targeted for iOS and finding memory leaks despite not spawning any new entities. I've created an isolated example that reproduces the issue. This example generates 250 squares and randomly moves them around the screen ad-infinitum. This code can be used in the mobile example inside `lib.rs`.
```
use bevy::{
prelude::*,
winit::WinitSettings,
window::*,
render::*,
render::settings::*,
};
fn superloop(
window: Query<&Window>,
squares: Query<&mut Transform>)
{
let w_dim = window.single().unwrap().resolution.size();
for mut transform in squares
{
let x = rand::random_range(-w_dim.x/2.0..w_dim.x/2.0);
let y = rand::random_range(-w_dim.y/2.0..w_dim.y/2.0);
transform.translation.x = x;
transform.translation.y = y;
}
}
fn setup(mut commands: Commands,
window: Query<&Window>,
mut meshes: ResMut>,
mut materials: ResMut>){
let w_dim = window.single().unwrap().resolution.size();
commands.spawn(Camera2d);
commands.insert_resource(ClearColor(Color::hsl(0.0,0.0,0.0)));
for _idx in 0..250
{
let x = rand::random_range(-w_dim.x/2.0..w_dim.x/2.0);
let y = rand::random_range(-w_dim.y/2.0..w_dim.y/2.0);
let rect = Rectangle::new(10.0, 10.0);
let colour = Color::hsl(1.0,1.0,1.0);
let colour_mat = materials.add(colour);
commands.spawn((
Mesh2d(meshes.add(rect)),
MeshMaterial2d(colour_mat.clone()),
Transform::from_xyz(x,y,0.0),
));
}
}
#[bevy_main]
pub fn main() {
let mut app = App::new();
app.add_plugins((DefaultPlugins
.set(
WindowPlugin{
primary_window: {
Some(
Window{
present_mode:PresentMode::Fifo,
mode:WindowMode::BorderlessFullscreen(MonitorSelection::Current),
..default()
}
)},
..default()
}
)
.set(
RenderPlugin
{
render_creation:
RenderCreation::Automatic(
WgpuSettings{
memory_hints:MemoryHints::MemoryUsage,
..default()
}
),
..default()
}
)))
.insert_resource(WinitSettings::mobile())
.add_systems(Startup, setup)
.add_systems(Update, superloop)
.run();
}
```
## What went wrong
I can see when using the debugger that memory is leaking, if I leave the the app running for longer it eventually consumes all available ram and crashes the system:
I tested the same program on my laptop running Arch Linux (x86) and the memory usage stays stable, so I think it is something specific to the iOS build.
Contributor guide
Research direction
Place the provided reproduction in the mobile example's lib.rs and run it on iOS with 250 moving Mesh2d squares, then compare its memory behavior with the reported Arch Linux run. Trace the iOS rendering path to identify why memory grows during Transform updates; done means the reproduction runs without unbounded memory growth and the behavior is covered by a regression check where appropriate.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ios, rust
- Domain
- game-dev, mobile-dev, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100