473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 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 2803
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Alan Lee | last post by:
Is there any way to set RAND_MAX in rand()? I am looking for a way to get a random number between 1 and 1E9. So far I can't figure out how to do that. any suggestions would be greatly appreciated.
12
by: Klaus Drechsel | last post by:
My Problem: The Function Dispose on a IDisposable shuld be called on the last reference to this instance. Can I get the number of references. to an object? Klaus Drechsel
11
by: codebloatation | last post by:
I know how to use references but i DO not get WHY they exist other than to add to the language. Are they actually needed for anything?
42
by: Holger | last post by:
Hi guys Tried searching for a solution to this, but the error message is so generic, that I could not get any meaningfull results. Anyways - errormessage:...
22
by: mehdi_mousavi | last post by:
Hi folks, Consider the following line of code: Address addr = ei.Address; where ei is an instance of a class that has got a property of Address type. The question is that what is happening...
6
by: Shailen Sukul | last post by:
Observed a weird behaviour with object references. See code listing below: using System; using System.Collections.Generic; using System.Text; namespace PointerExceptionTest { /*
14
by: Philipp Reif | last post by:
Hi all, I've got a little hole in my head concerning references. Here's what I'm trying to do: I'm calling a function, passing the reference of a business object for editing. The function clones...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
28
by: Tony Johansson | last post by:
Hello! If I write this statement int number = new int(); I don't get compiler error or run time error but how will the compiler read such a statement ? For example will number be a reference...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.