5 Replies Latest reply on Jul 26, 2011 6:33 AM by kglad

    Doubt on creating new instance of a class

    M.S.H Level 1

      Hi,

      I have a situation wherein I would like to know if there is any impact on performance.

       

      I have two custom classes customClass1.as & customClass2.as written below:

       

      class customClass1 {

           /* Variables are declared here*/

       

       

           public function customClass1() {

                /* Does some initialisation */

           }

       

       

           /* Contains number of methods to perform various activities */

           private function activity1():void {

       

           }

           ...

           ...

           private function activityN():void {

       

           }

      }

       

       

      class customClass2 {

       

           private var class1Instance:customClass1;

       

           /* Variables are declared here*/

       

          

           public function customClass2() {

                /* Does some initialisation */

           }

       

       

           /* Contains number of methods to perform various activities */

           private function activity1():void {

       

           }

           ...

           ...

           /* This function is an event handler */

           private function activityK():void {

                /* Here new instance of class 1 is created without making the previously created instance to NULL and this is done many times over the              * period of time while application's running

                 *

                 * Does this has any impact on performance?

                 * What happens to the earlier instance when new one is created and assigned to same variable?

                 */

       

                class1Instance = new customClass1();

       

                /* Does something */

           }

           ...

           ...

           private function activityN():void {

       

           }

      }

       

      Thanks for your time and suggestions.

       

      Regards,

      MSH

        • 1. Re: Doubt on creating new instance of a class
          kglad Adobe Community Professional & MVP

          yes, it has an impact.  how large an impact depends on what you're creating in customClass1.

           

          if you're creating an on-stage object, the impact can be substantial if you're creating more than a few instances.

          1 person found this helpful
          • 2. Re: Doubt on creating new instance of a class
            M.S.H Level 1

            That means all the instances created will remain in memory, is that correct? I would like to know in depth of this object oriented concept.

            1. if any of the instance created is not assigned NULL then will it never garbage collected ?

            2. Let me consider this situation: I generate a swf file from customClass1 & 2 and load it from another swf. When I unload this swf file will the memory consumed  is freed automatically after unload?

            3. Let me assume I create N number of instances of customClass1 and only the last instance variable is assigned null; does that mean only last instance is garbage collected and rest all remains in memory till the application is closed?

             

            Is there any reference material on how memory management is done in flash player?

             

            kglad, thanks for your time.

             

            Regards,

            MSH

            • 3. Re: Doubt on creating new instance of a class
              kglad Adobe Community Professional & MVP

              the following applies to as2.  it's different in as3.

               

              first, i'm going to assume all your objects are movieclips or buttons.

               

               

              That means all the instances created will remain in memory, is that correct? I would like to know in depth of this object oriented concept.

              1. if any of the instance created is not assigned NULL then will it never garbage collected ?

              not necessarily.  in as2, you apply removeMovieClip() to a movieclip to free memory.  nulling is used in as3.

              2. Let me consider this situation: I generate a swf file from customClass1 & 2 and load it from another swf. When I unload this swf file will the memory consumed  is freed automatically after unload?

              unloading's not enough.   apply removeMovieClip() to the load-target.

              3. Let me assume I create N number of instances of customClass1 and only the last instance variable is assigned null; does that mean only last instance is garbage collected and rest all remains in memory till the application is closed?

              not necessarily.

              in as2, you must use removeMovieClip() for movieclips and extend that method to buttons to clear buttons from memory.

              Is there any reference material on how memory management is done in flash player?

              you want to read about garbage collection (aka gc).  but be careful because almost all that you'll find is about as3.  you'll have to check carefully to see is something relates to as2.

              1 person found this helpful
              • 4. Re: Doubt on creating new instance of a class
                M.S.H Level 1

                Thanks kglad.. That clears my doubts...

                • 5. Re: Doubt on creating new instance of a class
                  kglad Adobe Community Professional & MVP

                  you're welcome.

                   

                  p.s.  in the future, please mark helpful/correct responses, if there are any.