Repository navigation
[serve] Freeze GC after replica initialization by default - #64742
Conversation
Add RAY_SERVE_FREEZE_GC_ON_STARTUP (default: 1). When enabled, each replica runs gc.collect() + gc.freeze() once user code finishes initializing, excluding long-lived startup objects from future GC scans. This reduces GC pauses and tail latency in the request path, mirroring the existing proxy-side GC optimizations (RAY_SERVE_ENABLE_PROXY_GC_OPTIMIZATIONS). Set RAY_SERVE_FREEZE_GC_ON_STARTUP=0 to disable (e.g. if the memory overhead of permanently frozen startup objects is a concern). Co-Authored-By: Claude Fable 5 <[email protected]> Signed-off-by: harshit-anyscale <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request introduces a feature flag RAY_SERVE_FREEZE_GC_ON_STARTUP to run garbage collection and freeze the GC at the end of replica initialization, aiming to reduce GC pauses during request handling. The reviewer noted that freezing the GC at the end of initialize_callable is too early because it happens before the initial reconfigure call, which is typically used for loading heavy models. They suggested moving the GC collection and freezing to the end of Replica.initialize after the first reconfigure has completed to ensure all startup allocations are properly frozen.
Mirror the other throughput-optimized settings by explicitly (re)deriving RAY_SERVE_FREEZE_GC_ON_STARTUP inside the RAY_SERVE_THROUGHPUT_OPTIMIZED block. Functionally a no-op today (the base default is already 1), but it keeps the throughput-optimized bundle self-contained: freeze-GC stays part of it even if the base default changes. Co-Authored-By: Claude Fable 5 <[email protected]> Signed-off-by: harshit-anyscale <[email protected]>
…gure Move gc.collect() + gc.freeze() from the end of UserCallableWrapper.initialize_callable to the end of Replica.initialize, after the initial call_reconfigure. Deployments that do heavy initialization in reconfigure(user_config) (the recommended pattern for config-driven setup) now get those objects frozen too. Guarded by is_first_init so runtime reconfigure calls (user_config updates) and controller-restart re-initializations never re-freeze. Co-Authored-By: Claude Fable 5 <[email protected]> Signed-off-by: harshit-anyscale <[email protected]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit 9bbaa52. Configure here.
abrarsheikh
left a comment
There was a problem hiding this comment.
add a plot to the PR description showing perf improvement
…t#64742) ## Description Adds `RAY_SERVE_FREEZE_GC_ON_STARTUP` (default: `1`). When enabled, each Serve replica runs `gc.collect()` + `gc.freeze()` once at the end of replica initialization (`Replica.initialize`) — after `__init__`, ASGI app setup, and the initial `reconfigure(user_config)` call (if any) have completed. A first-init guard ensures runtime `reconfigure` calls (user_config updates) and controller-restart re-initializations never re-freeze. Objects allocated during replica startup (imports, model weights, framework state) are long-lived, so freezing them moves them to the GC's permanent generation and excludes them from all future collection scans. This reduces GC pauses in the request path, mirroring the proxy-side GC optimizations that already exist (`RAY_SERVE_ENABLE_PROXY_GC_OPTIMIZATIONS`). In load tests of this behavior, tail latency (p95/p99) dropped substantially at high request concurrency and throughput stayed flat instead of degrading. The controller also logs a bullet in the throughput-optimized settings message when the flag is on. **Behavior change note:** this is on by default for all Serve deployments. A full `gc.collect()` runs before freezing, so startup garbage is reclaimed first and only surviving objects are frozen. Frozen objects are still freed by refcounting, but cycles among them are permanently excluded from cyclic GC. The frozen set is fixed once at startup, so any resulting RSS increase is one-time and bounded — it does not compound across config updates. Opt out with `RAY_SERVE_FREEZE_GC_ON_STARTUP=0` (e.g. for deployments that routinely replace initialization-time state). ## Related issues N/A ## Additional information Verified end-to-end with a deployment that reports `gc.get_freeze_count()` and allocates a marker object in `reconfigure(user_config)`: - default: replica reports >100k frozen objects after initialization, and the marker allocated by the initial `reconfigure` is frozen (absent from `gc.get_objects()`) - after a runtime `user_config` update: the newly allocated marker is not frozen and the permanent generation count does not grow (no re-freeze) - with `RAY_SERVE_FREEZE_GC_ON_STARTUP=0` set via `runtime_env` `env_vars`: replica reports 0 frozen objects 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: harshit-anyscale <[email protected]> Co-authored-by: Claude Fable 5 <[email protected]> Signed-off-by: 400Ping <[email protected]>


Description
Adds
RAY_SERVE_FREEZE_GC_ON_STARTUP(default:1). When enabled, each Serve replica runsgc.collect()+gc.freeze()once at the end of replica initialization (Replica.initialize) — after__init__, ASGI app setup, and the initialreconfigure(user_config)call (if any) have completed. A first-init guard ensures runtimereconfigurecalls (user_config updates) and controller-restart re-initializations never re-freeze.Objects allocated during replica startup (imports, model weights, framework state) are long-lived, so freezing them moves them to the GC's permanent generation and excludes them from all future collection scans. This reduces GC pauses in the request path, mirroring the proxy-side GC optimizations that already exist (
RAY_SERVE_ENABLE_PROXY_GC_OPTIMIZATIONS). In load tests of this behavior, tail latency (p95/p99) dropped substantially at high request concurrency and throughput stayed flat instead of degrading. The controller also logs a bullet in the throughput-optimized settings message when the flag is on.Behavior change note: this is on by default for all Serve deployments. A full
gc.collect()runs before freezing, so startup garbage is reclaimed first and only surviving objects are frozen. Frozen objects are still freed by refcounting, but cycles among them are permanently excluded from cyclic GC. The frozen set is fixed once at startup, so any resulting RSS increase is one-time and bounded — it does not compound across config updates. Opt out withRAY_SERVE_FREEZE_GC_ON_STARTUP=0(e.g. for deployments that routinely replace initialization-time state).Perf changes