Skip to the page

Performance: measure, then fix · senior

Memory leaks, ANRs and low-end Android

Memory leaks in React Native apps usually come from listeners, timers and subscriptions that are never cleaned up, screens that stay mounted in a stack, and uncapped image caches, while ANRs come from a blocked main thread; you find both with heap snapshots, native profilers and Play Console traces on real low-end phones.

The notes

What you must know

  • Every subscription in useEffect needs a cleanup: unsubscribe, clearInterval, abort the fetch, remove AppState and Keyboard listeners.
  • Stack screens stay mounted under the top screen. Deep stacks hold memory; pop or reset when a flow ends.
  • Find JS leaks: DevTools Memory panel, take heap snapshots before and after opening and closing a screen a few times, and compare retained objects.
  • Native leaks: Xcode Instruments (Allocations, Leaks), Android Studio Memory Profiler, LeakCanary.
  • iOS native uses ARC (reference counting; break retain cycles with weak references). Android ART and Hermes use tracing garbage collectors; a leak there is a reference you still hold.
  • ANR = main thread blocked (input not handled for about 5 s). Common causes: heavy SDK init in Application.onCreate, disk I/O on the main thread, large bitmap decoding, sync native calls doing I/O.
  • Reproduce on a real low-RAM Android phone; that is where Indian users and ANR spikes are.

The red pen

Common traps: mistake → fix

  • Mistake: "JS cannot leak because it has a GC"

    Fix: anything still referenced (a listener in a Set) is never collected.

  • Mistake: "iOS has a garbage collector"

    Fix: iOS native code uses ARC.

  • Mistake: "A slow JS thread causes ANRs"

    Fix: ANRs are a blocked main thread.

Easy to hard

Questions interviewers ask, easy to hard

  1. Memory climbs each time a user opens and closes the chat screen. How do you find the leak?
  2. Fix this effect that subscribes to a socket and starts an interval.
  3. How is memory managed on iOS, Android and in Hermes?
  4. Crashlytics shows an ANR spike on Android Go phones after a release. Walk me through it.

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview