Ray Serve "Leaked" Memory: Medical AI Engineers Found Two Causes and Fixed Them
The team at Third Opinion Platform, which builds medical AI, saw what looked like a memory leak in Ray Serve: memory did not return to baseline after heavy tasks. The first cause was their own setting: cleanup of idle ray::IDLE processes had been disabled (kill_idle_workers_interval_ms=0 instead of 200 ms). The second was large NumPy arrays being copied between stages and filling the Object Store. The takeaway: treat your Ray config as part of the architecture and verify it with load tests.
AI-processed from Habr AI; edited by Hamidun News
Ray Serve lets you build a distributed pipeline out of ordinary Python functions, and calling another deployment looks almost like a regular method call. But behind the line await deployment_handle.remote(data) sit separate processes, a scheduler, serialization, shared memory, and distributed reference counting. While everything works, you don't see any of this. When problems start, you are no longer debugging application code but how Ray manages memory and object lifetimes. The TM.TECH team at Third Opinion Platform, which builds medical AI for image processing, described the pitfalls they hit in the second part of their write-up.
Configuration Is Part of the Architecture
Ray has many parameters, and they determine not only speed but also how the system behaves under memory pressure, load spikes, and replica shutdowns. The authors advise pinning the following explicitly in the repository:
- resources for a single replica: num_cpus, num_gpus, custom resources;
- the number of replicas or autoscaling bounds;
- max_ongoing_requests and the target load per replica;
- Object Store size and space for spilling (dumping data to disk);
- health check and graceful shutdown timeouts;
- the Ray version everything was tested on.
The last point is especially important: internal system config parameters and their defaults change from version to version. If the system is not stable without a given parameter, it should be documented as a deliberate workaround: the Ray version, the symptom, the expected effect, and the load test that confirms it.
A "Leak"
From Idle Workers
The first problem looked like a classic leak. Heavy tasks finished, but memory did not return to its original level, and ray::IDLE processes lingered in the Ray Dashboard. That is the name for a worker waiting for a task: Ray keeps such processes around to avoid spending time launching new ones.
The cause turned out to be the team's own setting. To speed up processing, they had set kill_idle_workers_interval_ms to 0 (the default is 200 ms), which disables periodic cleanup of idle processes. Python frees objects with no references, but memory held by native libraries is not always returned to the operating system right away. As a result, the RSS (the amount of physical memory a process occupies right now) of a live IDLE process stays higher than it was before the task. Without cleanup, such processes are never terminated at all, and from the outside this is indistinguishable from a leak.
Two more parameters affect worker termination. idle_worker_killing_time_threshold_ms (default 1000 ms) sets how long a worker must sit idle to become eligible for cleanup. max_calls in ray.remote limits the number of function calls per process: for CPU tasks there is no limit by default, while for GPU tasks it is one. The authors' conclusion: pick these values based on load test results, not blindly "for speed." In their charts, cleanup with a 20-second interval noticeably reduced memory consumption compared with cleanup disabled.
The Object Store and Large Arrays Between Stages
To pass large objects between processes, Ray uses the Object Store in shared memory. When it fills up, it spills part of the data to disk (object spilling). This protects against OOM but adds writes and reads, and latency grows. The load is increased by unnecessary ray.put() calls, repeatedly passing large objects by value, high parallelism, and forgotten ObjectRefs.
In the team's pipeline, after each await the array was materialized in the calling replica and sent across the process boundary again. The same large array could go to several stages at once, and copies of it piled up in the Object Store. The amount of memory in use never returned to baseline. A simple example: the result of the first stage is needed by both the second and the third, and the store already holds two or three copies of the same data.
The recommendation: if the result of one deployment is needed by the next, don't await it; pass the response along instead: preprocessed = preprocessor.remote(data), then await model.remote(preprocessed). That way a reference travels onward, not the array itself.
The NumpyStore Wrapper: It Works, but at a Price
The team took a route that they themselves call a mistake. The NumpyStore class puts an array into the Object Store once via ray.put() with the _owner parameter pointing to a long-lived detached actor. Only a lightweight wrapper with a reference is passed between deployments, and the data is fetched via retrieve() at the moment of computation. The object's owner is no longer tied to a worker that has already finished its task. In Ray 2.49.1 this stopped Object Store growth without a noticeable change in processing time.
The price of the solution:
- manual ray.put() and ray.get() at every stage;
- an object created by ray.put() cannot be recovered by re-running the task that produced it;
- all arrays depend on a single actor, and a reference-counting error can keep data alive for too long;
_ownerwas an experimental internal API and was removed in Ray 2.56.
What to Take Away
Practical rules for anyone building pipelines on Ray Serve: pin the version and config, don't disable IDLE worker cleanup for speed without measurements, and measure the cost of the standard DeploymentResponse before writing your own layer on top of the Object Store. The limitation of this experience is obvious: the conclusions come from a specific Ray version and a specific architecture, so before applying them elsewhere, verify them with your own load tests.
- Nov 12, 2026GPT-6 Astra fixed Qwen's looping: accuracy rose from 7.9% to 48.1%
- May 16, 2026Basata automates the medical back office, and doctors can finally call patients back
- Aug 30, 2026SpaceX is leasing out all of its Colossus 1 data center after its engineers struggled to run it
- Feb 9, 2026New RRAM Design Breaks Through the Memory Wall Holding Back AI
Key Questions Answered
What are the key details on Configuration Is Part of the Architecture?
Ray has many parameters, and they determine not only speed but also how the system behaves under memory pressure, load spikes, and replica shutdowns. The authors advise pinning the following explicitly in the repository: resources for a single replica: num_cpus, num_gpus, custom resources; the number of replicas or autoscaling bounds; max_ongoing_requests and the target load per replica; Object Store size and space for spilling (dumping data to disk); health check and graceful shutdown timeouts;…
What are the key details on A "Leak" From Idle Workers?
The first problem looked like a classic leak. Heavy tasks finished, but memory did not return to its original level, and ray::IDLE processes lingered in the Ray Dashboard. That is the name for a worker waiting for a task: Ray keeps such processes around to avoid spending time launching new ones. The cause turned out to be the team's own setting. To speed up processing, they had set kill_idle_workers_interval_ms to 0 (the default is 200 ms), which disables periodic cleanup of idle processes.
What are the key details on The Object Store and Large Arrays Between Stages?
To pass large objects between processes, Ray uses the Object Store in shared memory. When it fills up, it spills part of the data to disk (object spilling). This protects against OOM but adds writes and reads, and latency grows. The load is increased by unnecessary ray.put() calls, repeatedly passing large objects by value, high parallelism, and forgotten ObjectRefs. In the team's pipeline, after each await the array was materialized in the calling replica and sent across the process boundary again.
Test Frontier LLMs & Autonomous Agents in One Workspace
Run Claude Opus 5.5, GPT-6, Gemini 3.1, and DeepSeek side-by-side in Nomad AI (nomadnet.ai) — unified model routing, deep research, and multimodal workflows under one subscription.
Daily 60-sec AI Voice Brief + Weekly Production AI Agent Playbook
Listen to a 60-second AI news brief every morning and get a weekly hands-on playbook for shipping AI agents to production — tools, MCP servers and patterns that work. Curated by Jemal Hamidun, Head of AI at Alpina Digital.
