I'm making an animation which will run for infinite time. I've used various objects in the animation which transforms from one shape to another and then I create a new object to take that place and remove the old one.
Example: There's a Rectangle object I modify the segments to convert it into a circle. Then I insert a new Circle object at same point and remove the old Rectangle object. Similar things happen as I add these objects to group and modify them.
Problem:
When I remove the objects, I remove all segments from the path and then use delete
keyword from javascript to remove the object in which I hold the paths.
Creation:
var Obj = {};
Obj.line = new Path.Line({
segments: [new Point.random() * view.size, new Point.random() * view.size],
storkeColor: 'black',
fillColor: 'black'
});
Obj.vector = new Point({
angle: 360 * Math.random(),
length: Math.random() * 10
});
Deletion:
Obj.line.removeSegments();
delete Obj;
using this approach the line is removed from the scene and the animation goes on.
Question:
1) Is this line leaving any traces in memory? What is the best way to completely destroy this object? Because this process is repeated multiple times and I'm worried about memory issues which will effect performance.
2) Same in case of Groups. I remove group children using group.removeChildren()
and in docs it is mentioned that these children are not destroyed and can be added back in scene.
Do they still exist in memory after using group.removeChildren()
? If yes, what is the best way to destroy them?
I've tried to find out how to destroy the objects instead of removing them but didn't find any solution. Please ask if you need any more details on this.
Thanks in advance.
EDIT: There's a possible solution that I found which is deleting the layer itself. I'm not looking for that one because all other objects are in same layer so deleting the layer will also delete those objects.
If you remove the item there are no references to it in paper, so it will eventually get cleaned up in garbage collection if you don't retain references to it in your code. JavaScript doesn't have a way to actually delete an object; as @Pogrindis notes, all it does is delete properties from an object. That can indirectly lead to garbage collection freeing memory by removing references to objects, but it doesn't free memory itself.
Here's a sketch that creates 10000 objects and overwrites them 10 times. If you create a Chrome timeline for memory usage you'll see that the memory usage settles down to the baseline when it is done. Net: it seems like paper is releasing all references to items when item.remove()
is called.