Skip to content

perf(memory): find why footprint stays high after a large result is closed #4557

Done

Description

@datlechin

Problem

After a large result is closed, the app does not return to the memory it started with. Measured on SELECT * FROM trips_1m (1M x 20, Execute Without Limit) on PostgreSQL, phys_footprint:

  • 573 MB before the run. That figure came from an earlier 1M-row result that had already been closed.
  • 1,262 MB with the result open.
  • 277 MB after closing the tab.

So the first closed result still held at least 296 MB (573 - 277) when the second run started. That the footprint then fell below 573 MB suggests the second run reused that memory and gave part of it back. The MySQL run started at 257 MB. A fresh launch's footprint was not recorded in this run. This is an investigation first: the cause is either a live reference that keeps rows alive, or the allocator keeping freed pages.

Cause

Unknown. What the code shows:

  • A user close (closeTabsByUser, apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TabClosing.swift:10-24) removes the tab, and the tabs setter unregisters its session (apple/TablePro/Models/Query/QueryTabManager.swift:250-256, apple/TablePro/Models/Query/TabSessionRegistry.swift:20-22), on the main actor. Other paths clear rows with removeTableRows (TabSessionRegistry.swift:76-82).
  • Other holders of a result's rows or of per-row state, each to check after a close:
    • the tab's ResultSet objects (apple/TablePro/Models/Query/ResultSet.swift:30), which share the session buffer while active (apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableRowsMutation.swift:149-155);
    • the grid model and display caches in dataGridEntries, pruned by cleanupTabCaches (MainContentCoordinator+TabClosing.swift:67-70);
    • RowPositions, which holds a [RowID: Int] entry per row after a sort or a mid-result insert (apple/TablePro/Models/Query/RowPositions.swift:13, :35-43).
  • A result of this shape is about 1M row buffers of 896 B, 2M heap strings and a 32 MB row index, all small and medium malloc blocks. phys_footprint keeps counting those regions' dirty pages until malloc hands them back to the system.

Change

First measure, then fix what the measurement names.

  1. Fresh launch: connect, then record footprint <pid>, vmmap --summary <pid> and heap <pid>.
  2. Run the query, then record the same three.
  3. Close the tab. Record again after 10 s and after 2 minutes.
  4. In heap, count _ContiguousArrayStorage<PluginCellValue>, StringStorage, TableRows and ResultSet instances.
    • If about 1M row arrays are still alive, a reference holds them. Take a memory graph (leaks <pid> --outputGraph=closed.memgraph) and follow one row array to its root in Xcode's memory graph debugger.
    • If the objects are gone but footprint stays high, compare the MALLOC_TINY, MALLOC_SMALL and MALLOC_NANO dirty sizes in vmmap. Then call malloc_zone_pressure_relief(NULL, 0) once from lldb and read footprint again. A drop means allocator retention.
  5. Repeat open and close 5 times. Footprint that grows per cycle is a leak. Footprint that levels off is retention.
  6. Run it once more on MySQL.

Then:

Overlaps #4453: L1 measures memory after a connection window closes (coordinators, workspaces, split views). It does not measure a closed tab's rows.

Measure

  • The table from steps 1 to 5 for PostgreSQL and MySQL, before and after the fix: footprint at launch, with the result, and 2 minutes after close.
  • Target: footprint 2 minutes after close is back at the pre-run level, within the spread of five repeated pre-run readings.

Part of the performance plan.

Activity

  1. added
    enhancement
    platform-macos
    on Nov 25, 2026
  2. datlechin commented on Nov 26, 2026

    @datlechin
    MemberAuthor

    #4598 adds the test this issue asks for on the live-reference branch: a closed tab's ResultSet (weak reference) and its row buffer (uniquely held after the close) are gone, for the last tab and for one of two, through the real run and close paths. Left: the footprint table (launch, with the result, 10 s and 2 min after close, five cycles, PostgreSQL and MySQL) and the malloc_zone_pressure_relief reading, which need the real app against a live server. The shared Mac ran out of disk during this run, so they were not taken. If #4598 is green, the objects are released at close and the rest is allocator retention for #4521.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementplatform-macos

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions