Reference counters

  • Thread starter Thread starter Bill O
  • Start date Start date
B

Bill O

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

Thank you,
Bill
 
Bill said:
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.
 
Bill O said:
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.
 
Bill O said:
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.
 
Chris Dunaway said:
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.
 
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...
 
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.
 
To be clear, the .NET CLR garbage collection does not use reference
counting,
but walks the root graph looking for reachable objects.

Regards,
Jeff
 
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.
 
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.
 
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.
 
Bill O said:
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).
 
Bill O said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top