Skip to content

[serve] Freeze GC after replica initialization by default - #64742

Merged
abrarsheikh merged 3 commits into
ray-project:masterfrom
harshit-anyscale:serve-freeze-gc-on-startup
Jul 14, 2026
Merged

abrarsheikh merged 3 commits into
ray-project:masterfrom
harshit-anyscale:serve-freeze-gc-on-startup

Conversation

@harshit-anyscale

@harshit-anyscale harshit-anyscale commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

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).

Perf changes

Screenshot 2026-07-14 at 23 23 21

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]>
@harshit-anyscale
harshit-anyscale requested a review from a team as a code owner July 14, 2026 11:27

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread python/ray/serve/_private/replica.py Resolved
@harshit-anyscale harshit-anyscale self-assigned this Jul 14, 2026
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]>
Comment thread python/ray/serve/_private/replica.py Resolved
@harshit-anyscale harshit-anyscale added the go add ONLY when ready to merge, run all tests label Jul 14, 2026
…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]>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 9bbaa52. Configure here.

Comment thread python/ray/serve/_private/replica.py Resolved
@ray-gardener ray-gardener Bot added the serve Ray Serve Related Issue label Jul 14, 2026

@abrarsheikh abrarsheikh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a plot to the PR description showing perf improvement

@abrarsheikh
abrarsheikh merged commit 1eab59c into ray-project:master Jul 14, 2026
4 checks passed
@harshit-anyscale

harshit-anyscale commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor Author

/mux was the deployment we were investigated for higher tail latency.

Screenshot 2026-07-14 at 23 23 21

added in the PR description as well.

400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests serve Ray Serve Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants