Skip to content

rtk rg can consume over 50 GB by buffering and duplicating large search output #3106

Closed as duplicate

Description

@MeroZemory

RTK version 0.43.0 on Windows consumed more than 50 GB of private memory while processing one large rg result.

The offending command line was captured from the running process. Paths are normalized below.

rtk rg -n "load_workspace_dependencies|workspace_dependencies|models/list|/models|model_provider|wire_api|supports_tools|tool server" src docs issues "%USERPROFILE%\.codex" "%APPDATA%\npm\node_modules\@openai\codex"

Observed behavior

  • Working set was initially 21.111 GB and later reached 37.236 GB
  • Private memory reached 50.829 GB
  • Virtual memory reached 54.855 GB
  • The rg child had already exited when this was measured
  • rtk.exe had one thread and continued using CPU while post-processing the output
  • The parent process had also exited

I ran the same underlying rg argument shape separately and discarded stdout through a fixed 1 MB buffer.

  • Native rg stdout was 4,431,986,055 bytes, or 4.128 GiB
  • Native rg peak working set was 1,118.4 MB
  • Native rg peak private memory was 1,409.7 MB
  • RTK peak private memory was about 12.3 times the raw output size

The large output came from Codex session JSONL files under .codex. The relevant corpus contained about 3,450 JSONL files totaling 14.796 GiB. About 3,494 files and 49,000 long JSONL lines matched.

The current implementation appears to amplify memory in several places.

  • exec_capture_stdin uses Command::output, so all child stdout is buffered before filtering
  • String::from_utf8_lossy followed by into_owned creates an owned String copy
  • search.rs clones the complete stdout into raw_output
  • all matches are processed before the 200 result limit is applied
  • clean_line lowercases the complete line and may collect the complete line into Vec char, even when only a short result is retained
  • a full plain representation is rebuilt before RTK decides whether to use the capped output

Relevant source locations in commit 5a7880d

  • src/core/stream.rs lines 545 to 552
  • src/cmds/system/search.rs lines 397 to 405
  • src/cmds/system/search.rs lines 427 to 480
  • src/cmds/system/search.rs lines 629 to 670

This seems different from issue 897. That issue involved grep child processes remaining alive. In this case no child remained and rtk.exe itself held 50.829 GB while post-processing one command.

I also checked develop at f9d8c77. The relevant capture and duplication behavior is still present.

Expected behavior

RTK should keep memory bounded when filtering large search output. The result limit should apply while output is being read, or raw output should be streamed or spooled instead of retained and duplicated in memory.

Limitations

  • The exact offending command line was captured, but its original working directory was lost after the parent process exited. The relative src, docs, and issues paths therefore could not be reproduced exactly.
  • The .codex session files continue to change, so this is not a byte-for-byte replay of the original input.
  • I did not rerun the full command through RTK because it had already reached 50.829 GB and repeating it risked another system OOM. I reproduced the underlying 4.128 GiB native output with bounded streaming instead.
  • No heap profiler was attached, so the exact share of the 50.829 GB attributable to each allocation is not known. The source path and overall unbounded buffering behavior are confirmed.

Activity

  1. pszymkowiak commented on Jul 20, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 88%

    Summary

    rtk rg buffers, clones, and fully materializes child stdout before applying the 200-result cap, causing unbounded (50+ GB) memory use on large search corpora; needs streaming/early-capping in stream.rs and search.rs.

    📁 Relevant files
    • src/core/stream.rs
    • src/cmds/system/search.rs

    Triaged automatically by wshm · This is an automated analysis, not a human review.

  2. albatrossflyon-coder commented on Jul 21, 2026

    @albatrossflyon-coder
    Contributor

    Hi — I'd like to work on this too, same root cause/theme as #3107 (buffering full output before applying a cap instead of streaming/capping early). Planning to look at both together since the fix pattern is likely shared between stream.rs/search.rs here and the read path in #3107.

  3. added a commit that references this issue on Aug 27, 2026
    a91df0e
  4. KuSh commented on Sep 13, 2026

    @KuSh
    Collaborator

    Closing as a duplicate of #3392, which tracks the same problem with the more complete discussion. Please follow up there; if your case differs in a way that issue does not cover, comment here and it can be reopened.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions