Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7030327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:39:21+00:00 2026-05-28T00:39:21+00:00

I have the following code in a class function : public function foo():void {

  • 0

I have the following code in a class function :

public function foo():void
{
    var timer:Timer = new Timer(10000,1);
    timer.addEventListener(TimerEvent.TIMER_COMPLETE,onTimerComplete);
    timer.start();
}

public function onTimerComplete(e:TimerEvent):void
{
  // do stuff
}

The above code works most of the time but my concern is what happens if timer gets garbage collected? Is it possible that onTimerComplete will never fire because there are no other references to timer?

I know timer has an internal list of handlers but that won’t keep it from being GC’ed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:39:22+00:00Added an answer on May 28, 2026 at 12:39 am

    There are some references on the web to running timers never being garbage collected, e.g.:

    Just to be clear: even if you have no references to a Timer, as long
    as the timer is running, it will not be Garbage Collected (think of
    it as if the runtime was keeping a reference to running timers).

    by Arno Gourdol on the Adobe AIR Team

    but I haven’t been able to find an authoritative source.

    Probably best to not rely on this special behavior and instead make timer a class-level variable, though.

    Answers suggesting that event listeners are keeping the timer from being garbage collected are incorrect. The reference is from the timer to the listener function (onTimerComplete), so if the timer is reachable then the listener function won’t be garbage collected, but not vice versa. This is easily tested:

    <?xml version="1.0" encoding="utf-8"?>
    <s:Application
    xmlns:s="library://ns.adobe.com/flex/spark"
    creationComplete="application1_creationCompleteHandler(event)">
    
    <fx:Script>
        <![CDATA[
    private var _gcTimer:Timer;
    
    protected function application1_creationCompleteHandler(event:FlexEvent):void {
        var timer:Timer = new Timer(30, 4);
        timer.addEventListener(TimerEvent.TIMER, onTimer, false, 0, true);
        
        var sprite:Sprite = new Sprite();
        sprite.addEventListener(Event.ENTER_FRAME, onSprite, false, 0, true);
        
        _gcTimer = new Timer(59, 1);
        _gcTimer.addEventListener(TimerEvent.TIMER, garbageCollect);
    
        timer.start();
        _gcTimer.start();
    }
    
    private function onTimer(event:TimerEvent):void {
        trace("timer");
    }
    
    private function onSprite(event:Event):void {
        trace("sprite");
    }
    ]]>
    </fx:Script>
    </s:Application>
    

    Output:

    sprite
    timer
    sprite
    timer
    Collecting garbage
    timer
    timer

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
I have following code public class TEST { public static void main(String arg[]){ try
I have the following code: class Program { static void Main(string[] args) { new
I have the following code: Public Class Form1 Private Function Step1_Execute() As Boolean Return
I have the following code: public class ClassExample { void DoSomthing<T>(string name, T value)
Suppose I have the following code: class siteMS { ... function __CONSTRUCT() { require
I have following code class User attr_accessor :name end u = User.new u.name =
I have the following code: class Foo<T> where T : struct { private T
I have the following code: class asd { public: int b; asd() { b
When I compile the following code using g++ class A {}; void foo(A&) {}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.