c# - Counting total objects queued for garbage collection -


i wanted add small debug ui opengl game, updated various debugging options/output displays. 1 thing wanted constant counter shows active objects in each generation of garbage collector. don't want names or anything, total count; can eyeball when things within game.

my problem, however, can't seem find way count total objects alive in various generations.

i considered keeping global static field, incremented within every constructor , decremented within class finalizers. require hand-coding said functionality every class though, , not solve problem of "per-generation total".

do know how go doing this?

(question title:) "counting total objects queued garbage collection"

(from question's body:) "my problem, however, can't seem find way count total objects alive in various generations."

remark: question's title , body ask opposite things. in title, you're asking number of objects can no longer reached via gc root, while in body, you're asking "live" objects, i.e. can still reached via gc root.

let me start saying there might not way this, because objects in .net not reference-counted, cannot marked "no longer needed" when last reference them disappears or goes out of scope. believe .net's mark-and-compact garbage collector discovers objects alive , can reclaimed during actual garbage collection (during "mark" phase). seem want information in advance, i.e. before gc occurs.

that being said, here perhaps best options:

  1. perhaps best bet in .net's managed framework class library performance counters. doesn't there suitable counters available: there performance counters giving number of allocated bytes in various gc generations, afaik no counters number of live/dead objects.

  2. you might want take @ the clr's (i.e. runtime's) unmanaged, com-based debugging api. given have retrieved icordebugprocess5 interface, these methods might of interest:

    "gets enumerator objects garbage-collected in process."

    see this answer a similar question on so.

    note objects garbage-collected, not live objects.

    "provides general information garbage collection heap, including whether enumerable."

    if turns out managed heap is enumerable, use…

    "gets enumerator objects on managed heap."

    the objects returned enumerator of type:

    "provides information object on managed heap."

    you might not interested in these details, in number of objects returned enumerator.

    (i haven't used api myself, perhaps there exists better , more efficient way.)

  3. in sept 2015, microsoft published managed library called clrmd aka microsoft.diagnostics.runtime on github. based on same foundation unmanaged debugging api mentioned above. project includes documentation enumerating objects in gc heap.

btw. there extremely informative book out there ben watson, "writing high-performance .net code", includes solid tips on how make .net memory allocation , gc more efficient.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -