Ultrashock Forums > Flash > ActionScript
AS3 - Flex Gumbo Papervision Memory Issue

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
AS3 - Flex Gumbo Papervision Memory Issue
Old 2008-11-20

I am using Flex Builder Pro/Gumbo 4.0 SDK - Flex Project, targeting FP10.

I am using a simple states based model (using enterstate and mouseclick events to execute the loading and unloading of swfs into my main application (aState)

at runtime). If i had more time and researched better in the first place i may have invested in a better mvc strategy.

I am loading papervision3d swfs created in flex mxml/as3.

The 3d models i use are made with as3 geom and are classes that extend TriangleMesh3D and use jpg skins using the embed/class technique.

(TriangleMesh3D Inherits from: Vertices3D < DisplayObject3D < DisplayObjectContainer3D < Flash.Events.EventDispatcher).

Hmmm. the latter makes me listen.

As you write your problem you start to work things out, ask better questions, think more clearly - i'm quite inexperienced at programming.

And its Constructor:

TriangleMesh3D (material:MaterialObject3D, vertices:Array, faces:Array, name:String = null, initObject:Object = null)

MaterialObject3D also Inherits from flash.events.EventDispatcher. Ok, so...

..I have managed to load child apps/swfs into the main app using SWFLoader. Testing went well with two browser exceptions (opera and chrome but thats for

another day; just to let you know opera doesn't like the loaded papervision flex swf viewport setting to false for autoclipping, it hangs the browser and you

have to click the task bar reference to opera to shunt it into working, chrome failed on the unloadAndStop() not giving back the cpu usage properly from 50

to 32%, which is interesting. However, they all failed on...


...The problem: When the swf is unloaded the ram used (lets say 30 mb) stays. As the swfs load and unload without the ram being taken back that ram keeps

going up. A ram/gc/listener/referencing/EventDispatcher!!! problem. Heres Johnny! Again! (But am i getting warm).

Using unloadAndStop(true) sorted out the cpu usage that unload and FP9 were not doing, which is interesting because cpu usage goes to 0 on unloading so are

my loaded swfs/references/listeners getting unloaded/killed properly...NO, not in ram.

The garbage collector does not free any of the ram that is used to render the loaded swf (the loaded flex swf contains 3d data as described above -

TriangleMesh3D).

I've got a strong feeling now this is to do with TriangleMesh3D still being referenced < Flash.Events.EventDispatcher (as3 geom is a converter module that 2

guys developed to convert 3d objects in max to papervision classes (you heard of mr.doob! hes probably a friend of yours too - live long ricardo ) but i

don't think its on adobes priority list for flash player 10s unloadAndStop method. Is unloadAndStop(true) missing what the swf has implanted into the main

app/being referenced by the main app?


So Let me just test...

...10 mins later.

Thats interesting i took the 3d as3 geom model away and left the plane with a bitmap on it. When i clicked in and out to load/unload the memory rose to about

250mb then it started to drop and level off at 230 (i am reading the memory through windows task manager), i think unloadAndStop and gc set to true is doing

its job(s) but gc took a little time to kick in. It didn't go back to the original main app weight of about 170mb ram on first load either, which gives me a

little concern that there is initial memory leakage or perhaps gc allocates a suitable byte allocation block on the flash player to that level?!

Most importantly though the ram stabilised and even came back down a little. So its the 3d model thats the culprit i think!

I've tried using the memory profiler in flex to look into this also but cannot get that to work - don't ask - so i don't know exactly but its becoming clear

that the 3d model is still being referenced and maybe the bitmap skin to.

Can you please help me with this problem.

Help me define it and solve it. What is this problem exactly. Why is UnloadAndStop() not terminating the references/listeners generated by the 3d model on

the loaded swf. Is the loaded swf reversing the referencing logic through EventDispatcher.

How can I terminate the references and from where, which app, main or loaded.

Pls help me.

Sean.
postbit arrow 3 comments | 1459 views postbit arrow Reply: with Quote   
Registered User
Flexilicious is offline
seperator
Posts: 8
2008-11-20
Flexilicious lives in United Kingdom
seperator

Ultrashock Member Comments:
sentinels sentinels is offline sentinels lives in United States 2008-11-20 #2 Old  
one thing i've learned in all my years of dev (and especially since picking up C/Objective-C again) is you can not rely on other things to handle garbage collection/memory management for you. you must, i repeat: must, handle your own memory management. make sure bitmaps are being disposed of properly (look at the BitmapData dispose() method). make sure all your listeners are removed upon being removed from the display list and prior to it being killed. null out objects after you remove your listeners on it to ensure that the object is flagged for deletion. there are a lot of other things you should watch out for and i'm sure you can go through your app and find many places where this is not being handled. especially if you're using an outside framework other than your own.
Reply With Quote  
Flexilicious Flexilicious is offline Flexilicious lives in United Kingdom 2008-11-20 #3 Old  
My code is as follows:

..............................................
..............................................

Main App (Interface) Code portion:

<mx:State name="Car3DScene"
enterState="initSWF(event);"
exitState="unloadSWFHandler(event);">
<mx:AddChild relativeTo="{panel1}" position="lastChild">

<mx:Canvas width="100%" height="100%"
minHeight="340" minWidth="920"
borderColor="#1d1d1d" borderThickness="1" borderStyle="solid"
backgroundAlpha="1" backgroundColor="#000000">
<mx:Image x="0" y="0" width="295" height="250"
id="myLogo" source="@Embed('assets/images/scene3dMainImage.png')"
autoLoad="true" maintainAspectRatio="false"/>
<mx:Script>
<![CDATA[
import flash.events.*;
import flash.net.URLRequest;
import mx.controls.SWFLoader;
import mx.events.FlexEvent;
import flash.display.Loader;

public var swfContent:MovieClip;

public function initSWF(e:Event):void
{
myLoader.addEventListener(Event.INIT,loadHandler,f alse, 0, true);
myLoader.addEventListener(Event.UNLOAD,unloadSWFHa ndler,false, 0, true);
myLoader.load("assets/swfs/PV3DCanvasSWFFVPortWR.swf");
}

private function loadHandler(e:Event):void
{
swfContent = e.target.content;
}

private function unloadSWFHandler(e:Event):void
{
swfContent.loaderInfo.loader.unloadAndStop(true);
//myLoader.unloadAndStop(true);
}
]]>
</mx:Script>
<mx:SWFLoader x="295" y="0"
width="625" height="250"
minWidth="625" minHeight="250"
maintainAspectRatio="false"
scaleContent="true" blendMode="normal"
id="myLoader" alpha="1"/>
.................................................. .....
.................................................. .....

Here is the code of the incoming SWF:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
preloader="com.usppro.BasicCustomPreloader"
layout="absolute"
applicationComplete="init3D();">
<mx:Script>
<![CDATA[
import mx.controls.*;
import flash.display.*;
import objects.*;//The as3 geom (3d model) classes
import org.papervision3d.cameras.*;
import org.papervision3d.materials.*;
import org.papervision3d.materials.special.*;
import org.papervision3d.objects.*;
import org.papervision3d.objects.special.*;
import org.papervision3d.render.*;
import org.papervision3d.scenes.*;
import org.papervision3d.view.*;
import org.papervision3d.objects.primitives.*;
import org.papervision3d.materials.*;
import org.papervision3d.view.*;
import org.papervision3d.core.utils.*;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.core.geom.renderables.*;

private var camera :Camera3D;
private var renderer :BasicRenderEngine;
private var scene :Scene3D;
private var viewport :Viewport3D;
private var universe isplayObject3D;

private var c:CarFinal;//The car 3d model class
[Embed(source="/assets/Car_Black.jpg")]
private var CarTexture:Class;


private function init3D():void
{
Application.application.stage.align = StageAlign.TOP_LEFT;
Application.application.stage.scaleMode = StageScaleMode.NO_SCALE;
Application.application.stage.quality = StageQuality.LOW;
Application.application.stage.frameRate = 30;

viewport = new Viewport3D(625,250,false,false,true,true);
pv3dCanvas.rawChildren.addChild(viewport);
pv3dCanvas.rawChildren.addChild(new Stats());
renderer = new BasicRenderEngine();
scene = new Scene3D();
camera = new Camera3D(universe,18,200);
camera.x = 0;
camera.y = 0;
camera.z = -200;
createObjects();
addEventListeners();
}

private function createObjects():void
{
c = new CarFinal();
c.material = new BitmapMaterial(new CarTexture().bitmapData);
c.x = 0;
c.y = 0;
c.z = 0;

universe = new DisplayObject3D();
universe.addChild(c);

scene.addChild(universe);
universe.x = 200000;
universe.y = -200000;
universe.rotationY = -90;
camera.target = universe;
}

private function addEventListeners():void
{
addEventListener(Event.ENTER_FRAME, loop3D,false,0,true);
}

public function loop3D(e:Event):void
{
camera.target.x -= (((mouseX - (pv3dCanvas.width * 0.5)) * 12) + camera.target.x) / 24;
camera.target.y -= (((mouseY - (pv3dCanvas.height * 0.5)) * 12) + camera.target.y) / 24;

universe.yaw(-0.6);

renderer.renderScene(scene, camera, viewport);
}
]]>
</mx:Script>
<mx:Canvas width="625" height="250"
x="0" y="0"
blendMode="normal"
id="pv3dCanvas"
backgroundColor="#000000" backgroundAlpha="1">
</mx:Canvas>
</mx:Application>

..............................................
..............................................

Here is a code portion of the 3d car model class

package objects
{
import org.papervision3d.core.*;
import org.papervision3d.core.geom.*;
import org.papervision3d.core.geom.renderables.Triangle3D ;
import org.papervision3d.core.geom.renderables.Vertex3D;
import org.papervision3d.core.math.NumberUV;
import org.papervision3d.core.proto.*;

public class CarFinal extends TriangleMesh3D
{

public var verts :Array;
public var faceAr:Array;
public var uvs :Array;

private function v(x:Number,y:Number,z:Number):void
{
verts.push(new Vertex3D(x,y,z));
}

private function uv(u:Number,v:Number):void
{
uvs.push(new NumberUV(u,v));
}

private function f(vn0:int, vn1:int, vn2:int, uvn0:int, uvn1:int,uvn2:int):void
{
faceAr.push( new Triangle3D( this, [verts[vn0],verts[vn1],verts[vn2] ], null, [uvs[uvn0],uvs[uvn1],uvs[uvn2]] ) );
}

public function cobraCarFinal( material:MaterialObject3D=null, initObject:Object=null )
{
super( material, new Array(), new Array(), null );
verts = this.geometry.vertices;
faceAr= this.geometry.faces;
uvs =new Array();
v(108.544,-31.9748,-101.242);
...........................
f(1248,5195,5196,1248,5195,5196);
this.geometry.ready = true;
}

}

}

Pls show me how
Reply With Quote  
taherm taherm is offline 2009-12-31 #4 Old  
Unloading Papervision Objects from Scenes
I posted an article speaking about my experience here. Hope it helps. don't hesitate to post your comments or suggestions.

Best Regards.
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: