2

Similar to this question: Fetch API leaks memory in Chrome

When using fetch to regularly poll data, Chrome's memory usage continually increases without ever releasing the memory, which eventually causes a crash.

https://jsfiddle.net/abfinz/ahc65y3s/13/

const state = {
    response: {},
  count: 0
}

function poll(){
    fetch('https://upload.wikimedia.org/wikipedia/commons/3/3d/LARGE_elevation.jpg')
    .then(response => {
      state.response = response;
      state.count = state.count + 1;
      if (state.count < 20){
        console.log(state.count);
                setTimeout(poll, 3000);
      } else {
        console.log("Will restart in 1 minute");
        state.count = 0;
        setTimeout(poll, 60000);
      }
    });
}

poll();

This JSFiddle demonstrates the issue fairly well. By polling for data every 3 seconds, it seems that something is causing Chrome to continually hold onto the memory. If I let it stop and wait for a few minutes, it usually will release the memory, but if polling continues, it usually holds onto it. Also, if I let it run for a few full cycles, even forcing garbage collection from the perfomance tab of the dev tools doesn't always release all of the memory.

  • The memory doesn't show up in the JS Heap. I have to use the Task Manager to see it.
  • Occasionally, the memory will clear while actively polling, but inevitably builds to extreme levels.
  • Edge also shows the issue, but seems to be more proactive in clearing the memory. Though it still eventually builds to 1GB+ of extra memory usage.

Am I doing something wrong, or is this a bug? Any ideas on how I can get this kind of polling to work long-term without the memory leak?

  • 1
    Not sure if it's related to the problem, but why are you using `await` if you use `.then()` to process the response? – Barmar Nov 11 '21 at 17:35
  • Garbage collection doesn't always return memory to the OS. Heap fragmentation can often prevent this. – Barmar Nov 11 '21 at 17:36
  • Are you just using that image as an example, or are you really trying to fetch the same image every time? – Andy Nov 11 '21 at 17:37
  • @Barmar Yeah, that's an artifact of the original and test code being different. Sorry. – Pete Kuhlmann Nov 11 '21 at 17:54
  • @Andy That image is an example. In the original code, we get a small bit of status, so the growth is significantly smaller, but still causes a problem after a few hours. – Pete Kuhlmann Nov 11 '21 at 17:56
  • Can't see the problem in Firefox, but maybe they are using Rust, so there's no memory leaks. If there was more info on what the code was used for, we could give an alternative method that doesn't cause the same issue. – Invizi Nov 11 '21 at 18:05
  • @Invizi The original code is getting a list of notifications to display to the user. – Pete Kuhlmann Nov 11 '21 at 18:14
  • 2
    @PeteKuhlmann You should probably use a web socket instead then, rather than constant requests. [socket.io](https://www.npmjs.com/package/socket.io) – Invizi Nov 11 '21 at 18:41

1 Answers1

1

I played around a bit with it and it seems to be a bug with the handling of the response so that it won't free the allocated memory if you are not calling any of the response functions.

The chrome task manager and the windows task manager report the same size of 30 MB constantly if i start the code snippet here using this order of execution. Meanwhile it runs on jsfiddle too with 30 MB on request #120.

enter image description here

const state = {
    response: {},
    count: 0
  },
  uri = 'https://upload.wikimedia.org/wikipedia/commons/3/3d/LARGE_elevation.jpg';

!function poll() {
  const controller = new AbortController(),
    signal = controller.signal;
  // using this you can cancel it and destroy it completly.
  fetch(uri, { signal })
    // this is triggered as soon as the header data is transfered
    .then(response => {
      /**
       * Continung without doing anything on response
       * fills the memory.
       *
       * Chrome downloads the file in the background and 
       * seems to wait for the use of a call on the
       * response.fx() or an abort signal.
       * 
       * This seems to be a bug or a small design mistake
       * if response is never used.
       *
       * If response.json(), .blob(), .body() or .text() is
       * called the memory will be free'd.
       */
      return response.blob();
    }).then((binary) => {
      // store data to a var
      return state.response = binary;
    }).catch((err) => {
      console.error(err);
    }).finally(() => {
      // and start the next poll
      console.log(++state.count, state.response.type, (state.response.size / 1024 / 1024).toFixed(2)+' MB');
      requestAnimationFrame(poll);
      // console.dir(state.response);
      
      // reduces memory a bit more
      controller.abort();
    })
}()
Christopher
  • 3,124
  • 2
  • 12
  • 29
  • When I run this in JSFiddle, the Chrome task manager reports consistent memory usage, but the Windows task manager shows continual growth, and after a couple minutes it's over 3 GB, and Chrome crashes soon after. – Pete Kuhlmann Nov 18 '21 at 22:45
  • I tried the same with [`fetch-polyfill`](https://github.com/github/fetch) which makes use of XMLHttpRequest but provides the basic functionality of fetch(). Using this the memory inside the System Task Manager stays low. - Have you reported this behavior on https://bugs.chromium.org/p/chromium/? – Christopher Nov 19 '21 at 16:24
  • Thanks for that link. I hadn't reported it, but I just did. – Pete Kuhlmann Nov 19 '21 at 21:56
  • 1
    This Bug seems to be fixed in one of the next [Updates](https://bugs.chromium.org/p/chromium/issues/detail?id=1272084#c14). – Christopher Jan 24 '22 at 02:13