[Haskell-cafe] number of references to a variable
jmaessen at alum.mit.edu
Sat Nov 1 13:51:21 EDT 2008
On Nov 1, 2008, at 9:38 AM, Andrew Coppin wrote:
> Alberto G. Corona wrote:
>> Is there a way to know the number of memory references for a
>> variable?. The runtime must know it but i do not know if this
>> available for the program trough any low level trick
> More precisely, the GC computes it each time it runs. (And only
> computes it precisely during a "major" pass, not the more frequent
> "minor" passes.)
Even this isn't quite true for most GC algorithms. The GC only needs
to compute whether there is 0 or >= 1 reference to a given location
(with some special weasel words for stuff with finalizers defined).
If you can see it, the answer is always >=1, so this information is
much less useful than you might think!
Usually the clever thing you want to know is "this is the sole
reference to the pointed-to object". If that's what you're interested
in, try looking up "one-bit reference counting", but note that like
any accurate reference counting technique it's really inefficient in
practice compared to GC. [There are efficient reference counting
techniques, but they defer refcount updates in various subtle ways.
Also, every ref count technique requires a cycle detector.]
More information about the Haskell-Cafe