fix: Add periodic Kitty image garbage collection during scroll - #432
Closed
kaantradelab wants to merge 1 commit into
Closed
fix: Add periodic Kitty image garbage collection during scroll#432kaantradelab wants to merge 1 commit into
kaantradelab wants to merge 1 commit into
Conversation
Problem: When using Kitty graphics protocol, orphan images accumulate in `kittyGraphicsState.imagesById` because `cleanupUnusedKittyImages()` is only called on explicit delete commands. During long-running terminal sessions with heavy output (e.g., CI/CD logs, AI agent sessions), this causes memory to grow unbounded. Observed: 11GB+ memory usage, terminal freeze, inability to scroll. Solution: - Add counter-based garbage collection in `scroll()` - Track lines scrolled since last cleanup - Trigger `cleanupUnusedKittyImages()` every 500 lines when buffer is trimmed - Change function visibility from `private` to `internal` Results: - Memory usage reduced from 11GB+ to ~400MB (96%+ improvement) - Terminal remains responsive during long sessions Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Owner
|
Oops, thank you so very much for tracking this! Let me see if I can do something a little more aggressive, rather than doing a cycle scan every N scrolled lines, if that does not pan out, I will merge your PR. |
Owner
|
Btw, the reason I do not want to do a collection after the number of lines is both that it would add another layer of uncertainty and jumps while I am profiling the performance and because it would probably break the kitty protocol. Since reference counting does not work (as this is part of the design), what I am going to do is add an eviction of data after certain size. |
Owner
|
Ok, this is what I am doing instead: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem:
When using Kitty graphics protocol, orphan images accumulate in
kittyGraphicsState.imagesByIdbecausecleanupUnusedKittyImages()is only called on explicit delete commands.During long-running terminal sessions with heavy output (e.g., CI/CD logs, AI agent sessions), this causes memory to grow unbounded. Observed: 11GB+ memory usage, terminal freeze, inability to scroll.
Solution:
scroll()cleanupUnusedKittyImages()every 500 lines when buffer is trimmedprivatetointernalResults: