Skip to content

Commit 680c994

Browse files
brianreavismockersf
authored andcommitted
Fix TextureCache memory leak and add is_empty() method (#14480)
# Objective Fix a memory leak in `TextureCache` caused by the internal HashMap never having unused entries cleared. This isn't a giant memory leak, given the unused entries are simply empty vectors. Though, if someone goes and resizes a window a bunch, it can lead to hundreds/thousands of TextureDescriptor keys adding up in the hashmap – which isn't ideal. ## Solution - Only retain hashmap entries that still have textures. - I also added an `is_empty()` method to `TextureCache`, which is useful for 3rd-party higher-level caches that might have individual caches by view entity or texture type, for example. ## Testing - Verified the examples still work (this is a trivial change)
1 parent c4ea477 commit 680c994

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

crates/bevy_render/src/texture/texture_cache.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,22 @@ impl TextureCache {
8282
}
8383
}
8484

85+
/// Returns `true` if the texture cache contains no textures.
86+
pub fn is_empty(&self) -> bool {
87+
self.textures.is_empty()
88+
}
89+
8590
/// Updates the cache and only retains recently used textures.
8691
pub fn update(&mut self) {
87-
for textures in self.textures.values_mut() {
92+
self.textures.retain(|_, textures| {
8893
for texture in textures.iter_mut() {
8994
texture.frames_since_last_use += 1;
9095
texture.taken = false;
9196
}
9297

9398
textures.retain(|texture| texture.frames_since_last_use < 3);
94-
}
99+
!textures.is_empty()
100+
});
95101
}
96102
}
97103

0 commit comments

Comments
 (0)