Delete WR images when dropping the ImageCacheStore (#36956)

This deletes images from WR when dropping the ImageCacheStore for a
WebView.

Testing: Run `./mach run --enable-experimental-web-platform-features
unsplash.com` and then open `about:memory` in a new tab. On Linux we end
up with ~30MB of WR images. Then close the unsplash.com tab and measure
memory again, it will down to ~1.25MB

Fixes: https://github.com/servo/servo/issues/25927

Signed-off-by: webbeef <me@webbeef.org>
This commit is contained in:
webbeef 2025-05-10 21:01:49 -07:00 committed by GitHub
parent 9c3069366e
commit e702bde9bf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,7 +7,7 @@ use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::sync::{Arc, Mutex};
use std::{mem, thread};
use compositing_traits::{CrossProcessCompositorApi, SerializableImageData};
use compositing_traits::{CrossProcessCompositorApi, ImageUpdate, SerializableImageData};
use imsz::imsz_from_reader;
use ipc_channel::ipc::IpcSharedMemory;
use log::{debug, warn};
@ -675,6 +675,20 @@ impl ImageCache for ImageCacheImpl {
}
}
impl Drop for ImageCacheStore {
fn drop(&mut self) {
let image_updates = self
.completed_loads
.values()
.filter_map(|load| match &load.image_response {
ImageResponse::Loaded(image, _) => image.id.map(ImageUpdate::DeleteImage),
_ => None,
})
.collect();
self.compositor_api.update_images(image_updates);
}
}
impl ImageCacheImpl {
/// Require self.store.lock() before calling.
fn add_listener_with_store(&self, store: &mut ImageCacheStore, listener: ImageResponder) {