Number of references to an instance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason Bell

    Number of references to an instance

    How can I find out how many references to an instance exist at any given
    time?

    I want to make it so that when the number of references is one, a countdown
    starts (that one reference being the one held by the resource manager).
    This way I can ensure that objects are only deleted if they haven't been
    used in an amount of time defined by the user.
  • Peter Rilling

    #2
    Re: Number of references to an instance

    I don't think you can.

    You should try to add the appropriate architecture such that you can control
    that, somehow.

    "Jason Bell" <cetrocet@shaw. ca> wrote in message
    news:Xns9729E74 64B4Ehalobear33 3yahoocom@64.59 .144.76...[color=blue]
    > How can I find out how many references to an instance exist at any given
    > time?
    >
    > I want to make it so that when the number of references is one, a
    > countdown
    > starts (that one reference being the one held by the resource manager).
    > This way I can ensure that objects are only deleted if they haven't been
    > used in an amount of time defined by the user.[/color]


    Comment

    • Christoph Nahr

      #3
      Re: Number of references to an instance

      On Sun, 11 Dec 2005 09:25:17 GMT, Jason Bell <cetrocet@shaw. ca> wrote:
      [color=blue]
      >How can I find out how many references to an instance exist at any given
      >time?[/color]

      You can't. The CLR memory manager doesn't work like that. Reference
      counting leaves you open to cyclic references that cause memory leaks.
      Instead, every garbage collection starts afresh and determines all
      objects that are currently reachable by your program. I don't think
      the actual number of references is ever counted.
      [color=blue]
      >I want to make it so that when the number of references is one, a countdown
      >starts (that one reference being the one held by the resource manager).
      >This way I can ensure that objects are only deleted if they haven't been
      >used in an amount of time defined by the user.[/color]

      Hmm... there's the System.WeakRefe rence class which allows you to
      track the garbage collection status of an object. You can't prevent
      GC in this way, though, so I guess it's not what you want.

      You can call System.GC.KeepA live to prevent garbage collection of an
      object within the scope of a method, if that helps you.

      Other than that, you have to track references and timestamps manually.
      There's no way to determine the current number of references, other
      than making a WeakReference and checking for !IsAlive, and in that
      case the object has already been garbage-collected.

      Why exactly are you planning to interfere with normal garbage
      collection, anyway?
      --
      Stay ahead in World of Warcraft with expert guides, latest patch news, class tips, dungeon strategies, PvP builds, and The War Within updates—all in one place.

      Comment

      • Jason Bell

        #4
        Re: Number of references to an instance

        Thanks for the info.

        The scenario I'm working in is that of a graphics engine, where closely
        managing resources is very important. There are times when a resource,
        such as a texture, may not be needed for several seconds. But I don't
        necessarily want the GC to swoop in and dispose it, as it may be needed
        soon afterward and need to be reloaded.

        So my thinking was that I'd have one reference to a texture or mesh or
        something in a resourcemanager , and all other objects obtain references
        to the resource from that resourcemanager . Each resource would have a
        user defined time associated with it, say 30 seconds (which is an
        eternity in graphics programming). Any time the reference count dropped
        to one, the timer would start counting down. When the timer hits zero,
        the resource manager removes its reference to the resource, which allows
        the GC to swoop in and dispose of it.

        The elegence of that solution would have been that other objects using
        references to the resource wouldn't have needed to inform the manager
        when they weren't using it any more.

        I suppose I'll just have to go with an uglier solution where objects
        have to inform the resourcemanager when they're obtaining or removing a
        reference. The downside of course would be, that if the user forgets to
        tell the manager when a reference has been removed, the resource will
        never get disposed.

        I suppose it's not so bad, it's just like pairing "new" and "delete []"
        in c/c++.

        Christoph Nahr <christoph.nahr @kynosarges.de> wrote in
        news:q93op15647 on31fvupo88b6s1 nogc2imn0@4ax.c om:[color=blue]
        > On Sun, 11 Dec 2005 09:25:17 GMT, Jason Bell <cetrocet@shaw. ca> wrote:
        >[color=green]
        >>How can I find out how many references to an instance exist at any
        >>given time?[/color]
        >
        > You can't. The CLR memory manager doesn't work like that. Reference
        > counting leaves you open to cyclic references that cause memory leaks.
        > Instead, every garbage collection starts afresh and determines all
        > objects that are currently reachable by your program. I don't think
        > the actual number of references is ever counted.
        >[color=green]
        >>I want to make it so that when the number of references is one, a
        >>countdown starts (that one reference being the one held by the
        >>resource manager). This way I can ensure that objects are only
        >>deleted if they haven't been used in an amount of time defined by the
        >>user.[/color]
        >
        > Hmm... there's the System.WeakRefe rence class which allows you to
        > track the garbage collection status of an object. You can't prevent
        > GC in this way, though, so I guess it's not what you want.
        >
        > You can call System.GC.KeepA live to prevent garbage collection of an
        > object within the scope of a method, if that helps you.
        >
        > Other than that, you have to track references and timestamps manually.
        > There's no way to determine the current number of references, other
        > than making a WeakReference and checking for !IsAlive, and in that
        > case the object has already been garbage-collected.
        >
        > Why exactly are you planning to interfere with normal garbage
        > collection, anyway?[/color]

        Comment

        • Bruce Wood

          #5
          Re: Number of references to an instance

          I can give you a different solution: you can create a central garbage
          collector for your objects. Mine works like this:

          Each client exposes an interface that exposes a "Disposed" boolean
          property, and a property to return the Images (in my case) that it is
          currently using.
          Each client that requests a resource from the central cache (in my
          design) provides a pointer to itself: e.g. Image fetchedImage =
          Cache.GetImage( this, ... other arguments ...)
          The cache stores a WeakReference to each of the clients.
          At intervals, the cache runs through its list of clients. Any clients
          that are Disposed or that have been garbage collected don't count.
          In my system, the cache asks each client which Images it is currently
          using. Essentially the cache does a mark pass.
          At this point I Dispose any images not in use by live clients, but you
          could start a timer instead.
          Any new requests coming in during the garbage collection pass mark the
          relevant images as "in use" so that they won't be collected at the end.

          Anyway, that's the basic idea: it's a form of garbage collection,
          rather than reference counting. Maybe you can devise a solution along
          the same lines.

          Comment

          • Bruce Wood

            #6
            Re: Number of references to an instance

            "Each client exposes an interface..."

            Sorry. That should have been,

            "Each client implements an interface..."

            Comment

            • Christoph Nahr

              #7
              Re: Number of references to an instance

              One thought: You could simply wrap all your textures in WeakReference
              objects without implementing a custom resource manager. GC doesn't
              happen immediately, so you should be able to reuse most textures a few
              seconds after all references have been set to null.

              The .NET GC is generation-based, and if the texture has been moved to
              a high enough generation it will stick around for quite some time
              before being garbage-collected. Perhaps this (non-deterministic) time
              window would be big enough to allow for sufficient reuse?
              --
              Stay ahead in World of Warcraft with expert guides, latest patch news, class tips, dungeon strategies, PvP builds, and The War Within updates—all in one place.

              Comment

              Working...