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. C# hierarchical data structure and .NET garbage collection ?

C# hierarchical data structure and .NET garbage collection ?

Scheduled Pinned Locked Moved C#
csharpdelphiwinformsoopquestion
9 Posts 6 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.
  • B Offline
    B Offline
    BillWoodruff
    wrote on last edited by
    #1

    Assuming I've defined a hierarchy of #whatever that allows nesting an arbitrary number of levels : then, I delete a reference to a top-level #whatever instance which contains #n levels deep of #n other #whatever instances: Can I assume that the #whatever instance now-without-a-reference, and all its sub-levels of references/instances, will be garbage collected ? Or, is it on me to recursively remove child references ? Of course, it is on me to handle references in the code to sub-elements that are "outside" the parent/ancestor inheritance chain. Is there a specific 'Dispose technique that can be used here ? Note: I've deliberately phrased the hypothetical here in an abstract way, to avoid talking about what a specific Control, like the WinForms TreeView, might implement internally. thanks, Bill

    «When I consider my brief span of life, swallowed up in an eternity before and after, the little space I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which know me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then.» Blaise Pascal

    L K 2 Replies Last reply
    0
    • B BillWoodruff

      Assuming I've defined a hierarchy of #whatever that allows nesting an arbitrary number of levels : then, I delete a reference to a top-level #whatever instance which contains #n levels deep of #n other #whatever instances: Can I assume that the #whatever instance now-without-a-reference, and all its sub-levels of references/instances, will be garbage collected ? Or, is it on me to recursively remove child references ? Of course, it is on me to handle references in the code to sub-elements that are "outside" the parent/ancestor inheritance chain. Is there a specific 'Dispose technique that can be used here ? Note: I've deliberately phrased the hypothetical here in an abstract way, to avoid talking about what a specific Control, like the WinForms TreeView, might implement internally. thanks, Bill

      «When I consider my brief span of life, swallowed up in an eternity before and after, the little space I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which know me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then.» Blaise Pascal

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi Bill, 1. an object is dead and collectable as soon as you no longer have a reachable reference to it (reachables are inside other obects that are reachable, stacks, static variables, ...). When a number of objects have internal references to each other but no outside references are alive, then all of those objects are dead/collectable. 2. exactly when they get garbage collected is more complex, it depends on the size, the generation number, when your GC runs, etc. However one normally should not care about this, that is the GC's job. 3. when your objects have a Dispose() method (necessary when they may hold pointers to unmanaged system resources), then for each of them the Dispose() method will be called eventually by the GC. In this case it is advisable to call their Dispose() explicitly so the system resources get released ASAP. Note: providing a Dispose() method will somewhat postpone the GC freeing their memory, so providing a Dispose() on huge objects is a bad idea! 4. when your Application terminates neither of those mechanisms will work for you; instead Windows itself will reclaim all memory and all system resources your program was using (unless...), so you can't safely implement wanted side effects in your Dispose() method as your program might terminate before Dispose() gets called by the GC! Hope this helps and is sufficiently clear and accurate. :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      B L 2 Replies Last reply
      0
      • L Luc Pattyn

        Hi Bill, 1. an object is dead and collectable as soon as you no longer have a reachable reference to it (reachables are inside other obects that are reachable, stacks, static variables, ...). When a number of objects have internal references to each other but no outside references are alive, then all of those objects are dead/collectable. 2. exactly when they get garbage collected is more complex, it depends on the size, the generation number, when your GC runs, etc. However one normally should not care about this, that is the GC's job. 3. when your objects have a Dispose() method (necessary when they may hold pointers to unmanaged system resources), then for each of them the Dispose() method will be called eventually by the GC. In this case it is advisable to call their Dispose() explicitly so the system resources get released ASAP. Note: providing a Dispose() method will somewhat postpone the GC freeing their memory, so providing a Dispose() on huge objects is a bad idea! 4. when your Application terminates neither of those mechanisms will work for you; instead Windows itself will reclaim all memory and all system resources your program was using (unless...), so you can't safely implement wanted side effects in your Dispose() method as your program might terminate before Dispose() gets called by the GC! Hope this helps and is sufficiently clear and accurate. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        B Offline
        B Offline
        BillWoodruff
        wrote on last edited by
        #3

        Thanks, Luc, that is very clear, very helpful. cheers, Bill

        «When I consider my brief span of life, swallowed up in an eternity before and after, the little space I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which know me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then.» Blaise Pascal

        1 Reply Last reply
        0
        • L Luc Pattyn

          Hi Bill, 1. an object is dead and collectable as soon as you no longer have a reachable reference to it (reachables are inside other obects that are reachable, stacks, static variables, ...). When a number of objects have internal references to each other but no outside references are alive, then all of those objects are dead/collectable. 2. exactly when they get garbage collected is more complex, it depends on the size, the generation number, when your GC runs, etc. However one normally should not care about this, that is the GC's job. 3. when your objects have a Dispose() method (necessary when they may hold pointers to unmanaged system resources), then for each of them the Dispose() method will be called eventually by the GC. In this case it is advisable to call their Dispose() explicitly so the system resources get released ASAP. Note: providing a Dispose() method will somewhat postpone the GC freeing their memory, so providing a Dispose() on huge objects is a bad idea! 4. when your Application terminates neither of those mechanisms will work for you; instead Windows itself will reclaim all memory and all system resources your program was using (unless...), so you can't safely implement wanted side effects in your Dispose() method as your program might terminate before Dispose() gets called by the GC! Hope this helps and is sufficiently clear and accurate. :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          L Offline
          L Offline
          lmoelleb
          wrote on last edited by
          #4

          Hi Luc It seems you have mixed finalizers and IDisposable.Dispose() in your reply. :) The garbage collector will never call Dispose. It will call the finalizer if one is present.

          L P 2 Replies Last reply
          0
          • L lmoelleb

            Hi Luc It seems you have mixed finalizers and IDisposable.Dispose() in your reply. :) The garbage collector will never call Dispose. It will call the finalizer if one is present.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            You're right, thanks. :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            1 Reply Last reply
            0
            • L lmoelleb

              Hi Luc It seems you have mixed finalizers and IDisposable.Dispose() in your reply. :) The garbage collector will never call Dispose. It will call the finalizer if one is present.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              Thanks. I must admit this had me doubting myself and scratching my head.

              This space for rent

              B 1 Reply Last reply
              0
              • P Pete OHanlon

                Thanks. I must admit this had me doubting myself and scratching my head.

                This space for rent

                B Offline
                B Offline
                BillWoodruff
                wrote on last edited by
                #7

                If I were wiser, would I doubt you, or Marc, or Richard, or Luc, or OG, and others here, speak from profound knowledge+experience ? I doubt it. I scratch my head often, but the right kind of doubt does not come. :wtf: cheers, Bill

                «When I consider my brief span of life, swallowed up in an eternity before and after, the little space I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which know me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then.» Blaise Pascal

                OriginalGriffO 1 Reply Last reply
                0
                • B BillWoodruff

                  If I were wiser, would I doubt you, or Marc, or Richard, or Luc, or OG, and others here, speak from profound knowledge+experience ? I doubt it. I scratch my head often, but the right kind of doubt does not come. :wtf: cheers, Bill

                  «When I consider my brief span of life, swallowed up in an eternity before and after, the little space I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which know me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then.» Blaise Pascal

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  Bill, you should doubt everything I say. Especially this. ;)

                  Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  • B BillWoodruff

                    Assuming I've defined a hierarchy of #whatever that allows nesting an arbitrary number of levels : then, I delete a reference to a top-level #whatever instance which contains #n levels deep of #n other #whatever instances: Can I assume that the #whatever instance now-without-a-reference, and all its sub-levels of references/instances, will be garbage collected ? Or, is it on me to recursively remove child references ? Of course, it is on me to handle references in the code to sub-elements that are "outside" the parent/ancestor inheritance chain. Is there a specific 'Dispose technique that can be used here ? Note: I've deliberately phrased the hypothetical here in an abstract way, to avoid talking about what a specific Control, like the WinForms TreeView, might implement internally. thanks, Bill

                    «When I consider my brief span of life, swallowed up in an eternity before and after, the little space I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which know me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then.» Blaise Pascal

                    K Offline
                    K Offline
                    kalberts
                    wrote on last edited by
                    #9

                    I found a really great description of dotNet garbage collection in CLR via C#[^]. That book turned me around about automatic garbage collection. At least half a dozen times I said to myself something like "Hey, that's smart! I would never have thought of that if I were to manage the heap myself!" I used to be sceptical about automatic GC - now I am sceptical to those who claim they can do better themselves. It is like an optimizing compiler vs. assembly code: The optimizer will discover a lot of tricks that the assembler coder wont. The only problem remaining now is that you believe that you have removed the last reference to that 1 Gbyte data structure, and you are waiting for ages for it to go away, forgetting that you still have a reference to it in some static struct holding, say, user preferences. (Maybe you needed that reference e.g. during initialization, but never after that.) This is similar to, say, forgetting to close files. (There was a similar situation in old style Unix file systems, where anyone with read access to a file could create another link to it, preventing it from being deleted: When the creator/owner removed his last link, he lost control over it; your 'secret' link might be the only access path to the file, keeping it alive, keeping disk space from being released. But the owner is still the same, so he will have to pay for the disk storage! - I think remedies for handling this was introduced many years ago, but in the first years of Unix, it was a real problem.)

                    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