Re: Lock-free reference counting



Mark Wooding wrote:
To be honest, I can't say that I care greatly exactly when objects get
freed up.

Memory is not the only resource that can be reserved and freed. There
are situations where an object must free some resource (other than
memory) as soon as the object is not used anymore because even a
temporary leak of that resource might be fatal.

Having to manually tell the object to free the resource is obviously
error-prone (especially in languages where there may be surprising exit
points from functions, such as a thrown exception).

Some languages offer tools to help these cases, but their usage is
sometimes limited. For example, if I'm not mistaken, the C# language
offers the "using" block for this exact purpose: When the block is
exited, any object defined with the "using" will be immediately
finalized, regardless of how the block is exited. In Java I think the
"finally" block can be used for a similar purpose.

Of course the problem with these is that they are limited to local
scope. You can't achieve a "finalize the object immediately after the
last reference to it dies" effect using those commands if the object is
shared among two different modules.

Another technique which is often hard to achieve with GC'd languages
which treat objects mostly as references-to-objects is the copy-on-write
idiom. For instance, one problem is that having ghost objects outliving
the scope of their references may cause unnecessary deep-copies of the
data, even though only one live object was actually referencing the data
(and thus deep-copying it would not be necessary). Also achieving the
automatic reference counting needed for copy-on-write can be rather
laborious in itself in many such languages.

If I'm not mistaken, if you want to use the copy-on-write technique in
a language like Java, the GC is actually more a hindrance than an aid.
.



Relevant Pages

  • Re: Lock-free reference counting
    ... Memory is not the only resource that can be reserved and freed. ... Some languages offer tools to help these cases, ... In Java I think the ... last reference to it dies" effect using those commands if the object is ...
    (comp.programming)
  • Re: Is behavior of += intentional for int?
    ... Python (and other similar languages?) are different in that. ... 'x' does not point to an area in memory, where you can do anything with ... If you actually want to be able to reference the function later (as you ...
    (comp.lang.python)
  • Re: about pointer
    ... Anything involving memory management involves pointers of some form, ... in C and C++ or hidden behind reference types as in Java, pointers are necessary for a wide variety of operations. ... A host of basic data structures such as trees and lists rely on the ability to reference arbitrary memory locations. ... Looking at references as "hiding" pointers is a good way to understand it coming from C, but it's unnecessary if you're working in the languages themselves -- references are just references. ...
    (comp.lang.c)
  • Re: Finding the instance reference of an object
    ... notion of a name as a reference to a specific area of memory. ... In many other languages, a ... The problem is that listeners from a C or FORTRAN background ...
    (comp.lang.python)
  • Re: Java References
    ... them - "pointer" seems to be used as the general term not as the ... The reference values (often ... pointers other than memory locations. ... other languages that use the word "pointer" where the set of allowable ...
    (comp.lang.java.programmer)