freelance developer of javascript, python and more!

While working with the Lines3D class in Papervision, I noticed that the memory usage was growing while my application was running. After a bit of hair pulling, debugging and profiling, I found that a lot of Vertex3D objects were being created and not garbage collected.

After the initial creation of the scene, it seemed that the only place where new Vertex3Ds were being created was in these lines:

1
2
3
4
lines.removeAllLines();
lines.addNewLine(o.extra.size,
x1, y1, z1,
x2, y2, z2);

The leak was caused because the addNewLine command creates a new Line object using the material of the Lines3D object. The removeAllLines() command removes all references to Line from the array, however there are a number of current objects still referenced by the Line instances. If you wanted to make sure the line instances are garbage collected, then you would need to null the instance and material properties on each Line instance (it wouldn’t hurt to null the v0, v1 and renderCommand properties too)

I believe it is correct that removeAllLines should not clear the material attribute as it is possible that you are going to use the removed lines for something else and you don’t want to lose reference to the material. However, I do think that removeLine (which is called by removeAllLines) should clear the instance property!

As I know that each Lines3D instance in my graph has only one line, and I’m only updating the endpoints of the line, my solution was to simply update the vertex at each end of the line, as follows:

1
2
3
4
5
6
7
8
var v0:Vertex3D = o.lines[0].v0;
var v1:Vertex3D = o.lines[0].v1;
v0.x = x1;
v0.y = y1;
v0.z = z1;
v1.x = x2;
v1.y = y2;
v1.z = z2;
§7 · December 3, 2009 · Papervision · · [Print]