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

Possible Race Condition Causing Large Textures to Stay in Memory After GC #556

Open
hiratariq opened this issue Dec 16, 2024 · 0 comments
Open
Labels

Comments

@hiratariq
Copy link

hiratariq commented Dec 16, 2024

Problem/Opportunity

Somewhere between changing the src and clip region for the next sprite sheet, the previous sprite sheet stays in memory.

Steps to reproduce

No response

Expected Behavior

previous spritesheet/texture is cleared from memory during gc

Actual Behavior

previous spritesheet/texture remains memory after gc

Notes (Optional)

We are building a thumbnail scrubber for media playback. For performance on low-end set-top boxes, we reuse elements that we assign ImageTexture to, avoiding frequent element creation. These elements are rotated to display (depending on the media position):

  • 7 thumbnails on-screen.
  • 6 off-screen thumbnails on either side (3 each side).

Implementation Details:

We use multiple sprite sheets for a single stream.
Each sprite sheet is approximately 4000x336, so clearing these during garbage collection is crucial.
To display the thumbnails, we frequently:

  • Enable clipping on the texture.
  • Swap the src of the texture.

Observed Issue:

When swapping the src for the next sprite sheet, the previous sprite sheet occasionally stays in memory. This appears to occur because the activeTextureCount in TextureSource remains >0, even though textures=Set(0).

Debugging Insights:

The problem starts in Texture.removeElement. When entering this function:

  • The texture’s activeCount is greater than 1.
  • The element.active is false.
    As a result:
  • The texture and element are removed successfully.
  • However, the texture’s activeCount is not reduced, leaving it activeTextureCount>0 in TextureSource.
  • This prevents the texture from being garbage collected.

Workaround:

As a preventative measure we have monkey patched TextureSource.isUsed function to return this._activeTextureCount > 0 && this.textures.size > 0; which works for now.

Assumed cause (Hypothesis):

It may be that this happens if a texture is set on an element that was active, but the element became inactive before the operation completed. The issue is reproducible only with multiple sprite sheets. It happens on all devices, including chrome on Macbook. It is tricky to identify the exact trigger.

@hiratariq hiratariq added the bug label Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant