Salesforce Decode
Salesforcedecode
Back to questions
LWCIntermediatelifecyclememory-leakconnectedCallback

Fix memory leak from setInterval in connectedCallback without cleanup

Real World Scenario

Dashboard LWC polls Apex every 5 seconds via setInterval in connectedCallback but disconnectedCallback is empty; users report browser slowdown after hours in console.

Expected Answer

• Store interval Id on component instance • clearInterval in disconnectedCallback mandatory • Prefer empApi or platform events over polling when real-time needed • Increase poll interval when tab not focused using Page Visibility API if polling retained • Abort fetch on disconnect if using fetch with AbortController • Code review checklist: every timer/subscription has symmetric cleanup • Load test console session 8+ hours in UAT

Follow-Up Questions & Answers

Click to expand — each follow-up includes a direct, interview-ready answer

Direct answer: Store interval Id on component instance Also consider: clearInterval in disconnectedCallback mandatory In practice: Prefer empApi or platform events over polling when real-time needed Balance speed of delivery with maintainability.

Architect Perspective

Console users keep tabs open all day — lifecycle cleanup is non-negotiable for polling LWCs.