Share via


Garbage Collection and static variables

Question

Tuesday, December 11, 2007 12:22 PM

I have a class that has a static member variable (which reference unmanaged C++ code) that is in initialize in the static constructor.  I need this varialble to always be available. (not collected by the garbage collector).  Since it is static, will it be deemed to be collected by the garbage collector or are static variables immune?  Below is what I am tryint to do.

public class Tasks
{
   protected static TaskConnection Task = null;

  static Tasks()
  {
      Task = new TaskConnection();
  }

  .....
}

Thanks,
Chris

All replies (5)

Tuesday, December 11, 2007 1:34 PM ✅Answered

It will live as long as something holds a reference to it. So as long as an instance of your class exists and is pointed to, it will stay. 


Tuesday, December 11, 2007 1:38 PM ✅Answered

Thanks, that is what I was wanting to know!

 Chris


Tuesday, December 11, 2007 1:31 PM

As far I know, static variables lives till the application ends. 


Wednesday, December 12, 2007 3:37 AM

It will live as long as something holds a reference to it. So as long as an instance of your class exists and is pointed to, it will stay. 

 

To be picky, in this case the question was about a static constructor initializing a static field - there's no need to actually hold a reference to an instance of the class once the static constructor has been invoked, which it will be before the first time the type is used.

The reference in this case is held by the static field of the type, not by an instance of the type, which is why it'll survive garbage collection. 

Also, explicit static constructors should be avoided because performance may suffer. The reason for this has to do with the statement above: "will be called before the first the type is used". Just when is this? The C# compiler, the CLR and the JIT compiler works together on this issue, which is complex. Somewhat simplified, C# thinks that if you have an explicit static constructor, you want this to be called at the precisely right time. If you do not have an explicit constructor, but have static field intializers, C# generates an implicit (but identical) static constructor for you - but in this case it things that you do not worry so much about just when the static constructor is called, and it can thus be called at any time as long as it's called before the first field is used.

Explicit static constructor: The JIT compiler must check if it's been called whenever it compiles a reference to the type, and if it's not, it must emit a call to the constructor. If this is an a loop or frequently called method, you'll be paying an expensive performance cost due to the need for thread synchronization when determining if it's already been called or not.

Implicit static constructor: The CLR can call the the constructor when the type is loaded (i.e. earlier) and the JIT compiler will never have to emit code to call the constructor at all. Better performance. 

You can achieve the same effect as an explicit static constructor (except for the above timing/performance behavior) by calling a static method returning an instance of the type the field is initialized to, in the field initializer, thus getting the best of both worlds. Unless you really do want the constructor to be called at precisely the right time, and not a second earlier.
 

 


Wednesday, July 15, 2009 1:50 AM

 Hi,

i have the following code

 

namespace STATICExample{

public class Test

{

static string **s=**string.Empty;public static string S

{

get

{

return Test.s;

}

set

{

Test.s =value**;**

}

}

}

class Program

{

static void **Main(**string [] args)

** {**

Test.S ="Demo";

}

}

}

For the above code where will be value of the static member property will be save .If it is going to be save in Heap memory how bcoz iam not creating object(not creating instance using new keyword).how i will be save .if its saved in Heap memory where the reference will be save and how the garbage collector reclaims the memory and when i will reclamin.