Skip to content

⚡ [Performance] Fix Canvas Animation Memory Leak in BiologyView#2

Draft
Sir-Ripley wants to merge 1 commit intomainfrom
fix-biologyview-memory-leak-3510052348411773146
Draft

⚡ [Performance] Fix Canvas Animation Memory Leak in BiologyView#2
Sir-Ripley wants to merge 1 commit intomainfrom
fix-biologyview-memory-leak-3510052348411773146

Conversation

@Sir-Ripley
Copy link
Owner

@Sir-Ripley Sir-Ripley commented Mar 16, 2026

💡 What:
Updated the animation frame loop inside the useEffect of components/BiologyView.tsx to re-assign the id variable on each requestAnimationFrame call.

🎯 Why:
Previously, id was assigned only once and did not track subsequent frames created within the anim function. When the component unmounted or re-rendered, cancelAnimationFrame received an outdated or invalid ID, leaving the newest recursive requestAnimationFrame loop running indefinitely. This caused a memory and CPU leak where orphaned animation loops piled up over multiple renders.

📊 Measured Improvement:
Because this involves React's unmount/re-render lifecycle and orphaned browser animation frames, standard runtime profiling of a single cycle doesn't capture the compounding leak. The change acts as a structural prevention mechanism (O(1) loops vs O(n) loops per render), eliminating the infinite growth of unmanaged callbacks and the associated continuous UI thread tax, which directly bounds CPU overhead to the single active component instance.


PR created automatically by Jules for task 3510052348411773146 started by @Sir-Ripley

Summary by Sourcery

Bug Fixes:

  • Prevent a memory and CPU leak in BiologyView by ensuring the animation frame ID is updated on each frame so the recursive loop is properly cancelled.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sourcery-ai
Copy link

sourcery-ai bot commented Mar 16, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR fixes a memory/CPU leak in BiologyView’s canvas animation loop by ensuring the requestAnimationFrame ID is tracked across all recursive frames and properly cancelled on unmount.

Sequence diagram for updated BiologyView canvas animation lifecycle

sequenceDiagram
  participant BiologyView
  participant Browser

  BiologyView->>Browser: requestAnimationFrame(anim)
  Browser-->>BiologyView: id0
  BiologyView->>Browser: store id0 in id

  loop On_each_frame
    BiologyView->>Browser: requestAnimationFrame(anim)
    Browser-->>BiologyView: idN
    BiologyView->>Browser: update id with idN
  end

  BiologyView-->>Browser: cancelAnimationFrame(id) on unmount_or_rerender

  alt Previous_behavior
    note over BiologyView,Browser: id was const and not updated inside anim
    BiologyView-->>Browser: cancelAnimationFrame(stale_id)
    Browser-->>BiologyView: newest animation loop continues running
  else New_behavior
    note over BiologyView,Browser: id is let and updated on each frame
    BiologyView-->>Browser: cancelAnimationFrame(current_id)
    Browser-->>BiologyView: active animation loop is stopped
  end
Loading

File-Level Changes

Change Details Files
Ensure the animation frame ID is updated on every recursive requestAnimationFrame call so cleanup cancels the correct frame.
  • Changed the animation loop to assign the return value of requestAnimationFrame(anim) to the id variable inside the anim function.
  • Updated the initial animation frame call to use a mutable variable (let id) so it can be reassigned on subsequent frames.
  • Kept the cleanup function to call cancelAnimationFrame(id), which now references the latest active animation frame.
components/BiologyView.tsx

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@netlify
Copy link

netlify bot commented Mar 16, 2026

Deploy Preview for qag-oracle failed.

Name Link
🔨 Latest commit 71cf768
🔍 Latest deploy log https://app.netlify.com/projects/qag-oracle/deploys/69b877c0425c3000072d2a63

@vercel
Copy link

vercel bot commented Mar 16, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
qag-all Ready Ready Preview, Comment Mar 16, 2026 9:36pm

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.

1 participant