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

  • Home
  • SEARCH
  • 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 5849933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:04:13+00:00 2026-05-22T13:04:13+00:00

I have a timer in C# which executes some code inside it’s method. Inside

  • 0

I have a timer in C# which executes some code inside it’s method. Inside the code I’m using several temporary objects.

  1. If I have something like Foo o = new Foo(); inside the method, does that mean that each time the timer ticks, I’m creating a new object and a new reference to that object?

  2. If I have string foo = null and then I just put something temporal in foo, is it the same as above?

  3. Does the garbage collector ever delete the object and the reference or objects are continually created and stay in memory?

  4. If I just declare Foo o; and not point it to any instance, isn’t that disposed when the method ends?

  5. If I want to ensure that everything is deleted, what is the best way of doing it:

    • with the using statement inside the method
    • by calling dispose method at the end
    • by putting Foo o; outside the timer’s method and just make the assignment o = new Foo() inside, so then the pointer to the object is deleted after the method ends, the garbage collector will delete the object.
  • 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-22T13:04:13+00:00Added an answer on May 22, 2026 at 1:04 pm

    1.If I have something like Foo o = new Foo(); inside the method, does that
    mean that each time the timer ticks,
    I’m creating a new object and a new
    reference to that object?

    Yes.

    2.If I have string foo = null and then I just put something temporal in foo,
    is it the same as above?

    If you are asking if the behavior is the same then yes.

    3.Does the garbage collector ever delete the object and the reference or
    objects are continually created and
    stay in memory?

    The memory used by those objects is most certainly collected after the references are deemed to be unused.

    4.If I just declare Foo o; and not point it to any instance, isn’t that
    disposed when the method ends?

    No, since no object was created then there is no object to collect (dispose is not the right word).

    5.If I want to ensure that everything is deleted, what is the best way of
    doing it

    If the object’s class implements IDisposable then you certainly want to greedily call Dispose as soon as possible. The using keyword makes this easier because it calls Dispose automatically in an exception-safe way.

    Other than that there really is nothing else you need to do except to stop using the object. If the reference is a local variable then when it goes out of scope it will be eligible for collection.1 If it is a class level variable then you may need to assign null to it to make it eligible before the containing class is eligible.


    1This is technically incorrect (or at least a little misleading). An object can be eligible for collection long before it goes out of scope. The CLR is optimized to collect memory when it detects that a reference is no longer used. In extreme cases the CLR can collect an object even while one of its methods is still executing!

    Update:

    Here is an example that demonstrates that the GC will collect objects even though they may still be in-scope. You have to compile a Release build and run this outside of the debugger.

    static void Main(string[] args)
    {
        Console.WriteLine("Before allocation");
        var bo = new BigObject();
        Console.WriteLine("After allocation");
        bo.SomeMethod();
        Console.ReadLine();
        // The object is technically in-scope here which means it must still be rooted.
    }
    
    private class BigObject
    {
        private byte[] LotsOfMemory = new byte[Int32.MaxValue / 4];
    
        public BigObject()
        {
            Console.WriteLine("BigObject()");
        }
    
        ~BigObject()
        {
            Console.WriteLine("~BigObject()");
        }
    
        public void SomeMethod()
        {
            Console.WriteLine("Begin SomeMethod");
            GC.Collect();
            GC.WaitForPendingFinalizers();
            Console.WriteLine("End SomeMethod");
        }
    }
    

    On my machine the finalizer is run while SomeMethod is still executing!

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

Sidebar

Related Questions

I have a control application - using asp.net webservices. I have a timer which
I'm developing an ASP.NET forms webapplication using C#. I have a method which creates
I have the following code which does nothing but reading some values from a
I have a piece of code in ANSI C which uses the time.h library
I have a C# application which generates .NET functions at run-time and executes them.
i have a input tag which is non editable, but some times i need
Okay, so I've got some C code to perform a mathematical operation which could,
This is what I want to do: Have a timer with some interval In
I have now something like this: public class Service1 : System.Web.Services.WebService { [WebMethod] public
I would like to execute a method which can only be called once my

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.