Skip to content

Fix TextureCache memory leak and add is_empty() method#14480

Merged
alice-i-cecile merged 1 commit intobevyengine:mainfrom
brianreavis:pr-texture-cache-memleak
Jul 27, 2024
Merged

Fix TextureCache memory leak and add is_empty() method#14480
alice-i-cecile merged 1 commit intobevyengine:mainfrom
brianreavis:pr-texture-cache-memleak

Conversation

@brianreavis
Copy link
Contributor

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)

@alice-i-cecile
Copy link
Member

@re0312 does this fix #14463 for you?

@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen C-Performance A change motivated by improving speed, memory usage or compile times labels Jul 25, 2024
@alice-i-cecile alice-i-cecile added this to the 0.14.1 milestone Jul 25, 2024
@re0312
Copy link
Contributor

re0312 commented Jul 26, 2024

@re0312 does this fix #14463 for you?

It doesn't seem to resolve the problem, as memory leaks are still occurring on my machine.

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jul 27, 2024
Merged via the queue into bevyengine:main with commit 724fe49 Jul 27, 2024
mockersf pushed a commit that referenced this pull request Aug 2, 2024
# 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Rendering Drawing game state to the screen C-Performance A change motivated by improving speed, memory usage or compile times

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants