MaterialManager memory leak in Papervision3d: Great White
I’ve just starting using Papervision3d for the first time and I decided to go head first into the Great White build.
After completing a portion of work in my project I usually run the profiler to make sure objects are being collected. So, after finishing my Papervision3d portion of work, I ran the project in the profiler. I immediately noticed that I had a worry number of PV3D objects still hanging out, taking up a lot of memory. They we’re mainly math type objects such as Number3D and Matrix3D. And after exploring some more using the profiler, I was able to see that the culprit was the MaterialManager. And surprise , surprise, it’s a Singleton.
Now, I’m not one of those people who hate Singleton’s in projects, all I ask is that they keep themselves tidy. So that when a singleton is “idle”, it’s memory foot print is minimal. And yes I understand that “Great White” is in a pre-release status so I’m not on a rant and bitch session here. I would merely like to warm others who are using Great White for projects to be carful.
So, how does MaterialManager leak? It would seem that the following Dictionary just keeps growing and growing with complex Material objects.
private var materials:Dictionary;
And in the way I’m using Papervision3d; I end up with hundreds of bitmap BitmapMaterial’s and WireframeMaterial’s by having Cubes covered in the BitmapMaterial’s fly on to stage and then off again. Once I remove the Cube from the Scene3D, I expected the Material to be GC’ed because I saw no hard deconstruct method. But no, I saw this Dictionary becomes quite large, very quickly.
To work around this problem I temporally commented out the body of the registerMaterial method so that my Material would never be added to materials:Dictionary.
public static function registerMaterial(material:MaterialObject3D):void {
//getInstance()._registerMaterial(material);
}
But you may need to build your own work around if you’re using shaders as the MaterialManager seems to be connected to ShadedMaterial’s. Maybe you could call init() on MaterialManager once you’re finished with your papervision3d portion of the project. Or work closely with MaterialManager when removing objects from the Scene3D.
I’m sure this issue shall be corrected for the final release of PV3D 2 but until then this work around will be working for me.




est il availible en Francais, my English not good
Alex,
Thanks so much for this - I’ve been tearing my hair out for the past few days with exactly the same problem. I’ve tried your solution and it works perfectly.
Thanks again
Well how do i say it….
You saved my life !! :))
the framerate of my project went up to the roof…i’ve been worried that i could never have it run smoothly and now it does !!
Thanks a lot !!
still got some other issues though…
my planes are never garbage collected and i don’t know why…
Every MaterialObject3D (or derived class) registers itself with the MaterialManager in the conctructor. So all that is
needed is for the application to call mymaterial.destroy(), which will unregister from the material, allowing GC to reclaim it.
But agree that this seems silly …
Thanks for the tip, this worked perfectly! I was actually using the .destroy() method and I was still leaking about 1MB each time I added a new Cube to my scene. Since I’m not using any Shaders or other utilities, there doesn’t seem to be any bugs or repercussions.