memray icon indicating copy to clipboard operation
memray copied to clipboard

Flamegraph report: add buttons to jump to peak RSS or peak memory usage

Open nmusolino opened this issue 5 months ago • 2 comments

Is there an existing proposal for this?

  • [x] I have searched the existing proposals

Is your feature request related to a problem?

A common use case for memray is to understand the allocations that contribute to peak heap usage or peak RSS. Application developers may want to diagnose and ultimately reduce peak RSS or peak heap usage, so that their programs can run under a certain memory threshold.

When examining a temporal flamegraph, it can be difficult to "manually" locate the peak RSS usage, especially if the scale of the RSS timeseries is much smaller than the scale of the heap size.

Image

The documentation for temporal flamegraphs says (emphasis added):

These temporal reports can be used to gain fine grained insight into how your process was using memory at any point during its run, which can be invaluable for understanding its memory usage patterns.

In practice, many users care about how the process was using memory at peak usage, rather than "any point".

Describe the solution you'd like

It would be very helpful if the HTML report provided buttons like "Zoom to peak RSS" and "Zoom to peak heap." Each button would set the focused interval (i.e. would move the sliders that control which allocations are displayed) to an interval containing the peak RSS or heap usage, respectively.

There are some decisions that would need to be made to implement this feature.

  • The initial duration of the focused interval: minimal (i.e. measurement granularity of ~10 milliseconds), a fixed duration like X milliseconds, or a fraction of the overall run?
  • How to handle cases where heap usage or RSS reaches a maximal value at multiple timestamps? For contiguous maximal values, it could expand the focused interval to cover the maximal values; for non-contiguous values, the button could arbitrarily choose the first or last, or cycle through all.

A more general version of this feature would be a button to cycle through peaks in heap usage or RSS. The documentation of scipy.find_peaks() illustrates the kinds of parameters that could be specified to identify peaks, although I'm not sure if one universal set of parameters would be "good enough" for general use.

Alternatives you considered

Currently, non-temporal flamegraph shows allocations at the peak heap usage. One alternative could be to add a flag like --peak-rss to the non-temporal flamegraph that instead focuses on the moment of peak RSS.

nmusolino avatar Jun 25 '26 15:06 nmusolino

I would like to work on the two peak-navigation buttons with Codex assistance. Proposed initial behavior: select the first global maximum (deterministic for ties), zoom to its sampling interval, and retain the existing slider/flamegraph behavior. For heap reports I would use the recorded per-interval high-water marks when available; RSS would use the sampled resident sizes. Empty captures would disable the buttons, and a single sample would still have a nonzero display interval.

I will add tests for first/last peaks, equal maxima and empty/single-sample captures, and check the generated report in a browser. Please let me know if you prefer a different initial interval or tie behavior; I can keep the implementation ready while the interaction design is discussed.

Shubham-Padkonde avatar Sep 17 '26 00:09 Shubham-Padkonde

The proposed implementation is ready for discussion in this branch, commit 8be11c9. I have held off opening a PR pending the maintainer discussion requested in CONTRIBUTING.md.

Validation: 24 JavaScript tests (including eight new peak/range cases), 47 Python reporter/template tests, and all applicable pre-commit checks pass. The report bundle builds. I generated an actual capture and checked both buttons in normal and --leaks temporal reports in a browser: the slider range changes and the corresponding flame graph updates. Documentation explains the sampling interval, earliest-tie rule and retained report semantics; selecting peak RSS does not imply an RSS allocation breakdown.

Shubham-Padkonde avatar Sep 17 '26 01:09 Shubham-Padkonde