473,587 Members | 2,316 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2816
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.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?
--
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.c om:
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.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?


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
433
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
2057
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
1959
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
3424
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: ---------------------------------------------------- TypeError: addFile() takes exactly 1 argument (2 given) ----------------------------------------------------
22
1646
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 in the above assignment? Unlike the C/C++, it seems that the above code instructs the compiler to point
6
2471
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
2386
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 the object, calls an editor dialog to let the user edit the object, and then - depending on the DialogResult - assigns either the clone/backup copy...
23
2922
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 method. QUOTE ENDS HERE I have never heard anyone else say that it is a problem for a function
28
9061
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 to a new int on the heap? //Tony
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8339
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.