Memory leak deleting images with filters
When replacing a kinetic image with a new image, if the images have filters memory is not reclaimed.
Consider a simple example:
<html>
<head>
<script src="lib/kinetic-v4.7.2.min.js"></script>
</head>
<body>
<input type="button" value="btn" onclick="btnclick();" />
<div id="container"></div>
<script>
var kStage = new Kinetic.Stage({
container: 'container',
width: 1500,
height: 1500
});
var kLayer = new Kinetic.Layer();
kStage.add(kLayer);
var imageObj;
function btnclick() {
// trying to clean the image memory here
kLayer.removeChildren();
imageObj=null;
kLayer.draw();
// now loading the image again
imageObj = new Image();
imageObj.onload = function() {
var kImage = new Kinetic.Image({
image: imageObj,
width: 500,
height: 500,
// applying the filter creates the mem leak
filter: Kinetic.Filters.Invert,
filterRadius: 20
});
// add the image to the kLayer
kLayer.add(kImage);
kLayer.draw();
};
imageObj.src = 'pic_003t.jpg';
}
</script>
</body>
</html>
(you will have to replace the link to kinetic and link to some local image instead of 'pic_003t.jpg', ideally a big image)
Basically creating a stage with a layer and then every time the user clicks a button replacing the image with a new image (in this case with the same src).
If you keep clicking the button you will see in task manager that browser memory keeps going up and is never cleared.
If you remove image filtering (remark filter: Kinetic.Filters.Invert, filterRadius: 20), there is no leak and pressing the button will not increase memory.
If this is a bug can a fix be made ? If not what is the proper way to clear the memory ?
Thank you!
Hi (nice report)! Instead of removing children, try destroying them:
kLayer.destroyChildren();
When I ran your code with removeChildren() - my memory usage kept increasing (I managed to push it over 1 gig) however, it was cleared once I closed the tab.
I also ran your code with destroyChildren() and my memory usage also increased but it eventually levelled off (around a few 100 mB).
I think we're trying to be conservative with our filter handling code but I'll double check to make sure we haven't overlooked anything.
To summarize this: use kLayer.destroyChildren(); in your code.
This is what you should expect: If you're clicking the button only occasionally - your memory usage should be effectively constant. However, if you're clicking the button really fast (more than once ever 20 seconds in my tests) the memory usage will rise until you don't click the button for a while. Then it should drop to a level just above where it was when your image didn't have a filter.
Reopen or start a new issue if something else bugs you.
Looking at Kinetic code, creating a new filter issues this.filterCanvas = new Kinetic.SceneCanvas, so I assume the memory is taken when this new canvas is not released.
I managed to solve this also with issuing clearFilter() before removeChildren(). But adding this to destroyChildren() is much better, thanks!
It sounds like we need to create a remove() extender for Images to make sure filter stuff is cleaned up. Similarly to the Layer extender here:
https://github.com/ericdrowell/KineticJS/blob/master/src/Layer.js#L202