memray icon indicating copy to clipboard operation
memray copied to clipboard

Feature request: a memory budget for `memray run` that stops the target and finalizes the capture

Open scks1 opened this issue 1 month ago • 0 comments

Is there an existing proposal for this?

  • [x] I have searched the existing proposals

Is your feature request related to a problem?

Searched for memory limit / budget / stop proposals. The closest match is #221 (memory bandwidth reporting, a different feature). pytest-memray's limit_memory marker also exists, but it is a post-hoc check over a finished capture, for tests only; it cannot stop a running program.

When a profiled program is killed by the OOM killer (SIGKILL), tracking ends with no finalization: no complete capture, no attribution of the allocations that caused the death, and an exit visible to the caller only as death by signal 9. memray run in 1.20.0 has no option to stop the target at a memory budget (memray run --help contains no limit, budget, duration, or stop option).

Reproduction (memray 1.20.0, Linux, CPython 3.12). A script allocates retained 10 MiB chunks until stopped. Run under prlimit --as=268435456 (256 MiB address-space cap):

  • Plain run: reached 220 MiB allocated, died with MemoryError, exit 1. Left behind only its stdout log.
  • memray run under the same cap: died the same way at a tracked peak of 94.386 MB (119 allocations), exit 1. Nothing stopped the process. The capture was readable only because a Python-level MemoryError unwound through memray's wrapper and finalized the file; an actual OOM kill is SIGKILL, with no unwinding and no finalization.

Describe the solution you'd like

An option on memray run, e.g. --memory-limit <size>: the launcher watches the child's memory (RSS) and, when the budget is crossed, stops the child in a way that lets the tracker finalize the capture, produces the usual reporters' output from that capture, and exits with a distinctive status and a message naming the budget and the peak observed.

Prototype evidence: I built a working prototype on the public API (subprocess child running the target under an in-process Tracker; a parent watchdog polling the child's RSS; on breach, SIGTERM, which the child converts into a clean tracker exit so the capture flushes; reports generated with memray stats / memray flamegraph). With a 128 MiB budget on the allocating script above:

  • Target stopped at 134.8 MiB peak observed RSS; runner exit code 75, with a one-line stderr message naming the budget, its source, and the peak.
  • The finalized capture attributes the breach precisely: 115.569 MB total allocated, peak 115.357 MB, with 115.343 MB in 11 allocations at the allocating function and line.
  • An under-budget program passes through untouched: its exit code and stdout are unchanged.

The budget source in the prototype resolves as: explicit flag, then the cgroup memory limit when readable and finite (the enforced limit is what will actually kill the job), then a stated default. Happy to share the prototype code, or to adapt it into a PR if the direction is welcome.

Known limits of the prototype, stated plainly: RSS polling overshoots (6.8 MiB at a 200 MiB/s allocation rate); a target that ignores SIGTERM degrades to the OOM outcome after a grace period; RSS includes the interpreter, the tracker, and native children, while the capture attributes only the tracked process.

Alternatives you considered

  • pytest-memray's limit_memory: post-hoc and tests-only, as noted above.
  • External supervision (systemd MemoryMax, cgroup limits): kills the process without finalizing the capture and provides no attribution.
  • An in-process self-limit through the Tracker API: a process cannot reliably finalize its own capture while being killed for memory the tracker itself is using. The watchdog belongs in the launcher, which is why memray run is the natural home for this.

Disclosure: this issue was written and filed by Árbol, an AI agent, at the direction of the account holder (Simon Koch-Sultan), following the recommendation in the prototype's report, which he reviewed. The reproduction and prototype are the agent's work; every number above comes from runs recorded on 2026-10-30 (memray 1.20.0, CPython 3.12.15).

scks1 avatar Oct 30 '26 14:10 scks1