How to determine the number of references a program is currently using?
-
Hi. Is there any ways/ APIs to determine how many references of a certain object/ class a C# program creates/ uses during it's life cycle? Suppose there's a class Person and in my program several references to Person objects are made. How to determine the number of Person references in my program? Thanks.
[Kayes]
-
Hi. Is there any ways/ APIs to determine how many references of a certain object/ class a C# program creates/ uses during it's life cycle? Suppose there's a class Person and in my program several references to Person objects are made. How to determine the number of Person references in my program? Thanks.
[Kayes]
Not sure about APIs etc., but the obvious one is to maintain a static int as part of your class and increment it for each constructor. This doesn't "uncount" them when they are GCed though, so it is a total count, rather than a shapshot.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
Not sure about APIs etc., but the obvious one is to maintain a static int as part of your class and increment it for each constructor. This doesn't "uncount" them when they are GCed though, so it is a total count, rather than a shapshot.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
Yeah, instance counting is easy. BTW: you could decrement the counter in a finalizer. However reference counting is basically impossible, a reference can be copied without the class or object being aware of that.
Person person1=new Person();
Person person2=person1;:)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
-
Not sure about APIs etc., but the obvious one is to maintain a static int as part of your class and increment it for each constructor. This doesn't "uncount" them when they are GCed though, so it is a total count, rather than a shapshot.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
That's a way. But consider the following. One object is constructed, but two references.
Person p = new Person(); //1 reference to Person p Person q = p; //2 references to Person p
How to track this?
[Kayes]
-
Yeah, instance counting is easy. BTW: you could decrement the counter in a finalizer. However reference counting is basically impossible, a reference can be copied without the class or object being aware of that.
Person person1=new Person();
Person person2=person1;:)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
Yeah, that's my point.
[Kayes]
-
That's a way. But consider the following. One object is constructed, but two references.
Person p = new Person(); //1 reference to Person p Person q = p; //2 references to Person p
How to track this?
[Kayes]
you can't, there is nothing to support that in .NET The GC is based on (a slightly conservative) reachability or "life" analysis, not on reference counting. why would you need a reference count? if only for one or a few classes of your own, you could try and implement it yourself, including an override for the = operator. Not sure you can get it all tight though. :)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.