Skip to content

fix(scene-manager): dispose GPU resources on event switch#783

Merged
EdwardMoyse merged 1 commit intoHSF:mainfrom
goyalpalak18:fix/gpu-memory-leak-event-switching
Jan 24, 2026
Merged

fix(scene-manager): dispose GPU resources on event switch#783
EdwardMoyse merged 1 commit intoHSF:mainfrom
goyalpalak18:fix/gpu-memory-leak-event-switching

Conversation

@goyalpalak18
Copy link
Copy Markdown
Contributor

Description

This PR addresses a critical memory leak that occurs when switching between events or clearing event data.

While the current implementation of clearEventData() successfully removes objects from the Three.js scene graph, it doesn't notify the WebGL renderer that the underlying GPU resources (geometries, materials, and textures) are no longer needed. In Three.js, these resources are not tracked by the JavaScript Garbage Collector and must be disposed of explicitly to free up VRAM.

Without this fix, every event switch causes a linear increase in GPU memory usage. For users browsing through large ATLAS/CMS events or running the display for extended periods, this inevitably leads to a WebGL context loss or a total browser tab crash.

Changes

  • Refactored clearEventData: Now ensures that every object is thoroughly "cleaned" before being detached from the scene.
  • Added disposeObject3D helper:
  • Recursively traverses the Object3D hierarchy.
  • Calls .dispose() on all BufferGeometry instances.
  • Handles Material disposal (including support for Material[] arrays).
  • Texture Cleanup: Iterates through all standard material maps (map, alphaMap, normalMap, etc.) to ensure textures are removed from GPU memory.

How to verify

  1. Open the Phoenix application and monitor the browser's performance (e.g., Chrome DevTools -> Three dots -> More Tools -> Performance Monitor).
  2. Watch the GPU Memory or JS Heap (indirectly affected).
  3. Load a large event and switch events 20–30 times.
  4. Before fix: GPU memory climbs steadily until the tab becomes sluggish or crashes.
  5. After fix: GPU memory should return to a stable baseline shortly after each clearEventData() call.

Impact

  • Stability: Eliminates "Rats! WebGL hit a snag" errors during long analysis sessions.
  • Performance: Improves frame rates for users on low-end hardware or integrated GPUs.
  • UX: Allows physicists to browse hundreds of events in a single session without needing to refresh the page.

Signed-off-by: goyalpalak18 <goyalpalak1806@gmail.com>
@goyalpalak18
Copy link
Copy Markdown
Contributor Author

Hi @EdwardMoyse @9inpachi , I’ve noticed the display can become unstable or crash after switching through multiple events—this is due to GPU memory not being released by Three.js automatically. I’ve implemented a recursive disposal helper in SceneManager to explicitly free up geometries, materials, and textures.

I've verified the fix with the Performance Monitor; VRAM now stays stable instead of climbing linearly. Ready for a review when you have a moment!

@EdwardMoyse EdwardMoyse merged commit 330c718 into HSF:main Jan 24, 2026
2 checks passed
@EdwardMoyse
Copy link
Copy Markdown
Collaborator

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants