Full answer
RxJS leaks follow one rule: a source holds a reference to every subscriber's callback. If the source lives longer than the subscriber and never completes, the callback (and the component it references) stays alive.
Common causes:
- Subscribing to a root service Subject,
interval,fromEvent(window), or store stream in a component and not unsubscribing. - Forgetting to unsubscribe in nested subscriptions, so inner subscriptions pile up (use flattening operators).
shareReplaywithoutrefCount.shareReplay(1)meansrefCount: false, and it still does in RxJS 7.8 for backward compatibility: once started, it never unsubscribes from its source. So with a source likeinterval(1000)or a WebSocket, the source keeps running after the last subscriber leaves. Even if the first subscriber was a component that is now destroyed, the timer continues forever.- Event listeners added with
addEventListenerand not removed. - Keeping large values in
ReplaySubjectwith a largebufferSizeor no time window.
Fixes:
shareReplay({ bufferSize: 1, refCount: true }): when the subscriber count drops to zero, the source is unsubscribed, and a later subscriber restarts it. This is usually what you want for page-level data. For app-level caches that should live for the whole session (user profile),refCount: falseis deliberate, so document it.takeUntilDestroyed()last in the pipe,asyncpipe,toSignal.take(1),first()ortakeUntil(stop$)for finite use.- A
Subscriptioncontainer andunsubscribe()for old code.
Finding leaks: use the browser Memory tab, take heap snapshots before and after navigating away and back, and look for detached DOM nodes and component instances that are still reachable. A simple check: add a teardown log inside a custom Observable and watch whether it runs when the component closes.
typescript
import { Observable, shareReplay } from 'rxjs';
function makeSource(name: string): Observable<number> {
return new Observable<number>((subscriber) => {
const id = setInterval(() => subscriber.next(1), 5);
console.log(name, 'source started');
return () => {
clearInterval(id);
console.log(name, 'source torn down');
};
});
}
const leaky$ = makeSource('shareReplay(1)').pipe(shareReplay(1));
const safe$ = makeSource('refCount true').pipe(shareReplay({ bufferSize: 1, refCount: true }));
const a = leaky$.subscribe();
const b = safe$.subscribe();
setTimeout(() => {
a.unsubscribe();
b.unsubscribe();
setTimeout(() => {
console.log('after the last unsubscribe, only the refCount source is torn down');
process.exit(0);
}, 20);
}, 20);RxJS 7.8. The script was run in Node 24 twice with identical output. It counts source teardowns with and without refCount.
Key points
- Leaks come from long-lived sources keeping subscriber callbacks alive.
- `shareReplay(1)` does not unsubscribe from the source. Use `{ bufferSize: 1, refCount: true }`.
- Prefer `async`, `toSignal`, `takeUntilDestroyed`.
- Find leaks with heap snapshots and teardown logs.
Common mistakes
- Using `shareReplay(1)` on an infinite source in a component-level stream.
- Using `refCount: true` for a cache that must survive route changes, losing the data.
- Nested subscribes that never clean up.
Follow-ups
- When is `refCount: false` the right choice?
- How would you write a test that proves the source was unsubscribed?
- What is the difference between `share` and `shareReplay`?
Tips
- Show the `shareReplay` teardown demo. It proves the point.
- Mention that `refCount: false` can be deliberate.
- Describe how you find leaks with the profiler.
Evaluation rubric
- Explains the root cause of RxJS leaks.
- Identifies the shareReplay refCount issue.
- Gives fixes and a way to detect leaks.