zero gravitySOLUTIONS
← All insightsJAVA & PERFORMANCE / RETAINED-MEMORY EXPERIMENT

Memory growth behind slow reactive consumers

QUICK ANSWER

Reactive backpressure does not guarantee bounded memory across every adapter and operator. Identify all buffering points, measure retained bytes and subscription multiplicity, and test a controlled slow consumer. Fix the stage retaining work and verify memory recovery after demand resumes or the subscription is canceled.

Inside this article 5 sections

A reactive service can use little CPU while its memory grows steadily. The downstream consumer is slow, upstream work continues somewhere in the pipeline, and objects remain reachable while waiting. The presence of Flux does not establish that every queue, adapter, and client buffer is bounded.

Investigate memory as ownership of unfinished work. Which component holds each record, how large is the retained object graph, how many subscriptions have their own copy of the queue, and what event releases it? Those questions lead to a more useful diagnosis than treating every rising heap graph as a garbage collector problem.

Draw the pipeline including invisible queues

Start with the source and follow one record through decoding, transformation, scheduling, remote calls, batching, and final consumption. Include client libraries and callback adapters. A reactive chain may request a bounded number of elements while a driver or application wrapper has already fetched a much larger collection.

For each stage, record queue capacity, units, payload representation, concurrency scope, and overflow behavior. Capacity measured in records is incomplete without record size. A compact identifier can expand into a large document during decoding, and a transformation can retain both input and output until downstream completion.

ILLUSTRATIVE RETENTION INVENTORY
Source adapter
Does it honor demand, or keep receiving callbacks into its own queue?
Scheduler boundary
How much prefetched work waits, and which object representation is retained?
Concurrent calls
How many input records remain reachable until each inner operation finishes?
Batch or collector
What condition flushes accumulated records, including a quiet or canceled stream?
Subscriber
Does it consume incrementally, or retain every result for a later response?
Backpressure must be inspected across the complete path. A bounded operator cannot constrain work already buffered outside its demand contract.

Pay particular attention to whole-stream collection. Turning an unbounded or very large stream into a list requires retaining its results until completion. That can be correct for a deliberately small response, but it changes the memory model. Put a product-level size limit on the operation or stream results through a protocol that supports incremental consumption.

Estimate retained bytes before changing heap size

Suppose an illustrative stage buffers 200 decoded records averaging 64 KiB each. One subscription can retain about 12.5 MiB of payload at that stage. Forty simultaneous subscriptions can therefore retain about 500 MiB before accounting for object headers, collections, in-flight operations, and other queues. These are planning inputs, not measurements.

Illustrative payload-only estimate:
200 records × 64 KiB = 12,800 KiB = 12.5 MiB per subscription
12.5 MiB × 40 subscriptions = 500 MiB

Actual retained memory also includes:
object graphs + queue structures + in-flight inputs + outputs

Measure the retained graph in the real process. Serialized size is not a reliable substitute for heap retention: strings, arrays, parsed trees, and duplicate representations can substantially change the footprint. Use a representative payload distribution, including unusually large items, rather than multiplying only an average that conceals the tail.

Also distinguish heap from native and direct-buffer memory. A stable Java heap does not prove that process memory is bounded. Network libraries may retain direct buffers, and the operating system reports a broader process footprint. Match the diagnostic tool to the memory category that is actually growing.

Create a slow consumer with a clear hypothesis

Use a fixed source dataset and a subscriber whose demand or processing pace can be controlled. Begin with ordinary consumption, introduce a sustained slowdown, restore normal consumption, and finally cancel a separate run while work is pending. Record emitted, admitted, completed, rejected, and outstanding elements along with memory and queue observations.

The hypothesis should name one suspected boundary: for example, decoded records accumulate before a scheduler handoff because each subscription prefetches independently. Change that boundary's configuration while preserving source data and consumer speed. If memory changes as predicted, inspect the retention path to confirm that the objects belong to that stage.

Capture comparable observations after ordinary garbage collection opportunities and after the backlog drains. Memory rising during a finite backlog is different from memory remaining reachable after all work has completed. Heap dumps can be expensive and contain sensitive data, so use a controlled fixture or an approved diagnostic process with deliberate retention.

Choose an overflow policy that matches the data

Reducing prefetch can lower retained work while changing throughput and request frequency. A bounded buffer can make the maximum explicit, but it still needs a full-buffer policy. Rejecting, dropping newest, dropping oldest, retaining only the latest value, or moving work to durable storage each has a different business meaning.

Latest-value replacement may suit a live temperature display. It is usually wrong for purchase orders, where every accepted intent requires an outcome. A durable queue can hold work beyond process lifetime, but it also needs retention and admission limits. Moving the backlog to another system does not remove the obligation to bound it.

Hot sources and callbacks need special attention because they may not slow down when the subscriber requests less. The adapter must enforce an explicit boundary and expose overflow. A silent drop makes the memory graph look better while losing data, so count dropped or rejected items and include them in the operation result.

Verify cleanup on every terminal path

Cancellation, errors, and timeouts should release references and resource-backed buffers according to the libraries' contracts. Check custom queues, cached publishers, retry state, and listeners that may keep subscriptions reachable. A retry can also resubscribe while retaining previous attempt data if the application stores history without bounds.

After the fix, repeat slowdown, drain, and cancellation with several subscription counts. The expected result is a bounded memory envelope and a clear explanation for retained work. The API backpressure guide covers admission scope, and latency analysis connects queueing to user impact. Java architecture work should make memory ownership as explicit as thread ownership.

FROM INSIGHT TO ACTIONSoftware architecture consulting

Bound the memory behind reactive throughput.

We can reproduce slow-consumer behavior, inspect retention paths, and implement measurable overflow and cleanup policies.

Explore architecture consultingDiscuss your architecture

Start with your goals.
We’ll work through the next step together.