The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Answer: What is the lifetime of local instances?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Eric Gunnerson

Posts: 1006
Nickname: ericgu
Registered: Aug, 2003

Eric Gunnerson is a program manager on the Visual C# team
Answer: What is the lifetime of local instances? Posted: Jul 23, 2004 5:23 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Eric Gunnerson.
Original Post: Answer: What is the lifetime of local instances?
Feed Title: Eric Gunnerson's C# Compendium
Feed URL: /msdnerror.htm?aspxerrorpath=/ericgu/Rss.aspx
Feed Description: Eric comments on C#, programming and dotnet in general, and the aerodynamic characteristics of the red-nosed flying squirrel of the Lesser Antilles
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Eric Gunnerson
Latest Posts From Eric Gunnerson's C# Compendium

Advertisement

Answer to this poser

I wasn't sure the answer to this question was observable, so I wrote a short program:

using System;
class Early
{
  ~Early()
  {
     Console.WriteLine("Early Cleaned Up");
  }
}
class Test
{
  public static void Main()
  {
     Early e = new Early();

     GC.Collect();
     GC.WaitForPendingFinalizers();
     Console.WriteLine("Done Waiting");
  }
}

The output from this is

Early Cleaned Up
Done Waiting

In other words, there is no guarantee that a local variable will remain live until the end of a scope if it isn't used. The runtime is free to analyze the code that it has and determine what there are no further usages of a variable beyond a certain point, and therefore not keep that variable live beyond that point (ie not treat it as a root for the purposes of GC).

Read: Answer: What is the lifetime of local instances?

Topic: [CoolTool] BlogWave: Scheduled RSS feed generation and publishing from multiple sources Previous Topic   Next Topic Topic: Use of Posters and Flow Charts in Lieu of Prose for Documentation - Is a Visio is Worth a...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use