ray icon indicating copy to clipboard operation
ray copied to clipboard

[Bug] Tune driver RSS grows linearly with trial count due to orphaned logger state

Open goingforstudying-ctrl opened this issue 5 months ago • 0 comments

Was running a large hyperparam sweep with tune.run() and noticed the driver process RSS just kept climbing. After a few thousand trials it was eating multiple GBs and eventually got killed by the OOM killer.

I dug into the per-trial accumulators and found three separate leaks layered on top of each other:

  1. CSVLoggerCallback keeps a _trial_continue dict to know whether to write a CSV header. The entry is never removed when the trial ends, so it grows linearly with the number of trials. Same issue in JsonLoggerCallback with _trial_configs.

  2. TuneController queues scheduler decisions in _queued_trial_decisions, but if a trial stops or fails outside the normal result path (e.g. via stop_trial() or an exception), the queued entry never gets popped by _maybe_execute_queued_decision. It just sits there forever.

  3. Trial.get_json_state() serializes trial state with indent=2 for pretty printing. This is the largest per-trial blob — it's cached in memory and rewritten to the experiment-state checkpoint file every period. Dropping the indentation saves about 25-30% on both the retained footprint and the on-disk experiment state. Since this JSON is machine-read anyway, the pretty formatting is just waste.

The first two are straightforward — just clean up the dict entries in log_trial_end() and _schedule_trial_stop(). The third is a one-line change to remove indent=2 from json.dumps.

I also added a test file (test_logger_memory_leak.py) that spins up a bunch of mock trials, runs them through the loggers, and asserts the internal dicts are empty afterwards. Caught all three issues during development.

Not 100% sure if removing indent=2 breaks any downstream tools that parse the experiment state checkpoint as plain text. From what I can tell it's always loaded back via json.loads, so it should be fine. Let me know if there's a tool I'm missing.

This is the same thing reported in #64231.

goingforstudying-ctrl avatar Jun 25 '26 06:06 goingforstudying-ctrl