Loading...
Loading...
DevTools (Performance, Memory), Console, React Profiler.console.log, console.trace, console.error for tracking.Performance tab in Chrome DevTools — analyze FPS, load and render time.React Profiler — identify unnecessary re-renders.Lighthouse — optimization recommendations.Memory tab and Heap Snapshot.setInterval, setTimeout, WebSocket, EventListeners.useEffect(() => {
const interval = setInterval(() => console.log("tick"), 1000);
return () => clearInterval(interval); // must clean up!
}, []);
useEffect(() => {
const handleScroll = () => console.log("scrolling...");
window.addEventListener("scroll", handleScroll);
return () => window.removeEventListener("scroll", handleScroll);
}, []);
Closures can "remember" references to objects, and they won't be deleted. Solution: use
useRef,useCallbackor clean up in time.
window.myCache = largeObject; // bad practice
// better explicitly clean
window.myCache = null;
const ref = useRef(null);
useEffect(() => {
ref.current = document.getElementById("my-element");
return () => {
ref.current = null; // release reference
};
}, []);
useEffect(() => {
const socket = new WebSocket("wss://example.com");
return () => socket.close(); // must close
}, []);
Rendering 1000+ elements without virtualization slows application and "clogs" memory.
Solution: use libraries:
Important:
Memory leaks are especially dangerous in SPA (Single Page Application), as application lives long. Even small leaks can lead to performance degradation over time.
setInterval, event listeners, sockets)// Example of safe useEffect pattern
useEffect(() => {
const res = startSomething();
return () => {
cleanupSomething(res);
};
}, []);