Memory leaks when manipulating images in Chrome

2019-01-14 08:52发布

I encountered the following 2 (huge!) memory leaks in Chrome:

  1. When editing the 'src' of an existing image, with new bytes
  2. When using clone() to clone an image

Note that in Internet Explorer there is NO memory leak what so ever!

Some background: I'm working on a project where an external camera provides a live feed of images (let's say 100 frames per second).

The main 3 functionalities of the project are:

  1. play live feed
  2. record live feed
  3. show recorded feed

You are welcome to download the following standalone code (simply save it as "leak.html" and execute it), and see for yourself:

<!DOCTYPE html>
<html>
    <body>
        <canvas id="meCanvas" width="526" height="395"></canvas>

        <script src="http://code.jquery.com/jquery-2.0.3.min.js" type="text/javascript"> </script>
        <script>
            var meContext = document.getElementById("meCanvas").getContext("2d");

            // Bytes array representing a chair image
            var chairImgSrc = "data:image/png;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAoHBwkHBgoJCAkLCwoMDxkQDw4ODx4WFxIZJCAmJSMgIyIoLTkwKCo2KyIjMkQyNjs9QEBAJjBGS0U+Sjk/QD3/2wBDAQsLCw8NDx0QEB09KSMpPT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT3/wAARCAHgAoADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5O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XH5q/o999muPLY/u3/AENS1dDTsdPRSA5orM0FpabRmgBc0ZpM0lADs0ZpuaM0CFzRmkzRmgBaSjNITQAUE0maTNAxc0tNzR0oEOozikzRmmAtLSZozQFhaRH8tiduc0ZpaLgSJcrGMR2yD6CopXMhVtuNpzgUtFNybFZdB6X0ijAiU/U0pu3bH7tRUeKUUXEkjGFLSUtQITtRR1opgFFJRQMXFGcUUlAxaWk60lAC0UUUALR1pKKAFopKXNAwpaSigBaWkooAWikooGLRSUUALS02loAXNGabmloAXNJSUZoAXNITQabQMDTSaUmmE0ANY1GxpzGoyaBDGNRPUjGomoASH75qwDVeL71Tg00JkgNDqJI2VhkEYNNFPBoA5i7tza3DIe3I+la1hebo1kY/MvyyfTsafq1r59vvX7yc/hWNaT+RN833G4Ye1XFktHTuuRkdKgYYospi6NC5yyfqOxp0gqiCAmmE09qjNAxM03NBpDSAXNGabRmgBSabn3pc0xuKAOo0a++1W21z+8Tg1pVxun3hs7tZM/KThvpXXo4dQynIIzUSRcWPzRTc0VBQtLmmmjNAC0UmaSmA6kzSZooAWikNJnigQppKTPNGaAFozTaM0AOzS5ptGaAHZozTc0tAC9aXNNBpaAHClzTc0oNACiikzRQBkUGkpaRIUlFFMBaSiigAopKWgA7UtJRmgYUA9qKKAFopKKAFooooGLRSUZoAWlpKKAFozSUUALmikooGLmjNJRQAuaM0lFABRSUUAGaQmjNIaBiE1GacxpjGgQ1qjNOJphNADWqJjUjGomoAI/vVOKrx/eqcGmJjxTgaYDTloAf1HPSub1K1NtckAfIeQa6QGqmp2v2m1JAy6cimgM6yuioSTPMXDe61tvhlDDoRmuVt5PKmGeh4I9q3tOlyjQMcsnK/7varTM2hziojViUYqu1MCM000402kMSjNJSUAOzSNyKTNBbApAMJ7V0mgXvnW5hc/PH/ACrmSataddG1vEkzhejfShq6BHZ0U1WDKCOhozWRoOopKKACiikoAWikozQAuaQmikoAM0UUlAgooooGLmgGkooEOopM0A0xjs0UlFAh2aBSZozQA6gUmaKAMmikz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FLuyeKrg1IvNO4Fu0iM9wka9WOK7uCDyYlQcACub8N2hMxuGX5V+79a6tGBrKT1NIrQwKM0UlMQtFJmigQGikozQAcUlBpKACg0UZoAKSjNFABRmjNANABS02loAXNGaTNFAxaKSjNIBaKSjNAC0lGaTNAC5pDRTSaYC5ppNBNIaACg0maKQwzTc0tIaAEpKCaQ0wENIaU000CEooNIaAE71aXoKq96sr0FADhTqaKWgBacKbThQA7NKKQDNW7ezaTBbgUWC9iKONpDhRWhb2OMM4/OpkjjgToOKhkuy3C/nQLVlppUhHHWqz3DSd8D0qsz9ycmlUkigdktiXNLnvUTOEGSarS3BbgcCobGlcsSXIXIXk1z3iO5Y2yJn7zc1pFjXOa7KXugvZRRHVjlojKJpuaWkrUyENANFKKAJIULyBQOSa7K0tktoQqLzjk+tczo0Jmv4/RTk11+KyqN7GkFcztVAMCjA61i+Up4rZ1k7IEIH8VY6ODzVQ+EUtzYtLCCS0UPEre+Oajm0C3c5Qsh9O1WbE+Zaqy9Ks7mHXmobdykk0c/N4enGTEysPrg1RuNOubfO+NsDv2rsA2R0pkgLKVI4IxTU31E4LocT060A1PewG3uXjPY/pVboa1M2OzSjpTRzTgpJ4oAOlLTzEwHSmbCO1ACU7NAGKMZNAC5zS+1AHFPVCTQAKDxVu0tnuJlRBkk4qax0me7YbFwvcmuq0zS4dPXON0h6saUpWGlcu2FolpaJEB0HJ96tCPPSmA1IDisjW1j/2Q==";

            var image = new Image();
            image.onload = drawNewImage;

            var RECORD_LEN = 20;
            var recordedImages = new Array(RECORD_LEN);
            var count = 0;

            function drawNewImage() {
                meContext.clearRect(0, 0, meContext.canvas.width, meContext.canvas.height);
                meContext.drawImage(image, 0, 0, meContext.canvas.width, meContext.canvas.height);

                setTimeout(nextImage, 10); // Simulates 100 frames per second
            }

            function drawOldImage() {
                var curImage = count % RECORD_LEN; // Cyclic loop over the array
                meContext.clearRect(0, 0, meContext.canvas.width, meContext.canvas.height);
                meContext.drawImage(recordedImages[curImage], 0, 0, meContext.canvas.width, meContext.canvas.height);

                setTimeout(nextImage, 10); // Simulates 100 frames per second
            }

            function nextImage() {
                count++;
                if (count <= 1000) // Phase I (during first 10 seconds): use live camera feed
                {
                    // Generating a random image src (Just for this example!!, instead of using the real camera feed)
                    var newImgSrc = chairImgSrc.slice(0, -11) + ("00000" + count).slice(-6) + "/2Q==";
                    // (CHROME MEMORY LEAK #1: editing the 'src' of an existing image with new bytes creates a new memory that never gets released)
                    image.src = newImgSrc;

                    // Cloning the image, to keep a recorded array of the last N frames
                    var $tmpImage = $(image);
                    // (CHROME MEMORY LEAK #2: clone() creates a new memory that never gets released
                    var clonedImage = $tmpImage.clone()[0];
                    recordedImages[count % RECORD_LEN] = clonedImage;
                }
                else                // Phase II: use recorded feed
                {
                    drawOldImage();
                }
            }

            window.onload = nextImage;
        </script>
    </body>
</html>

This example code takes a static image (of a chair) and modifies it randomly every frame (just to simulate my real camera feed).

For the first 1000 frames it shows the image, and stores the last 10 frames in a cyclic array, and from then on it just shows the 10 frames last recorded (in a loop).

(Obviously my real project is much more complicated, I just simplified it to illustrates the problem).

The question is - please suggest an alternative way (preferably - based on the provided source code) to perform the exact same functionality, without causing a memory leak in Chrome.

PS 1:

In chromium I found the following 2 related bugs, which were NOT really fixed (evidence - my code still leaks...):

  1. "manipulating img src via javascript will generate a massive memory leak" - https://code.google.com/p/chromium/issues/detail?id=36142
  2. "Memory usage grows infinitely when changing img.src" - https://code.google.com/p/chromium/issues/detail?id=114570

PS 2:

I'm fully aware of existing, similar questions in stackoverflow, and I made a lot of attempts, but none of them helped me solve my problem:

  1. Rapidly updating image with Data URI causes caching, memory leak
  2. Canvas even Img eating RAM and CPU
  3. Refresh image with a new one at the same url
  4. Setting img.src to dataUrl Leaks Memory
  5. Memory leak when loading images with javascript's settimeout

Some attempts I made, for example:

  • To make sure that the cache is not the cause, I work in Incognito mode of chrome, so cache is not relevant here.
  • Instead of setting bytes array as the src, I tried using blob URLs (but a similar leak still occurs):
    • img.src = window.URL.createObjectURL(new Blob([bytes.buffer], {type : "image/jpeg"}));
  • Tried putting the image in an iframe, and reloading it every X frames: this partially helps, but it's practically impossible for me to use this 'workaround'.

* UPDATE 29/Jan *

I replaced the following lines:

var $tmpImage = $(image);
var clonedImage = $tmpImage.clone()[0];

With:

var clonedImage = new Image();
clonedImage.src = newImgSrc;

and the leak is the same.

=> So I am down to 'only' 1 bug that requires a workaround (in 2 places): leak when editing an image's src.

1条回答
smile是对你的礼貌
2楼-- · 2019-01-14 09:05

I had the same problem. The only workaround I found was to reduce the number of new Image() to use (ideally one):

function ImageLoader() {
  var img = new Image();
  var queue = [];
  var lock = false;
  var lastURL;
  var lastLoadOk;
  return { load: load };

  function load(url, callback, errorCallback) {
    if (lock) return queue.push(arguments);
    lock = true;
    if (lastURL === url) return lastLoadOk ? onload() : onerror();
    lastURL = url;
    img.onload = onload;
    img.onerror = onerror;
    img.src = url;

    function onload() {
      lastLoadOk = true;
      callback(img);
      oncomplete();
    }
    function onerror() {
      lastLoadOk = false;
      if (errorCallback) errorCallback(url);
      oncomplete();
    }
  }
  function oncomplete() {
    lock = false;
    if (queue.length) load.apply(null, queue.shift());
  }
}
var loader = new ImageLoader();
loader.load(url1, function(img) { // do something });
loader.load(url2, function(img) { // do something });

Note that images will be loaded in serie. If if want to load 2 images in parallel, you'll need to instantiate 2 ImageLoader.

查看更多
登录 后发表回答