[Flash] How good is the garbage collector ?

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

[Flash] How good is the garbage collector ?

Martijn Loots
Hi.

In my SHWX app, I try to reuse as many objects as I can, because
the app should run 24/7. The only problem I experience is reuse
of TextField objects: they seem to stick to some of their previous
settings...

So, I gave up on reusing TextFields and create new ones every
time, leaving the old ones dangling without references.

Is this good enough ? Does the garbage collector free the memory
even with several thousand "new TextField()" calls a day ?

TIA, grtz,
--
-Martijn    @..@    ( Martijn Loots       -  Hengelo  [NL] )
-          (`--')   ( martijn<@>cosix.com -  www.cosix.com )
-         ( >__< )  ----------------------------------------
-         ^^^  ^^^  (   Netwerken, Security, Open Source   )

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: [Flash] How good is the garbage collector ?

edA-qa mort-ora-y
Martijn Loots wrote:
> Is this good enough ? Does the garbage collector free the memory
> even with several thousand "new TextField()" calls a day ?

It should be enough. I have some programs that create many new sprites
and objects and have not run into a problem.

A few things you need to watch our for though:

- ensure they are removed from their parent

- ensure there aren't any event listeners pointing to a function in the
object

--
edA-qa mort-ora-y
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

BigTPoker uses haXe and DHLIB
        http://BigTPoker.com/?source=haxe-list

The dis-Emi-A haXe Library
        http://wiki.disemia.com/HaXe
       
A full set of tools, classes, and support facilities aimed at
simplifying and expediting game creation in Flash 9.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Sign: Please digitally sign your emails.
Encrypt: I'm also happy to receive encrypted mail.


--
haXe - an open source web programming language
http://haxe.org

signature.asc (260 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [Flash] How good is the garbage collector ?

Armén
In reply to this post by Martijn Loots
On Wed, Nov 4, 2009 at 13:46, Martijn Loots <[hidden email]> wrote:
> ...
>
> The only problem I experience is reuse
> of TextField objects: they seem to stick to some of their previous
> settings...
>

That should not happen. If they seem to "stick" to their previous
settings, it may be because you do not reset all properties. There are
not all that many of them.
Just abstract your textfield creation factory to include a 'text field
reset' method. Also remember that the read only properties of text
fields will linger because since you cannot set them directly (some of
them do not even define what setting them will mean) you have to know
which of the METHODS of TextField indirectly affect them. Maybe this
is why it seems that some settings "stick"?

I don't know how reliable garbage collector is, but in my opinion an
object creation/management factory is always a good idea when projects
get bigger and especially if you have one already. It will add a
couple of kilobytes to your application size, but may speed it up
orders of magnitude, essentially for free.

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: [Flash] How good is the garbage collector ?

edA-qa mort-ora-y
[hidden email] wrote:
> which of the METHODS of TextField indirectly affect them. Maybe this
> is why it seems that some settings "stick"?

The caret rendering is actually a sticky/broken thing. It will not alter
its shape until text in the new font is rendering (just altering the
font is not enough). Additionally there are a few metrics which don't
fully reset themselves after setFont, they require an update operation
first to be fully correct.

Though this doesn't prevent reuse, it does complicate things sometimes.


--
edA-qa mort-ora-y
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

BigTPoker uses haXe and DHLIB
        http://BigTPoker.com/?source=haxe-list

The dis-Emi-A haXe Library
        http://wiki.disemia.com/HaXe
       
A full set of tools, classes, and support facilities aimed at
simplifying and expediting game creation in Flash 9.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Sign: Please digitally sign your emails.
Encrypt: I'm also happy to receive encrypted mail.


--
haXe - an open source web programming language
http://haxe.org

signature.asc (260 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [Flash] How good is the garbage collector ?

Martijn Loots
In reply to this post by edA-qa mort-ora-y
On Wed, 4 Nov 2009, edA-qa mort-ora-y wrote:

> Martijn Loots wrote:
>> Is this good enough ? Does the garbage collector free the memory
>> even with several thousand "new TextField()" calls a day ?
>
> It should be enough. I have some programs that create many new sprites
> and objects and have not run into a problem.
>
Thanks for your answer. Just like to have a little backing for my choice.

> A few things you need to watch our for though:
>
> - ensure they are removed from their parent
>
Done that.

> - ensure there aren't any event listeners pointing to a function in the
> object
>
Will check that, but I don't remember having set listeners yet for these
specific text fields. Should be okay, but worth checking.

Grtz,
--
-Martijn    @..@    ( Martijn Loots       -  Hengelo  [NL] )
-          (`--')   ( martijn<@>cosix.com -  www.cosix.com )
-         ( >__< )  ----------------------------------------
-         ^^^  ^^^  (   Netwerken, Security, Open Source   )

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: [Flash] How good is the garbage collector ?

Martijn Loots
In reply to this post by Armén
On Wed, 4 Nov 2009, [hidden email] wrote:

> On Wed, Nov 4, 2009 at 13:46, Martijn Loots <[hidden email]> wrote:
>> ...
>>
>> The only problem I experience is reuse
>> of TextField objects: they seem to stick to some of their previous
>> settings...
>>
>
> That should not happen. If they seem to "stick" to their previous
> settings, it may be because you do not reset all properties. There are
> not all that many of them.
> Just abstract your textfield creation factory to include a 'text field
> reset' method. Also remember that the read only properties of text
> fields will linger because since you cannot set them directly (some of
> them do not even define what setting them will mean) you have to know
> which of the METHODS of TextField indirectly affect them. Maybe this
> is why it seems that some settings "stick"?
>
I always use a "cleanup method" where I reuse objects. In this case
I initialize with:

   // initialize TextFormat
   ...
   ...
   // filling TextField
   defaultTextFormat = <initialised TextFormat>
   embedFonts        = true;
   autoSize          = TextFieldAutoSize.LEFT;
   multiline         = true;
   htmlText          = StringTools.replace(txt, "\\n", "<br>");
   border            = true;  // FOR DEBUGGING PURPOSES ONLY

as well as x, y, scaleX and scaleY


To cleanup:
   htmlText = "";
   x = y = width = height = 0;
   scaleX = scaleY = 1;


When I reuse a field that should be an Arial font, it has an Impact
font instead that I used elsewhere in the app... :(  Even though the
reused TextField claimed a new TextFormat...

> I don't know how reliable garbage collector is, but in my opinion an
> object creation/management factory is always a good idea when projects
> get bigger and especially if you have one already. It will add a
> couple of kilobytes to your application size, but may speed it up
> orders of magnitude, essentially for free.
>
My data structures can be rather big as well as complex, so reusing
seemed necessary: if I claim a hundred MBytes a few times, release
them, claim some more, etc., chances are I'm out of memory before
the garbage has been collected...

Thanks for the inputs, grtz,
--
-Martijn    @..@    ( Martijn Loots       -  Hengelo  [NL] )
-          (`--')   ( martijn<@>cosix.com -  www.cosix.com )
-         ( >__< )  ----------------------------------------
-         ^^^  ^^^  (   Netwerken, Security, Open Source   )

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: [Flash] How good is the garbage collector ?

jlm@justinfront.net
Textfield is not a true class in someways, I think ones with embeded  
fonts have like the embed attached to their instance prototype that is  
sort of what registering fonts does.  I may not be exact on the  
details but you might be better off attaching a premade in flash  
MovieClip containing the TextField instance.  If you add and remove  
these movieclip instances rather than the textfield itself I don't  
think you will have the same problems?

On 4 Nov 2009, at 14:48, Martijn Loots wrote:

> On Wed, 4 Nov 2009, [hidden email] wrote:
>
>> On Wed, Nov 4, 2009 at 13:46, Martijn Loots <[hidden email]> wrote:
>>> ...
>>>
>>> The only problem I experience is reuse
>>> of TextField objects: they seem to stick to some of their previous
>>> settings...
>>>
>>
>> That should not happen. If they seem to "stick" to their previous
>> settings, it may be because you do not reset all properties. There  
>> are
>> not all that many of them.
>> Just abstract your textfield creation factory to include a 'text  
>> field
>> reset' method. Also remember that the read only properties of text
>> fields will linger because since you cannot set them directly (some  
>> of
>> them do not even define what setting them will mean) you have to know
>> which of the METHODS of TextField indirectly affect them. Maybe this
>> is why it seems that some settings "stick"?
>>
> I always use a "cleanup method" where I reuse objects. In this case
> I initialize with:
>
>  // initialize TextFormat
>  ...
>  ...
>  // filling TextField
>  defaultTextFormat = <initialised TextFormat>
>  embedFonts        = true;
>  autoSize          = TextFieldAutoSize.LEFT;
>  multiline         = true;
>  htmlText          = StringTools.replace(txt, "\\n", "<br>");
>  border            = true;  // FOR DEBUGGING PURPOSES ONLY
>
> as well as x, y, scaleX and scaleY
>
>
> To cleanup:
>  htmlText = "";
>  x = y = width = height = 0;
>  scaleX = scaleY = 1;
>
>
> When I reuse a field that should be an Arial font, it has an Impact
> font instead that I used elsewhere in the app... :(  Even though the
> reused TextField claimed a new TextFormat...
>
>> I don't know how reliable garbage collector is, but in my opinion an
>> object creation/management factory is always a good idea when  
>> projects
>> get bigger and especially if you have one already. It will add a
>> couple of kilobytes to your application size, but may speed it up
>> orders of magnitude, essentially for free.
>>
> My data structures can be rather big as well as complex, so reusing
> seemed necessary: if I claim a hundred MBytes a few times, release
> them, claim some more, etc., chances are I'm out of memory before
> the garbage has been collected...
>
> Thanks for the inputs, grtz,
> --
> -Martijn    @..@    ( Martijn Loots       -  Hengelo  [NL] )
> -          (`--')   ( martijn<@>cosix.com -  www.cosix.com )
> -         ( >__< )  ----------------------------------------
> -         ^^^  ^^^  (   Netwerken, Security, Open Source   )
>
> --
> haXe - an open source web programming language
> http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: [Flash] How good is the garbage collector ?

Martijn Loots
On Wed, 4 Nov 2009, Justin Lawerance Mills wrote:

> Textfield is not a true class in someways, I think ones with embeded fonts
> have like the embed attached to their instance prototype that is sort of what
> registering fonts does.  I may not be exact on the details but you might be
> better off attaching a premade in flash MovieClip containing the TextField
> instance.  If you add and remove these movieclip instances rather than the
> textfield itself I don't think you will have the same problems?
>
Hey, that's a nice idea to try out sometime. Not the approach I would've
thought of myself: I just got rid of most swf resources and load nearly
everything @runtime now. It seems a step backward, allthough I load my
fonts also by means of swf files (@runtime at least... :) )

Thanks for the explanation. I thought it was a bit odd to find a mixup
in fonts in a reused TextField, but your story seems to explain that.

Grtz,
--
-Martijn    @..@    ( Martijn Loots       -  Hengelo  [NL] )
-          (`--')   ( martijn<@>cosix.com -  www.cosix.com )
-         ( >__< )  ----------------------------------------
-         ^^^  ^^^  (   Netwerken, Security, Open Source   )

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: [Flash] How good is the garbage collector ?

jlm@justinfront.net
Loading a layout swf at runtime seems better than laying out in code,  
I use linkages but do not put any code in the loaded, just some labels  
ie for textfields.  Another trick is you can actually create a movie  
with a textfield in and then move the textfield elsewhere in the  
movie, so if you have several 'types' of textfield with different  
embeds in your store you can relocate them where they are needed, so  
never remove/recreate just reposition and reassign a reference.

On 4 Nov 2009, at 15:11, Martijn Loots wrote:

> On Wed, 4 Nov 2009, Justin Lawerance Mills wrote:
>
>> Textfield is not a true class in someways, I think ones with  
>> embeded fonts have like the embed attached to their instance  
>> prototype that is sort of what registering fonts does.  I may not  
>> be exact on the details but you might be better off attaching a  
>> premade in flash MovieClip containing the TextField instance.  If  
>> you add and remove these movieclip instances rather than the  
>> textfield itself I don't think you will have the same problems?
>>
> Hey, that's a nice idea to try out sometime. Not the approach I  
> would've
> thought of myself: I just got rid of most swf resources and load  
> nearly
> everything @runtime now. It seems a step backward, allthough I load my
> fonts also by means of swf files (@runtime at least... :) )
>
> Thanks for the explanation. I thought it was a bit odd to find a mixup
> in fonts in a reused TextField, but your story seems to explain that.
>
> Grtz,
> --
> -Martijn    @..@    ( Martijn Loots       -  Hengelo  [NL] )
> -          (`--')   ( martijn<@>cosix.com -  www.cosix.com )
> -         ( >__< )  ----------------------------------------
> -         ^^^  ^^^  (   Netwerken, Security, Open Source   )
>
> --
> haXe - an open source web programming language
> http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: [Flash] How good is the garbage collector ?

edA-qa mort-ora-y
In reply to this post by Martijn Loots
Martijn Loots wrote:
> When I reuse a field that should be an Arial font, it has an Impact
> font instead that I used elsewhere in the app... :(  Even though the
> reused TextField claimed a new TextFormat...

This sounds very similar to the problem I had. Try setting the text in
the field to a single space " " rather than the empty string. If you
don't have any text several things just don't happen in the text field.


--
edA-qa mort-ora-y
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

BigTPoker uses haXe and DHLIB
        http://BigTPoker.com/?source=haxe-list

The dis-Emi-A haXe Library
        http://wiki.disemia.com/HaXe
       
A full set of tools, classes, and support facilities aimed at
simplifying and expediting game creation in Flash 9.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Sign: Please digitally sign your emails.
Encrypt: I'm also happy to receive encrypted mail.


--
haXe - an open source web programming language
http://haxe.org

signature.asc (260 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [Flash] How good is the garbage collector ?

Martijn Loots
In reply to this post by jlm@justinfront.net
On Wed, 4 Nov 2009, Justin Lawerance Mills wrote:

> Loading a layout swf at runtime seems better than laying out in code, I use
> linkages but do not put any code in the loaded, just some labels ie for
> textfields.  Another trick is you can actually create a movie with a
> textfield in and then move the textfield elsewhere in the movie, so if you
> have several 'types' of textfield with different embeds in your store you can
> relocate them where they are needed, so never remove/recreate just reposition
> and reassign a reference.
>
Another new trick to put in the goodies archive... :)

I have no Flash background at all, I just jumped into haXe. These tips
are great. Would be nice to have a "Tricks" entry on the Wiki, like the
Code Snippets...

Grtz,
--
-Martijn    @..@    ( Martijn Loots       -  Hengelo  [NL] )
-          (`--')   ( martijn<@>cosix.com -  www.cosix.com )
-         ( >__< )  ----------------------------------------
-         ^^^  ^^^  (   Netwerken, Security, Open Source   )

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: [Flash] How good is the garbage collector ?

Martijn Loots
In reply to this post by edA-qa mort-ora-y
On Wed, 4 Nov 2009, edA-qa mort-ora-y wrote:

> Martijn Loots wrote:
>> When I reuse a field that should be an Arial font, it has an Impact
>> font instead that I used elsewhere in the app... :(  Even though the
>> reused TextField claimed a new TextFormat...
>
> This sounds very similar to the problem I had. Try setting the text in
> the field to a single space " " rather than the empty string. If you
> don't have any text several things just don't happen in the text field.
>
Okay, I'll try that tomorrow. Would be nice if it's *that* simple...

Thanks again, grtz,
--
-Martijn    @..@    ( Martijn Loots       -  Hengelo  [NL] )
-          (`--')   ( martijn<@>cosix.com -  www.cosix.com )
-         ( >__< )  ----------------------------------------
-         ^^^  ^^^  (   Netwerken, Security, Open Source   )

--
haXe - an open source web programming language
http://haxe.org