471,337 Members | 1,091 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

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.
Dec 11 '05 #1
6 2688
I don't think you can.

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

"Jason Bell" <ce******@shaw.ca> wrote in message
news:Xn********************************@64.59.144. 76...
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.

Dec 11 '05 #2
On Sun, 11 Dec 2005 09:25:17 GMT, Jason Bell <ce******@shaw.ca> wrote:
How can I find out how many references to an instance exist at any given
time?
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.
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.


Hmm... there's the System.WeakReference 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.KeepAlive 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?
--
http://www.kynosarges.de
Dec 11 '05 #3
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 <ch************@kynosarges.de> wrote in
news:q9********************************@4ax.com:
On Sun, 11 Dec 2005 09:25:17 GMT, Jason Bell <ce******@shaw.ca> wrote:
How can I find out how many references to an instance exist at any
given time?


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.
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.


Hmm... there's the System.WeakReference 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.KeepAlive 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?


Dec 11 '05 #4
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.

Dec 12 '05 #5
"Each client exposes an interface..."

Sorry. That should have been,

"Each client implements an interface..."

Dec 12 '05 #6
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?
--
http://www.kynosarges.de
Dec 12 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

22 posts views Thread by Alan Lee | last post: by
11 posts views Thread by codebloatation | last post: by
22 posts views Thread by mehdi_mousavi | last post: by
6 posts views Thread by Shailen Sukul | last post: by
14 posts views Thread by Philipp Reif | last post: by
23 posts views Thread by pauldepstein | last post: by
28 posts views Thread by Tony Johansson | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.