473,395 Members | 1,554 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,395 software developers and data experts.

Reference counters

Is there a way to determine the number of references to an object? I only
need a reference count.

Thank you,
Bill
Nov 17 '05 #1
12 2106
Bill O wrote:
Is there a way to determine the number of references to an object? I only
need a reference count.


A simple way would be to add a static counter variable to the class and
in the constructor increment the counter. Each successive creation of
and instance of the class will increment the counter.

Nov 17 '05 #2

"Bill O" <billo@_imt_net> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...
Is there a way to determine the number of references to an object? I only
need a reference count.

Thank you,
Bill


Managed object aren't ref. counted by the CLR, so you can't get the number
of references.
If you really need this you will have to maintain a counter yourself.
Just curious, why would you need this?

Willy.
Nov 17 '05 #3

"Bill O" <billo@_imt_net> wrote in message
news:uT**************@TK2MSFTNGP09.phx.gbl...
Is there a way to determine the number of references to an object? I only
need a reference count.


A normal old every-day object? I don't think so.
Nov 17 '05 #4

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Bill O wrote:
Is there a way to determine the number of references to an object? I only
need a reference count.


A simple way would be to add a static counter variable to the class and
in the constructor increment the counter. Each successive creation of
and instance of the class will increment the counter.


This will give you the number of object instances, not the number of
references to an object.

Willy.
Nov 17 '05 #5
The garbage collector uses some way of determining when an object instance's
reference count reaches zero, because that's what makes the object instance a
candidate for collection.

Some research has been done in determining an object instance's reference
count, either on the fly, or by walking the process datastructure and
counting the number of pointers whose value equals the address of the object
in consideration. This task is made easier by reflection, assuming you can
access the address of an object and then the values of all the pointers in
your application.

There is a linear O(N) technique that gives an upper-bound to the object's
reference count by suggesting that since all 32-bit datatypes are memory
aligned, you can simply take the address-range of an object in question (that
is, its starting and ending addresses) and count the number of times any
32-bit aligned value in your process's allocated memory address space,
specifies a value within that object's address range.

For example, object at address range 0xB8000000-0xA0000000, foreach integer
in your process's memory as cast to a big integer array, does it have a value
in the range of the object.

Maybe that helps in some way...

"Bill O" wrote:
Is there a way to determine the number of references to an object? I only
need a reference count.

Thank you,
Bill

Nov 17 '05 #6
That is sufficient, but not the only way in which an object is eligible
for
collection. Objects with circular references that are not reachable are
still eligible
for garbage collection.

Regards,
Jeff
The garbage collector uses some way of determining when an object
instance's
reference count reaches zero, because that's what makes the object
instance a
candidate for collection.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #7
To be clear, the .NET CLR garbage collection does not use reference
counting,
but walks the root graph looking for reachable objects.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #8
Hello Marshal [DirectX MVP 2003],
The garbage collector uses some way of determining when an object
instance's reference count reaches zero, because that's what makes the
object instance a candidate for collection.


I think that in the context of the garbage collector, the only two numbers
it uses are 0 and N, in other words, either there is a reference to an object,
or there isn't. At least that's how we should consider it.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #9
> Just curious, why would you need this?


This is for our enterprise, data mapping framework. We have an object cache
(identity map) where each object in the application domain is associated
with a distinct database row (domain object). One of our constraints is that
we have to expire domain objects that have been in memory a long time but
are no longer referenced by our other calling frameworks.

Thank you for your response.
Nov 17 '05 #10
> To be clear, the .NET CLR garbage collection does not use reference
counting,
but walks the root graph looking for reachable objects.

Thank you. I assumed GC used reference counting, but I was wrong.

Thanks to all who responded. We have been researching this and struggling
with it for a few days. Your help is appreciated.
Nov 17 '05 #11

"Bill O" <billo@_imt_net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Just curious, why would you need this?

This is for our enterprise, data mapping framework. We have an object

cache (identity map) where each object in the application domain is associated
with a distinct database row (domain object). One of our constraints is that we have to expire domain objects that have been in memory a long time but
are no longer referenced by our other calling frameworks.

We use WeakReferences in our object cache and let normal garbage collection
occur on objects. If there is a "non-object cache" reference then the object
is also in the cache. As soon as the object is GC'd, it disappears from the
cache (well, the WeakReference.IsAlive becomes false).
Nov 17 '05 #12
Bill O <billo@_imt_net> wrote:
Just curious, why would you need this?


This is for our enterprise, data mapping framework. We have an object cache
(identity map) where each object in the application domain is associated
with a distinct database row (domain object). One of our constraints is that
we have to expire domain objects that have been in memory a long time but
are no longer referenced by our other calling frameworks.


Have a look at WeakReference - it's exactly for this kind of thing.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Mitchell | last post by:
Hello group, I'm trying to write code to test an application we're developing. This application needs to store its own performance data in the registry, and we'd like to be storing quite a...
2
by: Don | last post by:
Hello! I am trying to write some code to access performance counters on a remote machine. Ideally, I would like to add a machine to a tree control (like the "Servers" view in the .NET IDE) and...
0
by: Christopher Attard | last post by:
Hi, I need to create a dialog like the 'Add Counters' dialog box in perfmon. I'm using the System.Diagnostics namespace class in .NET and I've managed to do it. The problem arises when I'm...
1
by: db | last post by:
Hello, I am writing a utility to get the performance counters from a remote machine. Currently, I am usign the System.Diagnostics.PerformanceCounter class to retrieve these counters, but they...
7
by: James | last post by:
Hi Has anybody had any experience of ASP.Net performance counters not updating. In the performance monitor application when I try to add the groups ASP.NET and ASP.NET Applications the...
7
by: Fred Hedges | last post by:
I'm wondering if a tool exists that will integrate with the VS 2005 debugger, such that when I set a breakpoint I can inspect a reference and see how many other references exist to that object. ...
0
by: JackC | last post by:
Hi, We have an ASP.NET application with a (kinda shady) COM component that we don't know much about and don't have responsability for. But you know how it is - if the app. goes down its my fault...
10
by: Zytan | last post by:
I made a program using the Timer class, and I start the timer with Timer.Start(), but don't stop it with Timer.Stop(), and I assumed this was ok, but, Process Explorer informs me, after running it...
8
by: mathieu | last post by:
Hi there I have implemented a very simple smartpointer class (invasive design). And I was wondering what should be the natural API when using those in a Container. I choose to define the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
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.