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. .NET (Core and Framework)
  4. inhibit garbage collection

inhibit garbage collection

Scheduled Pinned Locked Moved .NET (Core and Framework)
dotnetdata-structuresquestionworkspace
2 Posts 2 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.
  • M Offline
    M Offline
    mikewinny
    wrote on last edited by
    #1

    im curious and unsure (and wading through a fog of nondeterministic destruction)... if one was to obtain a System.Runtime.InteropServices.GCHandle for each reference field of a class, and that class becomes eligible for finalization (implying that all its internal referenced objects also became eligible), would that class be finalized before any of its references (for sake of argument, the GCHandles were stored say, in an array in another reference type and both the array and its host object were also GCHandle'd in the array) ? would it be safe to assume all reference fields are still accessible at finalization time, allowing a controlled cleanup ? how might this be affected when the AppDomain unloads or CLR exits ? would the System.Environment.HasShutdownStarted property be of any use/relevance ?


    I worship his divine shadow. ^

    S 1 Reply Last reply
    0
    • M mikewinny

      im curious and unsure (and wading through a fog of nondeterministic destruction)... if one was to obtain a System.Runtime.InteropServices.GCHandle for each reference field of a class, and that class becomes eligible for finalization (implying that all its internal referenced objects also became eligible), would that class be finalized before any of its references (for sake of argument, the GCHandles were stored say, in an array in another reference type and both the array and its host object were also GCHandle'd in the array) ? would it be safe to assume all reference fields are still accessible at finalization time, allowing a controlled cleanup ? how might this be affected when the AppDomain unloads or CLR exits ? would the System.Environment.HasShutdownStarted property be of any use/relevance ?


      I worship his divine shadow. ^

      S Offline
      S Offline
      Scott Dorman
      wrote on last edited by
      #2

      The first question would be, why would you want to prevent the garbage collection from running?

      mikewinny wrote:

      would that class be finalized before any of its references

      I don't think so. The runtime doesn't guarantee when the objects will be collected and it doesn't provide any guarantees on the order the collection will occur.

      mikewinny wrote:

      would it be safe to assume all reference fields are still accessible at finalization time

      If you are writing your own finalizer, you shouldn't be accessing other finalizable fields. Again, since the runtime doesn't guarantee any ordering, I would say that it isn't safe to make that assumption. The Environment.HasShutdownStarted property will tell you if your finalizer has started to run, but not much more than that. It is also only available in .NET 2.0 and later. Check out the latest updates to my article Implementing IDisposable and the Dispose Pattern Properly[^]. Hopefully, it will answer these questions in a little more detail. You may also find some additional answers here: SafeHandle and Constrained Execution Regions[^]

      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