Could the GC collect a static variable?
-
Is it possible for the GC to collect a static variable if it determines that no further references to it exist? For example:
class LockFile {
static FileStream lockFile;
static LockFile ()
{
lockFile = File.Open(".lock", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
}
}If the
LockFile
class is being loaded somewhere in my code, could the file stream be collected before AppDomainUnload? -
Is it possible for the GC to collect a static variable if it determines that no further references to it exist? For example:
class LockFile {
static FileStream lockFile;
static LockFile ()
{
lockFile = File.Open(".lock", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
}
}If the
LockFile
class is being loaded somewhere in my code, could the file stream be collected before AppDomainUnload?AFAIK, static variables are not garbage collected. It will be cleaned up when the application domain unloads.
Navaneeth How to use google | Ask smart questions
-
AFAIK, static variables are not garbage collected. It will be cleaned up when the application domain unloads.
Navaneeth How to use google | Ask smart questions
Thanks a lot
-
AFAIK, static variables are not garbage collected. It will be cleaned up when the application domain unloads.
Navaneeth How to use google | Ask smart questions
If the variable is private, and not referenced in the class (except in the static constructor), would this still be the case? Thank you.
-
If the variable is private, and not referenced in the class (except in the static constructor), would this still be the case? Thank you.
I'm not sure. But why on earth would you want to do this? A private static variable that isn't referenced anywhere. What are you using it for? To be honest, I wouldn't be at all surprised if the compiler just removes it.
Simon
-
I'm not sure. But why on earth would you want to do this? A private static variable that isn't referenced anywhere. What are you using it for? To be honest, I wouldn't be at all surprised if the compiler just removes it.
Simon
Well, I'm loading an unmanaged library and maintaining a SafeHandle over it. The handle must be maintained as long as the application is running, and must be released when the application is unloaded. First I thought about using
GC.KeepAlive(libHandle)
in some public static methods. But I'm wondering whether I really need to callGC.KeepAlive
. Thanks for your help. -
Well, I'm loading an unmanaged library and maintaining a SafeHandle over it. The handle must be maintained as long as the application is running, and must be released when the application is unloaded. First I thought about using
GC.KeepAlive(libHandle)
in some public static methods. But I'm wondering whether I really need to callGC.KeepAlive
. Thanks for your help.Ahh, I see what you are trying to do. My assumption would be that static variables do not get collected. Even if you don't currently have any references too them, because at any point, you could have a reference by using them for something as they are accessible from everywhere. But, like I said, if private and it's not used anywhere in the class, I wouldn't be all the surprised if the compiler just removed it. You're probably going to struggle to get an answer to this. It's quite specific. Possibly try asking on the MSDN forum for the CLR, perhaps someone in MS could give you an answer. Alternatively, I believe the GCHandle class can be used to track object lifetime, so you could test it out. Good luck.
Simon
-
Ahh, I see what you are trying to do. My assumption would be that static variables do not get collected. Even if you don't currently have any references too them, because at any point, you could have a reference by using them for something as they are accessible from everywhere. But, like I said, if private and it's not used anywhere in the class, I wouldn't be all the surprised if the compiler just removed it. You're probably going to struggle to get an answer to this. It's quite specific. Possibly try asking on the MSDN forum for the CLR, perhaps someone in MS could give you an answer. Alternatively, I believe the GCHandle class can be used to track object lifetime, so you could test it out. Good luck.
Simon
Thanks a lot! I'll try to check with MSDN. Meanwhile, I made this workaround, tried stopping it from within the debugger (to simulate AppDomainUnload), and it worked OK. I'm not sure how safe it is, but it seems to be working fine.
class MyApp {
private static SafeHandle handle = new MyAppSafeHandle();
public static KeepAlive() { GC.KeepAlive(handle); }
}
class MyLibSafeHandle : SafeHandle {
public MyLibSafeHandle()
: base(IntPtr.Zero, true)
{ MyApp.KeepAlive(); }protected override ReleaseHandle() { externRelease(handle); MyApp.KeepAlive(); }
}