From efa80a2d9afc704132edb1ebed639f451d82046c Mon Sep 17 00:00:00 2001 From: Christian Findlay <16697547+MelbourneDeveloper@users.noreply.github.com> Date: Thu, 19 Nov 2026 06:33:22 +1100 Subject: [PATCH 1/2] Delete the .NET heap graph each snapshot supersedes Every memory snapshot and diff kept a new numbered heap graph (about 16 MB each) until the session stopped, though only the latest is ever read. A session polled for days filled the output directory. capture now deletes the graph it replaces. [PROF-DOTNET-MEMORY] --- crates/lsp-profiler-dotnet/src/memory.rs | 18 ++++- .../lsp-profiler-dotnet/tests/dotnet_e2e.rs | 2 + .../tests/dotnet_e2e/graph_retention.rs | 68 ++++++++++++++++++ .../test_stop_tracemalloc.cpython-312.pyc | Bin 4616 -> 4616 bytes docs/plans/memory-leak-fixes-0.6.0.md | 1 + docs/specs/LSP-PROFILER-SPEC.md | 2 +- 6 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 crates/lsp-profiler-dotnet/tests/dotnet_e2e/graph_retention.rs diff --git a/crates/lsp-profiler-dotnet/src/memory.rs b/crates/lsp-profiler-dotnet/src/memory.rs index dc27182..7ed80df 100644 --- a/crates/lsp-profiler-dotnet/src/memory.rs +++ b/crates/lsp-profiler-dotnet/src/memory.rs @@ -3,7 +3,8 @@ //! A direct backend: every step runs one engine command and answers with the //! neutral payload. A snapshot keeps its heap graph in the output directory //! so `references` and `objects` re-open it instead of touching the target -//! again; the session's graphs are removed when it stops. +//! again. Each snapshot deletes the graph it supersedes, and the session's +//! directory is removed when it stops. use std::collections::HashMap; use std::path::{Path, PathBuf}; @@ -100,13 +101,15 @@ impl DotNetMemory { )); }; let mut peak_bytes = heap_bytes; + let mut superseded = None; self.remember(&context.session_id, |state| { - state.graph = Some(graph.clone()); + superseded = state.graph.replace(graph.clone()); state.heap_bytes = Some(heap_bytes); state.peak_bytes = state.peak_bytes.max(heap_bytes).max(live_bytes); peak_bytes = state.peak_bytes; }) .await; + discard(superseded).await; Ok(HeapSnapshot { snapshot_id: String::new(), current_bytes: live_bytes, @@ -118,7 +121,7 @@ impl DotNetMemory { }) } - /// Allocate a fresh numbered graph path without overwriting earlier snapshots. + /// Allocate a fresh numbered graph path, so a query on the previous graph never reads a half-written file. async fn next_graph_path(&self, session_id: &str) -> Result { let dir = self.output_dir.join(format!("heap-{session_id}")); tokio::fs::create_dir_all(&dir).await.map_err(|error| { @@ -282,6 +285,15 @@ impl MemoryBackend for DotNetMemory { } } +/// Delete a graph a newer snapshot replaced: only the latest is ever read. +async fn discard(superseded: Option) { + if let Some(graph) = superseded { + if let Err(error) = tokio::fs::remove_file(&graph).await { + warn!(%error, "cannot remove a superseded heap graph"); + } + } +} + /// Build a graph query with its target type and operation-specific result limits. fn graph_args(command: &str, graph: &Path, type_name: &str, limits: &[(&str, u32)]) -> Vec { let mut args = vec![ diff --git a/crates/lsp-profiler-dotnet/tests/dotnet_e2e.rs b/crates/lsp-profiler-dotnet/tests/dotnet_e2e.rs index 85a1f23..8b32719 100644 --- a/crates/lsp-profiler-dotnet/tests/dotnet_e2e.rs +++ b/crates/lsp-profiler-dotnet/tests/dotnet_e2e.rs @@ -18,6 +18,8 @@ #[path = "dotnet_e2e/availability.rs"] mod availability; +#[path = "dotnet_e2e/graph_retention.rs"] +mod graph_retention; mod support; use std::path::PathBuf; diff --git a/crates/lsp-profiler-dotnet/tests/dotnet_e2e/graph_retention.rs b/crates/lsp-profiler-dotnet/tests/dotnet_e2e/graph_retention.rs new file mode 100644 index 0000000..251c3dc --- /dev/null +++ b/crates/lsp-profiler-dotnet/tests/dotnet_e2e/graph_retention.rs @@ -0,0 +1,68 @@ +//! [PROF-DOTNET-MEMORY]: only the latest heap graph is ever read, so a session +//! polled for days must keep one graph on disk, not one per snapshot or diff. + +use std::path::Path; + +use super::{call, completed, methods, service, Fixture, FIXTURE, FIXTURE_TYPE}; +use serde_json::json; + +/// Graph-capturing steps, each superseding the graph before it. +const STEPS: [&str; 3] = [ + methods::MEMORY_SNAPSHOT, + methods::MEMORY_DIFF, + methods::MEMORY_SNAPSHOT, +]; + +/// Every heap graph file in the session's directory. +fn graphs(dir: &Path) -> Vec { + std::fs::read_dir(dir) + .expect("the session's heap graph directory") + .map(|entry| { + entry + .expect("a heap graph directory entry") + .file_name() + .to_string_lossy() + .into_owned() + }) + .collect() +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_new_snapshot_removes_the_graph_it_supersedes() { + let fixture = Fixture::start(FIXTURE); + let dir = tempfile::tempdir().expect("tempdir"); + let service = service(dir.path().to_path_buf()); + let started = call( + &service, + methods::MEMORY_START, + json!({ "pid": fixture.pid }), + ) + .await; + let id = started["memory_session_id"].as_str().expect("a session id"); + let session = json!({ "memory_session_id": id }); + let heap = dir.path().join(format!("heap-{id}")); + for step in STEPS { + let answer = call(&service, step, session.clone()).await; + assert!( + completed(&answer)["outcome"].is_object(), + "{step}: {answer}" + ); + let kept = graphs(&heap); + assert_eq!( + kept.len(), + 1, + "after {step} only the latest graph stays: {kept:?}" + ); + } + let refs = + json!({ "memory_session_id": id, "type_name": FIXTURE_TYPE, "limit": 5, "max_depth": 3 }); + let answer = call(&service, methods::MEMORY_REFERENCES, refs).await; + assert_eq!( + completed(&answer)["outcome"]["kind"], + "Refs", + "the latest graph still answers: {answer}" + ); + let _ = call(&service, methods::MEMORY_STOP, session).await; + assert!(!heap.exists(), "stop removes the session's graphs"); + service.shutdown().await; +} diff --git a/crates/lsp-profiler-python/tests/python/__pycache__/test_stop_tracemalloc.cpython-312.pyc b/crates/lsp-profiler-python/tests/python/__pycache__/test_stop_tracemalloc.cpython-312.pyc index 12554a94041d111f1e91e4ab6a61d09213d86aef..b6835bf3b895cf829fce42922c0fd8073cf5712f 100644 GIT binary patch delta 30 kcmeBB=}_T2&CAQh00cj#|IJe3-pDtRnbCRk0_F@p0EOrX+yDRo delta 30 kcmeBB=}_T2&CAQh00a$F|7Kn1+Q>JNnK60u0_F@p0Ee{+HUIzs diff --git a/docs/plans/memory-leak-fixes-0.6.0.md b/docs/plans/memory-leak-fixes-0.6.0.md index d0e4560..fcd0af3 100644 --- a/docs/plans/memory-leak-fixes-0.6.0.md +++ b/docs/plans/memory-leak-fixes-0.6.0.md @@ -28,6 +28,7 @@ Fix every leak in the v0.6.0 static review (four pi agents on GLM 5.3 plus a man | [PROF-UI-LIFETIME] | LOW decoration-type maps keyed by colour grew on every palette change: the palette is re-read per render | `pruneToPalette` disposes types whose colour the current palette no longer paints | `lsp-profiler-ui/src/test/suite/decoration-palette.test.ts` (9 types after 8 changes before the fix) | | [PROF-CPU-COOPERATIVE] | LOW cooperative tail thread: a header that arrived after `tail` gave up left the in-process sampler writing into a file nobody read | The thread writes the stop sentinel, allows the drain grace, then removes both files | `cooperative.rs::a_header_after_attach_gave_up_stops_the_writer_and_removes_the_file` | | [PROF-MEMORY-STOP] | LOW Python `stop_tracemalloc` left `gc.garbage`, the baseline snapshot, the `atexit` hook and the signal handlers in the debuggee | The stop script (now `src/memory/scripts/stop_tracemalloc.py`) releases all four | `lsp-profiler-python/tests/python/test_stop_tracemalloc.py`, run by `make test` in a real interpreter | +| [PROF-DOTNET-MEMORY] | Follow-up review on 6957145: every .NET `snapshot` and `diff` kept a new numbered heap graph (about 16 MB each) until `stop`, though only the latest is read | `capture` deletes the graph it supersedes | `lsp-profiler-dotnet/tests/dotnet_e2e/graph_retention.rs` (two graphs after one diff before the fix) | ## [MEM-060-NOT-LEAKS] Checked and left as they are diff --git a/docs/specs/LSP-PROFILER-SPEC.md b/docs/specs/LSP-PROFILER-SPEC.md index 9164cdc..7f37d5c 100644 --- a/docs/specs/LSP-PROFILER-SPEC.md +++ b/docs/specs/LSP-PROFILER-SPEC.md @@ -812,7 +812,7 @@ Only methods with visible sequence points are searched, so the generated factori | `gcCollect` | `gc` | `Gc`; `collected` is 1 when a collection was observed, `freed_bytes` is the drop against the last known heap size | | `stop` | none | `Ack`; deletes the session's graphs | -Graphs live under `/heap-/snapshot-.heapgraph.json`. A graph of half a million objects is about 16 MB. +Graphs live under `/heap-/snapshot-.heapgraph.json`. A graph of half a million objects is about 16 MB. Only the latest graph is ever read, so each `snapshot` or `diff` deletes the graph it supersedes once the new one is kept: a session polled for days holds one graph on disk. ### [PROF-DOTNET-GC] Induced collection From ceca15bd125c28d3a31c70d2321c29198876a36f Mon Sep 17 00:00:00 2001 From: Christian Findlay <16697547+MelbourneDeveloper@users.noreply.github.com> Date: Thu, 19 Nov 2026 06:33:40 +1100 Subject: [PATCH 2/2] Restore the tracked test bytecode --- .../test_stop_tracemalloc.cpython-312.pyc | Bin 4616 -> 4616 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/crates/lsp-profiler-python/tests/python/__pycache__/test_stop_tracemalloc.cpython-312.pyc b/crates/lsp-profiler-python/tests/python/__pycache__/test_stop_tracemalloc.cpython-312.pyc index b6835bf3b895cf829fce42922c0fd8073cf5712f..12554a94041d111f1e91e4ab6a61d09213d86aef 100644 GIT binary patch delta 30 kcmeBB=}_T2&CAQh00a$F|7Kn1+Q>JNnK60u0_F@p0Ee{+HUIzs delta 30 kcmeBB=}_T2&CAQh00cj#|IJe3-pDtRnbCRk0_F@p0EOrX+yDRo