Blog>
Snippets

Detecting Memory Leaks with Chrome DevTools

Show how to use Chrome Developer Tools to profile memory usage over time, identify memory leaks in a JavaScript application through heap snapshots, and timeline recordings.
function createLeak() {
    const leaks = [];
    setInterval(() => {
        const leak = new Array(1000000).join('leak'); // Creates a large string
        leaks.push(leak); // Pushes it onto the array
    }, 100);
}

createLeak();
This function simulates a memory leak by creating a large string every 100ms and pushing it onto an array that's in the closure of the setInterval callback, ensuring that the memory cannot be freed.