AJAX请求数据缓存致浏览器内存激增问题求助
candlesData in setInterval AJAX Calls Hey Patrick, let's break down why your browser's memory is ballooning with that setInterval-run AJAX function—those cached candlesData payloads are clearly sticking around longer than they should, and your cleanup attempts aren't cutting it. Here are targeted fixes and debugging steps to resolve this:
1. Fix Unintended Closure References
The most likely culprit is that candlesData is being trapped in a closure created by your AJAX or setInterval callback. If the callback holds a reference to the data, the browser can't garbage-collect it even after the request finishes.
- Quick fix: After your request completes (success or failure), explicitly null out or clear the
candlesDatareference:// After handling AJAX response candlesData = null; // For simple variables // Or for objects: for (const key in candlesData) { delete candlesData[key]; } - Pro tip: Avoid declaring
candlesDatain a scope that persists across setInterval runs (like the global scope or an outer function that doesn't reset). Create it inside the setInterval callback instead, so it's isolated to each request.
2. Explicitly Disable Request Caching
Even though POST requests shouldn't be cached by default, some browsers or server configurations can override this. Force-disable caching with request headers to ensure the payload isn't stored:
For XMLHttpRequest:
const xhr = new XMLHttpRequest(); xhr.open('POST', '/your-endpoint'); // Add cache-busting headers xhr.setRequestHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); xhr.setRequestHeader('Pragma', 'no-cache'); xhr.setRequestHeader('Expires', '0'); xhr.send(JSON.stringify(candlesData));
For Fetch API:
fetch('/your-endpoint', { method: 'POST', headers: { 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0' }, body: JSON.stringify(candlesData) });
3. Cancel Incomplete Requests Before Spawning New Ones
If your setInterval interval is shorter than the time it takes for the AJAX request to complete, you'll end up with multiple in-flight requests—each holding a reference to candlesData. Use an AbortController to cancel pending requests before starting a new one:
let abortController; setInterval(() => { // Cancel any unfinished request if (abortController) abortController.abort(); abortController = new AbortController(); fetch('/your-endpoint', { method: 'POST', signal: abortController.signal, body: JSON.stringify(candlesData) }) .then(res => res.json()) .then(data => { // Handle response candlesData = null; // Clean up reference }) .catch(err => { // Ignore abort errors—they're intentional if (err.name !== 'AbortError') console.error('Request failed:', err); }); }, 1000); // Adjust interval to match your needs
4. Debug with Browser DevTools to Pinpoint Leaks
If the above fixes don't work, use Chrome DevTools to find exactly what's holding onto candlesData:
- Open DevTools → Go to the Memory tab
- Select Heap snapshot and click Take snapshot
- Let your setInterval run for a minute, then take a second snapshot
- In the second snapshot's search bar, type keywords related to
candlesData(like the object type or property names) - Look at the Retainers section to see which objects are keeping your data in memory (could be event listeners, global variables, or third-party code)
Start with the closure cleanup and cache headers—those are the easiest wins. If you still see leaks, move to request cancellation and heap snapshot analysis to track down the stubborn reference.
内容的提问来源于stack exchange,提问作者Patrick Kho

