Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to determine the number of references a program is currently using?

How to determine the number of references a program is currently using?

Scheduled Pinned Locked Moved C#
csharptutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Abul Kayes
    wrote on last edited by
    #1

    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]

    OriginalGriffO 1 Reply Last reply
    0
    • A Abul 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]

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      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

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      L A 2 Replies Last reply
      0
      • OriginalGriffO OriginalGriff

        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

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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.


        A 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          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

          A Offline
          A Offline
          Abul Kayes
          wrote on last edited by
          #4

          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]

          L 1 Reply Last reply
          0
          • L Luc Pattyn

            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.


            A Offline
            A Offline
            Abul Kayes
            wrote on last edited by
            #5

            Yeah, that's my point.

            [Kayes]

            1 Reply Last reply
            0
            • A Abul 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]

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              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.


              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups