You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@flex.apache.org by mo...@comcast.net on 2013/06/06 17:52:05 UTC

when to dispose of objects for garbage collection?

Can someone help me identify when I need to dispose of an object, array, etc. in a typical Flex app? 

For example, suppose I have an app with several states and a TitleWindow. 

I know that if I declare a variable for a state, such as: 

<fx:Script> 
<![CDATA[ 
  ...
  private var myArr:Array;
  ...	
]]>
</fx:Script>

that when I no longer need this array (or object, etc.), I should set it to null to inform the garbage collector (GC) it's ready to be picked up. That's because, otherwise, this variable remains in memory, since the state persists throughout the life of the app.

But what if this state uses the following function:

<fx:Script> 
<![CDATA[ 
  private var summation:Number;
  ...	
  private function myFunc():void {
      var anotherArr:Array=[1,2,3,4,5]
      for (var i:int=0; i<anotherArr.length; i++)
        summation+=anotherArr[i];
  }
  ...
]]>
</fx:Script>

QUESTION 1: Do I need to manually null variable myArr2 at the end of function myFunc()? Or, will it be picked up automatically by the GC?

How about TitleWindows? 

QUESTION 2: If I open a TitleWindow (e.g. popup) that contains a DataGrid, do I need to manually null its data provider when I close the TitleWindow? Or, will it be picked up automatically by the GC? 

QUESTION 3: This last question also applies to a data provider for ComboBox, or an ArrayList, or an Array that is used in a TitleWindow -- do I need to null those as well upon closing the window? Or, will they be picked up automatically by the GC?

Thanks in advance for any comments.








Re: when to dispose of objects for garbage collection?

Posted by Thomas Wright <tw...@yesco.com>.
as an addenda here's the link I originally learned this from. There may be
some hidden secrets of gc that only the most elite know about, but for the
average developer, I'd guess this is, for the most part, what you'll need
to know.
go to slides 78 and 79
http://www.slideshare.net/rjowen/adobe-flex-component-lifecycle-presentation


On Thu, Jun 6, 2013 at 10:35 AM, Thomas Wright <tw...@yesco.com> wrote:

> I'm fairly sure that unless you've created any sort of event listener
> attached to that particular object, or any sort of inherently necessary
> link to that object from a parent object, that GC should collect once it's
> removed from execution.
> However, to be safe and make sure GC collects on its next pass, setting a
> variable to null ensures collection. So in theory, that would be best
> practice, but in practice, that's not always the best way to do something.
> For question 2, I'm fairly sure you don't, since the dataprovider is only
> a reference, there is not any inherent dependence on the object that
> contains the data that is used as a dataprovider. Again, you may wish to
> double check that.
> For the third question, no, you don't need to null anything that's a
> child. Once you orphan those objects by clearing the window, GC immediately
> collects at its next pass.
> I'm still not 100% sure whether or not there's a gotcha here, but I'm
> fairly confident that this is how things are done.
>
>
> On Thu, Jun 6, 2013 at 10:03 AM, <mo...@comcast.net> wrote:
>
>> I should have mentioned regarding question 2 and 3, that the DataGrid,
>> ComboBox, etc., used by the TitleWindow was CREATED in the TitleWindow, and
>> is not used external to the TitleWindow. The questions being whether the
>> TitleWindow automatically cleans up these variables for GC, or requires me
>> to null them first...
>>
>> ----- Original Message -----
>> From: modjklist@comcast.net
>> To: users@flex.apache.org
>> Sent: Thursday, June 6, 2013 8:52:05 AM
>> Subject: when to dispose of objects for garbage collection?
>>
>>
>> Can someone help me identify when I need to dispose of an object, array,
>> etc. in a typical Flex app?
>>
>> For example, suppose I have an app with several states and a TitleWindow.
>>
>> I know that if I declare a variable for a state, such as:
>>
>> <fx:Script>
>> <![CDATA[
>>   ...
>>   private var myArr:Array;
>>   ...
>> ]]>
>> </fx:Script>
>>
>> that when I no longer need this array (or object, etc.), I should set it
>> to null to inform the garbage collector (GC) it's ready to be picked up.
>> That's because, otherwise, this variable remains in memory, since the state
>> persists throughout the life of the app.
>>
>> But what if this state uses the following function:
>>
>> <fx:Script>
>> <![CDATA[
>>   private var summation:Number;
>>   ...
>>   private function myFunc():void {
>>       var anotherArr:Array=[1,2,3,4,5]
>>       for (var i:int=0; i<anotherArr.length; i++)
>>         summation+=anotherArr[i];
>>   }
>>   ...
>> ]]>
>> </fx:Script>
>>
>> QUESTION 1: Do I need to manually null variable myArr2 at the end of
>> function myFunc()? Or, will it be picked up automatically by the GC?
>>
>> How about TitleWindows?
>>
>> QUESTION 2: If I open a TitleWindow (e.g. popup) that contains a
>> DataGrid, do I need to manually null its data provider when I close the
>> TitleWindow? Or, will it be picked up automatically by the GC?
>>
>> QUESTION 3: This last question also applies to a data provider for
>> ComboBox, or an ArrayList, or an Array that is used in a TitleWindow -- do
>> I need to null those as well upon closing the window? Or, will they be
>> picked up automatically by the GC?
>>
>> Thanks in advance for any comments.
>>
>>
>>
>>
>>
>>
>>
>>
>
>
> --
> *Thomas Wright*
> Software Engineer
> Extension: 1054
> Office: [801] 464.4600
>
> Corporate Division
> twright@yesco.com
>
>


-- 
*Thomas Wright*
Software Engineer
Extension: 1054
Office: [801] 464.4600

Corporate Division
twright@yesco.com

Re: when to dispose of objects for garbage collection?

Posted by Thomas Wright <tw...@yesco.com>.
I'm fairly sure that unless you've created any sort of event listener
attached to that particular object, or any sort of inherently necessary
link to that object from a parent object, that GC should collect once it's
removed from execution.
However, to be safe and make sure GC collects on its next pass, setting a
variable to null ensures collection. So in theory, that would be best
practice, but in practice, that's not always the best way to do something.
For question 2, I'm fairly sure you don't, since the dataprovider is only a
reference, there is not any inherent dependence on the object that contains
the data that is used as a dataprovider. Again, you may wish to double
check that.
For the third question, no, you don't need to null anything that's a child.
Once you orphan those objects by clearing the window, GC immediately
collects at its next pass.
I'm still not 100% sure whether or not there's a gotcha here, but I'm
fairly confident that this is how things are done.


On Thu, Jun 6, 2013 at 10:03 AM, <mo...@comcast.net> wrote:

> I should have mentioned regarding question 2 and 3, that the DataGrid,
> ComboBox, etc., used by the TitleWindow was CREATED in the TitleWindow, and
> is not used external to the TitleWindow. The questions being whether the
> TitleWindow automatically cleans up these variables for GC, or requires me
> to null them first...
>
> ----- Original Message -----
> From: modjklist@comcast.net
> To: users@flex.apache.org
> Sent: Thursday, June 6, 2013 8:52:05 AM
> Subject: when to dispose of objects for garbage collection?
>
>
> Can someone help me identify when I need to dispose of an object, array,
> etc. in a typical Flex app?
>
> For example, suppose I have an app with several states and a TitleWindow.
>
> I know that if I declare a variable for a state, such as:
>
> <fx:Script>
> <![CDATA[
>   ...
>   private var myArr:Array;
>   ...
> ]]>
> </fx:Script>
>
> that when I no longer need this array (or object, etc.), I should set it
> to null to inform the garbage collector (GC) it's ready to be picked up.
> That's because, otherwise, this variable remains in memory, since the state
> persists throughout the life of the app.
>
> But what if this state uses the following function:
>
> <fx:Script>
> <![CDATA[
>   private var summation:Number;
>   ...
>   private function myFunc():void {
>       var anotherArr:Array=[1,2,3,4,5]
>       for (var i:int=0; i<anotherArr.length; i++)
>         summation+=anotherArr[i];
>   }
>   ...
> ]]>
> </fx:Script>
>
> QUESTION 1: Do I need to manually null variable myArr2 at the end of
> function myFunc()? Or, will it be picked up automatically by the GC?
>
> How about TitleWindows?
>
> QUESTION 2: If I open a TitleWindow (e.g. popup) that contains a DataGrid,
> do I need to manually null its data provider when I close the TitleWindow?
> Or, will it be picked up automatically by the GC?
>
> QUESTION 3: This last question also applies to a data provider for
> ComboBox, or an ArrayList, or an Array that is used in a TitleWindow -- do
> I need to null those as well upon closing the window? Or, will they be
> picked up automatically by the GC?
>
> Thanks in advance for any comments.
>
>
>
>
>
>
>
>


-- 
*Thomas Wright*
Software Engineer
Extension: 1054
Office: [801] 464.4600

Corporate Division
twright@yesco.com

Re: when to dispose of objects for garbage collection?

Posted by Romain KELIFA <ro...@gmail.com>.
Ho, I forgot... you can also use IDE memory profiler or Adobe Scout to
track your potential memory leaks.

>From what I know, most of the things you create via MXML is automatically
handled (from GC point of view) by the framework.

However, everything you create via pure AS3 must be handled manually,
that's why it can be a good practice to implement a dispose() method in
your components 'abstract' classes (wired on FlexEvent.REMOVE), at least to
remove all classes event listeners and manual bindings (which is to say,
created via BindingUtils use).

Last but not least, every instances created inside a method (I mean locale
variables, not class members) won't stay in memory once method has finished
its execution, provided there's no listener or binding attached to it.

I hope these infos will help you :)
Le 6 juin 2013 20:19, <mo...@comcast.net> a écrit :

> When the user eventually closes the browser, if there's any leaked memory,
> does it get returned to the system, or does it remain leaked thereafter?
>
> ----- Original Message -----
> From: "Alex Harui" <ah...@adobe.com>
> To: users@flex.apache.org
> Sent: Thursday, June 6, 2013 10:12:20 AM
> Subject: Re: when to dispose of objects for garbage collection?
>
> Answers in-line, but I'm not sure what "CREATED in" really means.  Does it
> mean defined in a single MXML file?
>
> On 6/6/13 9:03 AM, "modjklist@comcast.net" <mo...@comcast.net> wrote:
>
> >I should have mentioned regarding question 2 and 3, that the DataGrid,
> >ComboBox, etc., used by the TitleWindow was CREATED in the TitleWindow,
> >and is not used external to the TitleWindow. The questions being whether
> >the TitleWindow automatically cleans up these variables for GC, or
> >requires me to null them first...
> >
> >----- Original Message -----
> >From: modjklist@comcast.net
> >To: users@flex.apache.org
> >Sent: Thursday, June 6, 2013 8:52:05 AM
> >Subject: when to dispose of objects for garbage collection?
> >
> >
> >Can someone help me identify when I need to dispose of an object, array,
> >etc. in a typical Flex app?
> >
> >For example, suppose I have an app with several states and a TitleWindow.
> >
> >I know that if I declare a variable for a state, such as:
> >
> ><fx:Script>
> ><![CDATA[
> >  ...
> >  private var myArr:Array;
> >  ...
> >]]>
> ></fx:Script>
> >
> >that when I no longer need this array (or object, etc.), I should set it
> >to null to inform the garbage collector (GC) it's ready to be picked up.
> >That's because, otherwise, this variable remains in memory, since the
> >state persists throughout the life of the app.
> >
> >But what if this state uses the following function:
> >
> ><fx:Script>
> ><![CDATA[
> >  private var summation:Number;
> >  ...
> >  private function myFunc():void {
> >      var anotherArr:Array=[1,2,3,4,5]
> >      for (var i:int=0; i<anotherArr.length; i++)
> >        summation+=anotherArr[i];
> >  }
> >  ...
> >]]>
> ></fx:Script>
> >
> >QUESTION 1: Do I need to manually null variable myArr2 at the end of
> >function myFunc()? Or, will it be picked up automatically by the GC?
> This particular function will not leak the array (note that it re-creates
> the array every time it gets called which is inefficient).  But if you did:
>
>         private function myFunc():void {
>             var anotherArr:Array=[1,2,3,4,5];
>             var someFunction:Function = function() {
>             }
>
> and someFunction is assigned as an event listener or other callback, then
> anotherArr is on the scope chain and will be held onto until someFunction
> is no longer referenced, which could be long after myFunc() ended.
>
> >
> >How about TitleWindows?
> >
> >QUESTION 2: If I open a TitleWindow (e.g. popup) that contains a
> >DataGrid, do I need to manually null its data provider when I close the
> >TitleWindow? Or, will it be picked up automatically by the GC?
> It depends on who else has references to the dataProvider.  The DG has a
> listener on the dataProvider, so that means the dataProvider has a
> reference to the DG in order to call a function in the DG when the
> dataProvider changes.  If the dataProvider is part of some global model,
> then the DG will be held in memory by the dataProvider.  If the
> dataProvider is only referenced by the DG or other things in the
> TitleWindow, then it will all go away together.
>
>
> >
> >
> >QUESTION 3: This last question also applies to a data provider for
> >ComboBox, or an ArrayList, or an Array that is used in a TitleWindow --
> >do I need to null those as well upon closing the window? Or, will they be
> >picked up automatically by the GC?
> See #2.
>
>

Re: when to dispose of objects for garbage collection?

Posted by OmPrakash Muppirala <bi...@gmail.com>.
Two very good articles here for anyone who wants to learn more about GC in
Flash Player

[1]
http://www.digitalprimates.net/author/codeslinger/2011/10/11/talking-trash-an-overview-of-player-gc/
[2] http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html
(Check
out the interactive simulation on this page as well)

Thanks,
Om

On Thu, Jun 6, 2013 at 11:36 AM, Romain KELIFA <ro...@gmail.com>wrote:

> From what I know, most of the things you create via MXML is automatically
> handled (from GC point of view) by the framework.
>
> However, everything you create via pure AS3 must be handled manually,
> that's why it can be a good practice to implement a dispose() method in
> your components 'abstract' classes (wired on FlexEvent.REMOVE), at least to
> remove all classes event listeners and manual bindings (which is to say,
> created via BindingUtils use).
>
> Last but not least, every instances created inside a method (I mean locale
> variables, not class members) won't stay in memory once method has finished
> its execution, provided there's no listener or binding attached to it.
>
> I hope these infos will help you :)
> Le 6 juin 2013 20:19, <mo...@comcast.net> a écrit :
>
> > When the user eventually closes the browser, if there's any leaked
> memory,
> > does it get returned to the system, or does it remain leaked thereafter?
> >
> > ----- Original Message -----
> > From: "Alex Harui" <ah...@adobe.com>
> > To: users@flex.apache.org
> > Sent: Thursday, June 6, 2013 10:12:20 AM
> > Subject: Re: when to dispose of objects for garbage collection?
> >
> > Answers in-line, but I'm not sure what "CREATED in" really means.  Does
> it
> > mean defined in a single MXML file?
> >
> > On 6/6/13 9:03 AM, "modjklist@comcast.net" <mo...@comcast.net>
> wrote:
> >
> > >I should have mentioned regarding question 2 and 3, that the DataGrid,
> > >ComboBox, etc., used by the TitleWindow was CREATED in the TitleWindow,
> > >and is not used external to the TitleWindow. The questions being whether
> > >the TitleWindow automatically cleans up these variables for GC, or
> > >requires me to null them first...
> > >
> > >----- Original Message -----
> > >From: modjklist@comcast.net
> > >To: users@flex.apache.org
> > >Sent: Thursday, June 6, 2013 8:52:05 AM
> > >Subject: when to dispose of objects for garbage collection?
> > >
> > >
> > >Can someone help me identify when I need to dispose of an object, array,
> > >etc. in a typical Flex app?
> > >
> > >For example, suppose I have an app with several states and a
> TitleWindow.
> > >
> > >I know that if I declare a variable for a state, such as:
> > >
> > ><fx:Script>
> > ><![CDATA[
> > >  ...
> > >  private var myArr:Array;
> > >  ...
> > >]]>
> > ></fx:Script>
> > >
> > >that when I no longer need this array (or object, etc.), I should set it
> > >to null to inform the garbage collector (GC) it's ready to be picked up.
> > >That's because, otherwise, this variable remains in memory, since the
> > >state persists throughout the life of the app.
> > >
> > >But what if this state uses the following function:
> > >
> > ><fx:Script>
> > ><![CDATA[
> > >  private var summation:Number;
> > >  ...
> > >  private function myFunc():void {
> > >      var anotherArr:Array=[1,2,3,4,5]
> > >      for (var i:int=0; i<anotherArr.length; i++)
> > >        summation+=anotherArr[i];
> > >  }
> > >  ...
> > >]]>
> > ></fx:Script>
> > >
> > >QUESTION 1: Do I need to manually null variable myArr2 at the end of
> > >function myFunc()? Or, will it be picked up automatically by the GC?
> > This particular function will not leak the array (note that it re-creates
> > the array every time it gets called which is inefficient).  But if you
> did:
> >
> >         private function myFunc():void {
> >             var anotherArr:Array=[1,2,3,4,5];
> >             var someFunction:Function = function() {
> >             }
> >
> > and someFunction is assigned as an event listener or other callback, then
> > anotherArr is on the scope chain and will be held onto until someFunction
> > is no longer referenced, which could be long after myFunc() ended.
> >
> > >
> > >How about TitleWindows?
> > >
> > >QUESTION 2: If I open a TitleWindow (e.g. popup) that contains a
> > >DataGrid, do I need to manually null its data provider when I close the
> > >TitleWindow? Or, will it be picked up automatically by the GC?
> > It depends on who else has references to the dataProvider.  The DG has a
> > listener on the dataProvider, so that means the dataProvider has a
> > reference to the DG in order to call a function in the DG when the
> > dataProvider changes.  If the dataProvider is part of some global model,
> > then the DG will be held in memory by the dataProvider.  If the
> > dataProvider is only referenced by the DG or other things in the
> > TitleWindow, then it will all go away together.
> >
> >
> > >
> > >
> > >QUESTION 3: This last question also applies to a data provider for
> > >ComboBox, or an ArrayList, or an Array that is used in a TitleWindow --
> > >do I need to null those as well upon closing the window? Or, will they
> be
> > >picked up automatically by the GC?
> > See #2.
> >
> >
>

Re: when to dispose of objects for garbage collection?

Posted by Romain KELIFA <ro...@gmail.com>.
>From what I know, most of the things you create via MXML is automatically
handled (from GC point of view) by the framework.

However, everything you create via pure AS3 must be handled manually,
that's why it can be a good practice to implement a dispose() method in
your components 'abstract' classes (wired on FlexEvent.REMOVE), at least to
remove all classes event listeners and manual bindings (which is to say,
created via BindingUtils use).

Last but not least, every instances created inside a method (I mean locale
variables, not class members) won't stay in memory once method has finished
its execution, provided there's no listener or binding attached to it.

I hope these infos will help you :)
Le 6 juin 2013 20:19, <mo...@comcast.net> a écrit :

> When the user eventually closes the browser, if there's any leaked memory,
> does it get returned to the system, or does it remain leaked thereafter?
>
> ----- Original Message -----
> From: "Alex Harui" <ah...@adobe.com>
> To: users@flex.apache.org
> Sent: Thursday, June 6, 2013 10:12:20 AM
> Subject: Re: when to dispose of objects for garbage collection?
>
> Answers in-line, but I'm not sure what "CREATED in" really means.  Does it
> mean defined in a single MXML file?
>
> On 6/6/13 9:03 AM, "modjklist@comcast.net" <mo...@comcast.net> wrote:
>
> >I should have mentioned regarding question 2 and 3, that the DataGrid,
> >ComboBox, etc., used by the TitleWindow was CREATED in the TitleWindow,
> >and is not used external to the TitleWindow. The questions being whether
> >the TitleWindow automatically cleans up these variables for GC, or
> >requires me to null them first...
> >
> >----- Original Message -----
> >From: modjklist@comcast.net
> >To: users@flex.apache.org
> >Sent: Thursday, June 6, 2013 8:52:05 AM
> >Subject: when to dispose of objects for garbage collection?
> >
> >
> >Can someone help me identify when I need to dispose of an object, array,
> >etc. in a typical Flex app?
> >
> >For example, suppose I have an app with several states and a TitleWindow.
> >
> >I know that if I declare a variable for a state, such as:
> >
> ><fx:Script>
> ><![CDATA[
> >  ...
> >  private var myArr:Array;
> >  ...
> >]]>
> ></fx:Script>
> >
> >that when I no longer need this array (or object, etc.), I should set it
> >to null to inform the garbage collector (GC) it's ready to be picked up.
> >That's because, otherwise, this variable remains in memory, since the
> >state persists throughout the life of the app.
> >
> >But what if this state uses the following function:
> >
> ><fx:Script>
> ><![CDATA[
> >  private var summation:Number;
> >  ...
> >  private function myFunc():void {
> >      var anotherArr:Array=[1,2,3,4,5]
> >      for (var i:int=0; i<anotherArr.length; i++)
> >        summation+=anotherArr[i];
> >  }
> >  ...
> >]]>
> ></fx:Script>
> >
> >QUESTION 1: Do I need to manually null variable myArr2 at the end of
> >function myFunc()? Or, will it be picked up automatically by the GC?
> This particular function will not leak the array (note that it re-creates
> the array every time it gets called which is inefficient).  But if you did:
>
>         private function myFunc():void {
>             var anotherArr:Array=[1,2,3,4,5];
>             var someFunction:Function = function() {
>             }
>
> and someFunction is assigned as an event listener or other callback, then
> anotherArr is on the scope chain and will be held onto until someFunction
> is no longer referenced, which could be long after myFunc() ended.
>
> >
> >How about TitleWindows?
> >
> >QUESTION 2: If I open a TitleWindow (e.g. popup) that contains a
> >DataGrid, do I need to manually null its data provider when I close the
> >TitleWindow? Or, will it be picked up automatically by the GC?
> It depends on who else has references to the dataProvider.  The DG has a
> listener on the dataProvider, so that means the dataProvider has a
> reference to the DG in order to call a function in the DG when the
> dataProvider changes.  If the dataProvider is part of some global model,
> then the DG will be held in memory by the dataProvider.  If the
> dataProvider is only referenced by the DG or other things in the
> TitleWindow, then it will all go away together.
>
>
> >
> >
> >QUESTION 3: This last question also applies to a data provider for
> >ComboBox, or an ArrayList, or an Array that is used in a TitleWindow --
> >do I need to null those as well upon closing the window? Or, will they be
> >picked up automatically by the GC?
> See #2.
>
>

Re: when to dispose of objects for garbage collection?

Posted by mo...@comcast.net.
By CREATED I mean defined in the TitleWindow like I showed below for anotherArr. 

----- Original Message -----
From: "Alex Harui" <ah...@adobe.com>
To: users@flex.apache.org
Sent: Thursday, June 6, 2013 10:12:20 AM
Subject: Re: when to dispose of objects for garbage collection?

Answers in-line, but I'm not sure what "CREATED in" really means.  Does it
mean defined in a single MXML file?

On 6/6/13 9:03 AM, "modjklist@comcast.net" <mo...@comcast.net> wrote:

>I should have mentioned regarding question 2 and 3, that the DataGrid,
>ComboBox, etc., used by the TitleWindow was CREATED in the TitleWindow,
>and is not used external to the TitleWindow. The questions being whether
>the TitleWindow automatically cleans up these variables for GC, or
>requires me to null them first...
>
>----- Original Message -----
>From: modjklist@comcast.net
>To: users@flex.apache.org
>Sent: Thursday, June 6, 2013 8:52:05 AM
>Subject: when to dispose of objects for garbage collection?
>
>
>Can someone help me identify when I need to dispose of an object, array,
>etc. in a typical Flex app?
>
>For example, suppose I have an app with several states and a TitleWindow.
>
>I know that if I declare a variable for a state, such as:
>
><fx:Script> 
><![CDATA[ 
>  ...
>  private var myArr:Array;
>  ...	
>]]>
></fx:Script>
>
>that when I no longer need this array (or object, etc.), I should set it
>to null to inform the garbage collector (GC) it's ready to be picked up.
>That's because, otherwise, this variable remains in memory, since the
>state persists throughout the life of the app.
>
>But what if this state uses the following function:
>
><fx:Script> 
><![CDATA[ 
>  private var summation:Number;
>  ...	
>  private function myFunc():void {
>      var anotherArr:Array=[1,2,3,4,5]
>      for (var i:int=0; i<anotherArr.length; i++)
>        summation+=anotherArr[i];
>  }
>  ...
>]]>
></fx:Script>
>
>QUESTION 1: Do I need to manually null variable myArr2 at the end of
>function myFunc()? Or, will it be picked up automatically by the GC?
This particular function will not leak the array (note that it re-creates
the array every time it gets called which is inefficient).  But if you did:

	private function myFunc():void {
	    var anotherArr:Array=[1,2,3,4,5];
            var someFunction:Function = function() {
            }

and someFunction is assigned as an event listener or other callback, then
anotherArr is on the scope chain and will be held onto until someFunction
is no longer referenced, which could be long after myFunc() ended.

>
>How about TitleWindows?
>
>QUESTION 2: If I open a TitleWindow (e.g. popup) that contains a
>DataGrid, do I need to manually null its data provider when I close the
>TitleWindow? Or, will it be picked up automatically by the GC?
It depends on who else has references to the dataProvider.  The DG has a
listener on the dataProvider, so that means the dataProvider has a
reference to the DG in order to call a function in the DG when the
dataProvider changes.  If the dataProvider is part of some global model,
then the DG will be held in memory by the dataProvider.  If the
dataProvider is only referenced by the DG or other things in the
TitleWindow, then it will all go away together.


> 
>
>QUESTION 3: This last question also applies to a data provider for
>ComboBox, or an ArrayList, or an Array that is used in a TitleWindow --
>do I need to null those as well upon closing the window? Or, will they be
>picked up automatically by the GC?
See #2.


Re: when to dispose of objects for garbage collection?

Posted by Alex Harui <ah...@adobe.com>.
It if it doesn't returned, then it is the browser's fault, not yours.

On 6/6/13 11:19 AM, "modjklist@comcast.net" <mo...@comcast.net> wrote:

>When the user eventually closes the browser, if there's any leaked
>memory, does it get returned to the system, or does it remain leaked
>thereafter?
>
>----- Original Message -----
>From: "Alex Harui" <ah...@adobe.com>
>To: users@flex.apache.org
>Sent: Thursday, June 6, 2013 10:12:20 AM
>Subject: Re: when to dispose of objects for garbage collection?
>
>Answers in-line, but I'm not sure what "CREATED in" really means.  Does it
>mean defined in a single MXML file?
>
>On 6/6/13 9:03 AM, "modjklist@comcast.net" <mo...@comcast.net> wrote:
>
>>I should have mentioned regarding question 2 and 3, that the DataGrid,
>>ComboBox, etc., used by the TitleWindow was CREATED in the TitleWindow,
>>and is not used external to the TitleWindow. The questions being whether
>>the TitleWindow automatically cleans up these variables for GC, or
>>requires me to null them first...
>>
>>----- Original Message -----
>>From: modjklist@comcast.net
>>To: users@flex.apache.org
>>Sent: Thursday, June 6, 2013 8:52:05 AM
>>Subject: when to dispose of objects for garbage collection?
>>
>>
>>Can someone help me identify when I need to dispose of an object, array,
>>etc. in a typical Flex app?
>>
>>For example, suppose I have an app with several states and a TitleWindow.
>>
>>I know that if I declare a variable for a state, such as:
>>
>><fx:Script> 
>><![CDATA[ 
>>  ...
>>  private var myArr:Array;
>>  ...	
>>]]>
>></fx:Script>
>>
>>that when I no longer need this array (or object, etc.), I should set it
>>to null to inform the garbage collector (GC) it's ready to be picked up.
>>That's because, otherwise, this variable remains in memory, since the
>>state persists throughout the life of the app.
>>
>>But what if this state uses the following function:
>>
>><fx:Script> 
>><![CDATA[ 
>>  private var summation:Number;
>>  ...	
>>  private function myFunc():void {
>>      var anotherArr:Array=[1,2,3,4,5]
>>      for (var i:int=0; i<anotherArr.length; i++)
>>        summation+=anotherArr[i];
>>  }
>>  ...
>>]]>
>></fx:Script>
>>
>>QUESTION 1: Do I need to manually null variable myArr2 at the end of
>>function myFunc()? Or, will it be picked up automatically by the GC?
>This particular function will not leak the array (note that it re-creates
>the array every time it gets called which is inefficient).  But if you
>did:
>
>	private function myFunc():void {
>	    var anotherArr:Array=[1,2,3,4,5];
>            var someFunction:Function = function() {
>            }
>
>and someFunction is assigned as an event listener or other callback, then
>anotherArr is on the scope chain and will be held onto until someFunction
>is no longer referenced, which could be long after myFunc() ended.
>
>>
>>How about TitleWindows?
>>
>>QUESTION 2: If I open a TitleWindow (e.g. popup) that contains a
>>DataGrid, do I need to manually null its data provider when I close the
>>TitleWindow? Or, will it be picked up automatically by the GC?
>It depends on who else has references to the dataProvider.  The DG has a
>listener on the dataProvider, so that means the dataProvider has a
>reference to the DG in order to call a function in the DG when the
>dataProvider changes.  If the dataProvider is part of some global model,
>then the DG will be held in memory by the dataProvider.  If the
>dataProvider is only referenced by the DG or other things in the
>TitleWindow, then it will all go away together.
>
>
>> 
>>
>>QUESTION 3: This last question also applies to a data provider for
>>ComboBox, or an ArrayList, or an Array that is used in a TitleWindow --
>>do I need to null those as well upon closing the window? Or, will they be
>>picked up automatically by the GC?
>See #2.
>


Re: when to dispose of objects for garbage collection?

Posted by mo...@comcast.net.
When the user eventually closes the browser, if there's any leaked memory, does it get returned to the system, or does it remain leaked thereafter?

----- Original Message -----
From: "Alex Harui" <ah...@adobe.com>
To: users@flex.apache.org
Sent: Thursday, June 6, 2013 10:12:20 AM
Subject: Re: when to dispose of objects for garbage collection?

Answers in-line, but I'm not sure what "CREATED in" really means.  Does it
mean defined in a single MXML file?

On 6/6/13 9:03 AM, "modjklist@comcast.net" <mo...@comcast.net> wrote:

>I should have mentioned regarding question 2 and 3, that the DataGrid,
>ComboBox, etc., used by the TitleWindow was CREATED in the TitleWindow,
>and is not used external to the TitleWindow. The questions being whether
>the TitleWindow automatically cleans up these variables for GC, or
>requires me to null them first...
>
>----- Original Message -----
>From: modjklist@comcast.net
>To: users@flex.apache.org
>Sent: Thursday, June 6, 2013 8:52:05 AM
>Subject: when to dispose of objects for garbage collection?
>
>
>Can someone help me identify when I need to dispose of an object, array,
>etc. in a typical Flex app?
>
>For example, suppose I have an app with several states and a TitleWindow.
>
>I know that if I declare a variable for a state, such as:
>
><fx:Script> 
><![CDATA[ 
>  ...
>  private var myArr:Array;
>  ...	
>]]>
></fx:Script>
>
>that when I no longer need this array (or object, etc.), I should set it
>to null to inform the garbage collector (GC) it's ready to be picked up.
>That's because, otherwise, this variable remains in memory, since the
>state persists throughout the life of the app.
>
>But what if this state uses the following function:
>
><fx:Script> 
><![CDATA[ 
>  private var summation:Number;
>  ...	
>  private function myFunc():void {
>      var anotherArr:Array=[1,2,3,4,5]
>      for (var i:int=0; i<anotherArr.length; i++)
>        summation+=anotherArr[i];
>  }
>  ...
>]]>
></fx:Script>
>
>QUESTION 1: Do I need to manually null variable myArr2 at the end of
>function myFunc()? Or, will it be picked up automatically by the GC?
This particular function will not leak the array (note that it re-creates
the array every time it gets called which is inefficient).  But if you did:

	private function myFunc():void {
	    var anotherArr:Array=[1,2,3,4,5];
            var someFunction:Function = function() {
            }

and someFunction is assigned as an event listener or other callback, then
anotherArr is on the scope chain and will be held onto until someFunction
is no longer referenced, which could be long after myFunc() ended.

>
>How about TitleWindows?
>
>QUESTION 2: If I open a TitleWindow (e.g. popup) that contains a
>DataGrid, do I need to manually null its data provider when I close the
>TitleWindow? Or, will it be picked up automatically by the GC?
It depends on who else has references to the dataProvider.  The DG has a
listener on the dataProvider, so that means the dataProvider has a
reference to the DG in order to call a function in the DG when the
dataProvider changes.  If the dataProvider is part of some global model,
then the DG will be held in memory by the dataProvider.  If the
dataProvider is only referenced by the DG or other things in the
TitleWindow, then it will all go away together.


> 
>
>QUESTION 3: This last question also applies to a data provider for
>ComboBox, or an ArrayList, or an Array that is used in a TitleWindow --
>do I need to null those as well upon closing the window? Or, will they be
>picked up automatically by the GC?
See #2.


Re: when to dispose of objects for garbage collection?

Posted by Alex Harui <ah...@adobe.com>.
Answers in-line, but I'm not sure what "CREATED in" really means.  Does it
mean defined in a single MXML file?

On 6/6/13 9:03 AM, "modjklist@comcast.net" <mo...@comcast.net> wrote:

>I should have mentioned regarding question 2 and 3, that the DataGrid,
>ComboBox, etc., used by the TitleWindow was CREATED in the TitleWindow,
>and is not used external to the TitleWindow. The questions being whether
>the TitleWindow automatically cleans up these variables for GC, or
>requires me to null them first...
>
>----- Original Message -----
>From: modjklist@comcast.net
>To: users@flex.apache.org
>Sent: Thursday, June 6, 2013 8:52:05 AM
>Subject: when to dispose of objects for garbage collection?
>
>
>Can someone help me identify when I need to dispose of an object, array,
>etc. in a typical Flex app?
>
>For example, suppose I have an app with several states and a TitleWindow.
>
>I know that if I declare a variable for a state, such as:
>
><fx:Script> 
><![CDATA[ 
>  ...
>  private var myArr:Array;
>  ...	
>]]>
></fx:Script>
>
>that when I no longer need this array (or object, etc.), I should set it
>to null to inform the garbage collector (GC) it's ready to be picked up.
>That's because, otherwise, this variable remains in memory, since the
>state persists throughout the life of the app.
>
>But what if this state uses the following function:
>
><fx:Script> 
><![CDATA[ 
>  private var summation:Number;
>  ...	
>  private function myFunc():void {
>      var anotherArr:Array=[1,2,3,4,5]
>      for (var i:int=0; i<anotherArr.length; i++)
>        summation+=anotherArr[i];
>  }
>  ...
>]]>
></fx:Script>
>
>QUESTION 1: Do I need to manually null variable myArr2 at the end of
>function myFunc()? Or, will it be picked up automatically by the GC?
This particular function will not leak the array (note that it re-creates
the array every time it gets called which is inefficient).  But if you did:

	private function myFunc():void {
	    var anotherArr:Array=[1,2,3,4,5];
            var someFunction:Function = function() {
            }

and someFunction is assigned as an event listener or other callback, then
anotherArr is on the scope chain and will be held onto until someFunction
is no longer referenced, which could be long after myFunc() ended.

>
>How about TitleWindows?
>
>QUESTION 2: If I open a TitleWindow (e.g. popup) that contains a
>DataGrid, do I need to manually null its data provider when I close the
>TitleWindow? Or, will it be picked up automatically by the GC?
It depends on who else has references to the dataProvider.  The DG has a
listener on the dataProvider, so that means the dataProvider has a
reference to the DG in order to call a function in the DG when the
dataProvider changes.  If the dataProvider is part of some global model,
then the DG will be held in memory by the dataProvider.  If the
dataProvider is only referenced by the DG or other things in the
TitleWindow, then it will all go away together.


> 
>
>QUESTION 3: This last question also applies to a data provider for
>ComboBox, or an ArrayList, or an Array that is used in a TitleWindow --
>do I need to null those as well upon closing the window? Or, will they be
>picked up automatically by the GC?
See #2.


Re: when to dispose of objects for garbage collection?

Posted by mo...@comcast.net.
I should have mentioned regarding question 2 and 3, that the DataGrid, ComboBox, etc., used by the TitleWindow was CREATED in the TitleWindow, and is not used external to the TitleWindow. The questions being whether the TitleWindow automatically cleans up these variables for GC, or requires me to null them first...

----- Original Message -----
From: modjklist@comcast.net
To: users@flex.apache.org
Sent: Thursday, June 6, 2013 8:52:05 AM
Subject: when to dispose of objects for garbage collection?


Can someone help me identify when I need to dispose of an object, array, etc. in a typical Flex app? 

For example, suppose I have an app with several states and a TitleWindow. 

I know that if I declare a variable for a state, such as: 

<fx:Script> 
<![CDATA[ 
  ...
  private var myArr:Array;
  ...	
]]>
</fx:Script>

that when I no longer need this array (or object, etc.), I should set it to null to inform the garbage collector (GC) it's ready to be picked up. That's because, otherwise, this variable remains in memory, since the state persists throughout the life of the app.

But what if this state uses the following function:

<fx:Script> 
<![CDATA[ 
  private var summation:Number;
  ...	
  private function myFunc():void {
      var anotherArr:Array=[1,2,3,4,5]
      for (var i:int=0; i<anotherArr.length; i++)
        summation+=anotherArr[i];
  }
  ...
]]>
</fx:Script>

QUESTION 1: Do I need to manually null variable myArr2 at the end of function myFunc()? Or, will it be picked up automatically by the GC?

How about TitleWindows? 

QUESTION 2: If I open a TitleWindow (e.g. popup) that contains a DataGrid, do I need to manually null its data provider when I close the TitleWindow? Or, will it be picked up automatically by the GC? 

QUESTION 3: This last question also applies to a data provider for ComboBox, or an ArrayList, or an Array that is used in a TitleWindow -- do I need to null those as well upon closing the window? Or, will they be picked up automatically by the GC?

Thanks in advance for any comments.