Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Could the GC collect a static variable?

Could the GC collect a static variable?

Scheduled Pinned Locked Moved C#
comtutorialquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HosamAly
    wrote on last edited by
    #1

    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?

    My LinkedIn Profile

    N 1 Reply Last reply
    0
    • H HosamAly

      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?

      My LinkedIn Profile

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      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

      H 2 Replies Last reply
      0
      • N N a v a n e e t h

        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

        H Offline
        H Offline
        HosamAly
        wrote on last edited by
        #3

        Thanks a lot

        My LinkedIn Profile

        1 Reply Last reply
        0
        • N N a v a n e e t h

          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

          H Offline
          H Offline
          HosamAly
          wrote on last edited by
          #4

          If the variable is private, and not referenced in the class (except in the static constructor), would this still be the case? Thank you.

          My LinkedIn Profile

          S 1 Reply Last reply
          0
          • H HosamAly

            If the variable is private, and not referenced in the class (except in the static constructor), would this still be the case? Thank you.

            My LinkedIn Profile

            S Offline
            S Offline
            Simon P Stevens
            wrote on last edited by
            #5

            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

            H 1 Reply Last reply
            0
            • S Simon P Stevens

              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

              H Offline
              H Offline
              HosamAly
              wrote on last edited by
              #6

              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 call GC.KeepAlive. Thanks for your help.

              My LinkedIn Profile

              S 1 Reply Last reply
              0
              • H HosamAly

                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 call GC.KeepAlive. Thanks for your help.

                My LinkedIn Profile

                S Offline
                S Offline
                Simon P Stevens
                wrote on last edited by
                #7

                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

                H 1 Reply Last reply
                0
                • S Simon P Stevens

                  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

                  H Offline
                  H Offline
                  HosamAly
                  wrote on last edited by
                  #8

                  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();
                  }
                  

                  }

                  My LinkedIn Profile

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups