bevyengine/bevy

Huge amount of data in vectors will not be released

Closed this issue · 4 comments

Bevy version

0.13.2
The release number or commit hash of the version you're using.

`AdapterInfo { name: "NVIDIA GeForce RTX 2070", vendor: 4318, device: 7938, device_type: DiscreteGpu, backend: Vulkan }`

What you did

fn my_function()->Vec<BigStuff> {
    let big_stuff_vector:Vec<BigStuff> = create_stuff();
    vec![] //big_stuff_vector is never used 
}

Calling this function (only from a bevy context, a menu button for example) causes the "big_stuff_vector" to take up memory, even it is never being used.
It contains about 1-2 GB of fairly deeply nested structures. It contains no pointers or references, or anything else like that. Only standard bevy enums/structs. There is no global variables referencing any of it.
After running this code 3-4 times, the computer runs out of memory and crashes.

I found a workaround, by using "mimalloc" as global allocator. This however would not run without crash unless I disabled dynamic_linking in Cargo.toml, so not the best workaround.


## What went wrong
Computer runs out of memory

- workarounds that you used
Use "mimalloc" as global allocator and disable dynamic_linking in Cargo.toml

Could you provide some additional information regarding the exact context you're using? For example, is this running inside a system?

use bevy::prelude::*;

fn my_function_that_causes_issues() { /* ... */ }

fn my_system() {
    let _ = my_function_that_causes_issues();
}

fn main() {
    App::new()
        .add_systems(Update, my_system)
        .run();
}

At a guess, it sounds like you might be storing highly fragmented data, making it (eventually) impossible to allocate your large structure. This video from fasterthanlime does a pretty good job of going over this particular type of issue.

As to why it's only present when run in a Bevy context: Bevy may be a sufficiently complex system that its allocations are hemming in your available chunks of memory.

It's not running directly in a system, but a system calls the function. Thanks for the link, will investigate this further.

So in any case, it's not a bevy thing, I'd assume.

I believe so. I suspect this is a more fundamental issue in your project that only exposes itself when run with Bevy (or a similar project).