Skip to content

Fixed memory leak in bindless material #19041

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

Merged
merged 1 commit into from
May 30, 2025

Conversation

SarthakSingh31
Copy link
Contributor

@SarthakSingh31 SarthakSingh31 commented May 3, 2025

Objective

Fixed #19035. Fixed #18882. It consisted of two different bugs:

  • The allocations where being incremented even when a Data binding was created.
  • The ref counting on the binding was broken.

Solution

  • Stopped incrementing the allocations when a data binding was created.
  • Rewrote the ref counting code to more reliably track the ref count.

Testing

Tested my fix for 10 minutes with the examples/3d/animated_material.rs example. I changed the example to spawn 51x51 meshes instead of 3x3 meshes to heighten the effects of the bug.

My branch: (After 10 minutes of running the modified example)
GPU: 172 MB
CPU: ~700 MB

Main branch: (After 2 minutes of running the modified example, my computer started to stutter so I had to end it early)
GPU: 376 MB
CPU: ~1300 MB

@SarthakSingh31 SarthakSingh31 changed the title Reworked ref counting to fix overcounting Fixed memory leak in bindless material May 3, 2025
@hukasu
Copy link
Contributor

hukasu commented May 3, 2025

nice

@TheCodeLamp
Copy link

TheCodeLamp commented May 3, 2025

Does this also fix #18808 ? Or is this only touching materials?

@hukasu
Copy link
Contributor

hukasu commented May 3, 2025

only materials

@alice-i-cecile alice-i-cecile added the C-Bug An unexpected or incorrect behavior label May 5, 2025
@alice-i-cecile alice-i-cecile added this to the 0.16.1 milestone May 5, 2025
@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels May 5, 2025
@alice-i-cecile alice-i-cecile requested a review from pcwalton May 5, 2025 04:34
Copy link
Contributor

@BenjaminBrienen BenjaminBrienen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"looks right"

@@ -1129,6 +1093,14 @@ where
.expect("Texture array should exist")
.insert(binding_resource_id, texture_view);
allocated_resource_slots.insert(bindless_index, slot);

if let Some(pre_existing_slot) = pre_existing_slot {
assert_eq!(*pre_existing_slot, slot);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be debug_assert_eq! ?

Copy link
Contributor Author

@SarthakSingh31 SarthakSingh31 May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this PR makes pre_existing_slot here kinda redundant. Its just a sanity check to make sure everything is working correctly.

I can make it a debug assertion if you think that would be better.

Instead of using the pre_existing_slot ideally we want to know if a allocation has actually been made (we should get this from the insert API). The pre_existing_slot is mostly a easy way to check that.

@hukasu
Copy link
Contributor

hukasu commented May 20, 2025

seems to have been fixed by another PR already

@BenjaminBrienen
Copy link
Contributor

BenjaminBrienen commented May 20, 2025

@SarthakSingh31 can you confirm that this issue still exists on main?

@hukasu
Copy link
Contributor

hukasu commented May 20, 2025

MY BAD, my test (#19311) was wrong, it still exists

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels May 30, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue May 30, 2025
Merged via the queue into bevyengine:main with commit d82d323 May 30, 2025
41 checks passed
mockersf pushed a commit that referenced this pull request May 30, 2025
# Objective

Fixed #19035. Fixed #18882. It consisted of two different bugs:
- The allocations where being incremented even when a Data binding was
created.
- The ref counting on the binding was broken.

## Solution

- Stopped incrementing the allocations when a data binding was created.
- Rewrote the ref counting code to more reliably track the ref count.

## Testing

Tested my fix for 10 minutes with the `examples/3d/animated_material.rs`
example. I changed the example to spawn 51x51 meshes instead of 3x3
meshes to heighten the effects of the bug.

My branch: (After 10 minutes of running the modified example)
GPU: 172 MB
CPU: ~700 MB

Main branch: (After 2 minutes of running the modified example, my
computer started to stutter so I had to end it early)
GPU: 376 MB
CPU: ~1300 MB
mockersf pushed a commit that referenced this pull request May 30, 2025
# Objective

Fixed #19035. Fixed #18882. It consisted of two different bugs:
- The allocations where being incremented even when a Data binding was
created.
- The ref counting on the binding was broken.

## Solution

- Stopped incrementing the allocations when a data binding was created.
- Rewrote the ref counting code to more reliably track the ref count.

## Testing

Tested my fix for 10 minutes with the `examples/3d/animated_material.rs`
example. I changed the example to spawn 51x51 meshes instead of 3x3
meshes to heighten the effects of the bug.

My branch: (After 10 minutes of running the modified example)
GPU: 172 MB
CPU: ~700 MB

Main branch: (After 2 minutes of running the modified example, my
computer started to stutter so I had to end it early)
GPU: 376 MB
CPU: ~1300 MB
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-Bug An unexpected or incorrect behavior S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Performance quickly drops when using get_mut on Assets<StandardMaterial> Animated Materials: Major memory leak
6 participants