Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

egui image loader uses too much ram #5439

Open
hacknus opened this issue Dec 5, 2024 · 3 comments
Open

egui image loader uses too much ram #5439

hacknus opened this issue Dec 5, 2024 · 3 comments
Labels
bug Something is broken

Comments

@hacknus
Copy link
Contributor

hacknus commented Dec 5, 2024

We are working on a file-dialog with egui, and we want to browse through files while displaying a preview image of the selected file using egui-loaders.

Describe the bug

When loading images with the egui loaders I see a huge memory increase.
After loading 10 images (each about 1 MB in size) the memory increases from 140 MB to 1.1 GB.

To Reproduce
Steps to reproduce the behavior:
cargo.toml:

[dev-dependencies]
eframe = { version = "0.29.1", default-features = false, features = [
    "glow",
    "persistence",
] }
egui-file-dialog = { path = "." , features = ["information_view"]}
egui_extras = { version = "0.29", features = ["all_loaders"] }
# required by the egui loaders
image = { version = "0.25.5", features = ["bmp", "jpeg", "gif", "png", "tiff", "rayon"] }

But it also happens with the latest version on master.

let image = egui::Image::new(format!(
    "file://{}",
   path
));
ui.vertical_centered(|ui| {
    ui.add(image.max_height(ui.available_width()));
});

Expected behavior
I would assume that the RAM only increases in the same order as the size of the loaded files, not an order of magnitude more.

Desktop (please complete the following information):

  • OS: macOS 14.6.1
  • Version 29
@lucasmerlin
Copy link
Collaborator

You have to keep in mind that egui decodes the image, so you can't really assume the memory size will match the file size.
Taking the image from the screenshot in fluxxcode/egui-file-dialog#184 as an example, which has 2880x1800, the size in memory would be at least 2880 * 1800 * 4 * 2 = 41.472.000 (w * h * channels * (cpu mem + gpu mem)) or ~41 MB. So loading 10 images with that resolution should take at least 400MB.
If you only ever show one image at a time you could handle the image loading manually (e.g. using Context::load_texture) and free them as soon as the next image is shown, which should improve memory usage.

egui-file-dialog looks really great!

@hacknus
Copy link
Contributor Author

hacknus commented Dec 5, 2024

Yeah, that kinda adds up.
Is there a way of reducing the size in egui? Especially in our case, but probably also in many others, not the full size is required. Would certainly be a nice feature!

The current workaround is to keep track of the loaded files and use the ui.ctx().forget_image() function to delete the last one, once we load more than 10. We could reduce this number, but it is nice to have some recent images in the buffer.

@lucasmerlin
Copy link
Collaborator

The easiest would probably be using image::thumbnail. Since egui uses image internally this shouldn't cause extra dependencies. Would be nice to have something like this in egui itself, maybe as part of the image loader api but it's not something supported today.

@hacknus hacknus changed the title egui image loaders uses too much ram egui image loader uses too much ram Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken
Projects
None yet
Development

No branches or pull requests

2 participants