question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Memory leak with Canvas and streaming video

See original GitHub issue

Using Chrome 16.

First, I create a texture object from a canvas that is loaded from a MJPEG stream and store it into a custom object within a function that I call every 1/3 of a second. I am constantly refreshing my canvas with a streaming video.

var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
var texture =  new THREE.Texture(canvas);
streamObject.texture = texture;

Then, I execute another function to display that texture by retrieving it from the custom object and setting the material’s map to that texture before I call my render function.

vCam.material.map = textureTemp;
render(vCam);

The problem however is that my memory climbs through the roof If I do not force a garbage collection with:

                try {
            window.gc();
                } catch(e) {
                    console.log("Garbage Collection is not available for this browser type.");
                }

I would like to avoid forcing the garbage collection. Is it possible that the texture objects are not being gc’d creating my memory to climb?

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mrdoobcommented, Mar 7, 2012
mesh.material = new THREE.MeshBasicMaterial( { map: new THREE.Texture(test) } );

Thats wrong. You should never create new objects in a render loop. You should be able to do this inside init().

0reactions
Ikspaycommented, Mar 7, 2012

Here is a simple code which makes this problem occur… What am I doing wrong here ?

        <div id="container"></div>
        <canvas id="test" width="100" height="100"></canvas>

        <script src="../build/Three.js"></script>

        <script>
            var changed = false;
            var camera, scene, renderer, mesh, material;

            var windowHalfX = window.innerWidth / 2;
            var windowHalfY = window.innerHeight / 2;

            init();
            animate();

            function init() {
                var container = document.getElementById( 'container' );
                var test = document.getElementById( 'test' );

                var ctx = document.getElementById('test').getContext('2d');

                ctx.fillStyle = 'rgb(255, 0, 0)';
                ctx.fillRect(0,0,100,100);

                camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
                camera.position.set( 0, 0, 100 );

                scene = new THREE.Scene();

                scene.add( camera );

                scene.add( new THREE.AmbientLight( 0x00020 ) );

                geometry = new THREE.CubeGeometry( 20, 20, 20 );

                mesh = new THREE.Mesh( geometry, material );
                scene.add( mesh );

                renderer = new THREE.CanvasRenderer();
                renderer.setSize( window.innerWidth, window.innerHeight );
                container.appendChild( renderer.domElement );
            }

            function animate() {
                requestAnimationFrame( animate );
                render();
            }

            function render() {
                mesh.material = new THREE.MeshBasicMaterial( { map: new THREE.Texture(test) } );

                renderer.render( scene, camera );
            }

        </script>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Memory Leak when Drawing Video to Canvas using ...
I found the problem was in this.canvas.captureStream() . I want to collage 1920x1080 side by side to create dual-monitor screen recorder, ...
Read more >
Memory leak when switching video streams from different ...
In my web application, I have 4 camera feeds streaming from different cameras. Every few seconds, the camera feeds will change their video...
Read more >
Mediarecorder used on canvas mediastream with audio track ...
Mediarecorder used on canvas mediastream with audio track results in increasing RAM usage/memory leak · Categories · Tracking · People · References ·...
Read more >
Plugging Memory Leaks With The Chrome Dev Tools - Ben Dilts
Garbage collection does not protect you from memory leaks. The Chrome Dev Tools have introduced a ... Your browser can't play this video....
Read more >
Memory Leak while streaming | OBS Forums
OBS (Open Broadcaster Software) is free and open source software for video recording and live streaming. Stream to Twitch, YouTube and many ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found