    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Garbage Collection and Object Lifetime</title>
        <link>https://members.accu.org/index.php/journals/244</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">Overload Journal #63 - Oct 2004 + Programming Topics</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c149/">63</a>
                    (6)
<br />

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c65/">Programming</a>
                    (877)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c149-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c149+65/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Garbage Collection and Object Lifetime</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 01 October 2004 16:25:39 +01:00 or Fri, 01 October 2004 16:25:39 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>It seemed a simple bug report. &quot;When we close the editing screen
the framework asks the user to save the temporary portfolio we use
internally as storage. Make sure it gets removed cleanly instead&quot;.
&quot;Should be easy,&quot; I thought. &quot;We can't be leaking the objects, as
the whole form and its helpers are written in C#. I just need to
destroy the portfolio object at the right time.&quot;</p>
<p>The resulting solution opened my eyes to what Garbage Collection
does and, more importantly, what it doesn't do. To understand this,
we'll go back to the very basics of memory and object management,
and see what various techniques are available. I'll concentrate on
the C family of languages: C, C++, C# and the upcoming C++/CLI.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e24" id="d0e24"></a>Memory and
Object Lifecycle</h2>
</div>
<p>The diagram below shows the stages in the life of memory and
objects.</p>
<p>Raw memory is very simple: you acquire some from a pool of
available memory, use it, and release it back to the pool to be
reused. Failing to release it causes it to be considered
&quot;leaked&quot;.</p>
<p>Objects are slightly more complex because as well as obtaining
the raw memory for their storage, they need to be initialised to a
usable state and establish their class invariant, and have that
state destroyed before releasing the memory. If an object is not
destroyed then its state is considered leaked, which is important
if that state is a scarce non-memory resource such as system file
handles.</p>
<p>Let's look at some pseudo-code for creating and destroying an
object, and then see how the C family languages map onto each
part:</p>
<pre class="programlisting">
Memory_location memory_for_T
                = Acquire_Memory(size_of_T);
if(succeeded) {
  T_location T_object
                = Initialise_T(memory_for_T);
  if(not succeeded)
    Release_Memory(memory_for_T);
  else {
    // use T_object....
    Destroy_T(T_object);
    Release_Memory(memory_for_T);
  }
}
</pre>
<div class="figure"><a name="d0e37" id="d0e37"></a>
<div class="mediaobject c2"><img src=
"/var/uploads/journals/resources/lifecycle-revised.png" align="middle" alt=
"Memory and Object Lifecycle"></div>
<p class="title c3">Figure 1. Memory and Object Lifecycle</p>
</div>
<p>There are four situations I'll look at: creating an object on
the stack; as a base class of some other object; as a member of an
object; and on the heap. We'll illustrate these by considering a
class described loosely as:</p>
<pre class="programlisting">
class B : A {
  C c;
  D* d;
}
</pre>
<p>We'll initialise each instance of A, B, C, and D with the
numbers 1, 2, 3, and 4.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e49" id="d0e49"></a>C++</h2>
</div>
<p>C++ provides a very simple and clean solution</p>
<pre class="programlisting">
class B : public A {
public:
  B(int b_param)
     : A(1),
       c(3),
       d(new D(4)) {} 
private:
  C c;
  std::auto_ptr&lt;D&gt; d;
};
 
int main() {
  B b(2);
  // use b 
}
</pre>
<p>In C++, and all the other languages, the size of an object is
worked out by the system, and takes into account all the space for
the sub-objects.</p>
<p>If objects are constructed on the stack, then the memory is
acquired and released automatically, often by just adjusting the
stack pointer. Constructors play the part of the initialise
function, and the programmer usually writes them, although there
are cases where the compiler will generate them. When objects go
out of scope the destructor is automatically called and the memory
released.</p>
<p>Destructors are the destroy functions and the compiler
automatically calls the destructors for base classes and members.
It can even write them too - if there is nothing else needed other
than the members' destructors to be called, then the required
destructor is trivial, and the compiler will generate it for us if
we leave it out altogether. Members of other objects are very
similar to stack variables. In particular, when the outer object is
destroyed, all its member objects are destroyed too.</p>
<p>Allocation on the heap is done using a &quot;new-expression&quot; which
allocates memory and calls the required constructor. A
&quot;delete-expression&quot; calls the destructor and frees the memory.</p>
<p>Experts at Kipling's game of &quot;Kim&quot; may have spotted something
missing - error checking. Fortunately the compiler generates it all
for you using exceptions. I'd have to be more careful if I had raw
pointers in my class, but wrapping them up in auto_ptr makes that
problem go away and I can be lazy and correct, which is every
programmer's ideal.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e66" id="d0e66"></a>C</h2>
</div>
<p>C is more verbose - after all you have to do a lot more
yourself. The only things the compiler will do for you is allocate
and deallocate space on the stack and for struct members, and tell
you the size needed for objects using the <tt class=
"literal">sizeof</tt> operator.</p>
<pre class="programlisting">
typedef struct B_tag {
  A a;
  C c;
  D* d;
} B;

D* D_new(int d_param) {
  void* memory = malloc(sizeof(D));
  if(memory == NULL)
    goto malloc_failed;

  D* d = D_init(memory, d_param);
  if(d == NULL)
    goto init_failed;

  return d;
 
init_failed:
  free(memory);

malloc_failed:
  return NULL; 
}
 
B* B_init(void* memory, int b_param) {
  A* a = A_init(memory, 1);
  if(a == NULL)
    goto A_failed;
 
  C* c = C_init(memory + offsetof(B, c), 3);
  if (c == NULL)
    goto c_failed;
 
  d = D_new(4);
  if(d == NULL) 
    goto d_failed;
 
  return (B*)memory;
 
d_failed:
   C_destroy(c);
c_failed:
   A_destroy(a);
A_failed:
   return NULL;
}
 
void* B_destroy(B* b) {
  // assume destruction can't fail
  free(D_destroy(b-&gt;d));
  C_destroy(&amp;b-&gt;c);
  A_destroy((A*)b);
}

int main() {
  B b;
  B_init(&amp;b, 2);
  // use b
  B_destroy(&amp;b);
}
</pre>
<p>This is directly analogous to the C++ solution, and illustrates
the sort of tricks the C++ compiler is doing behind the scenes, in
particular the error checking and clearing up of partially
constructed objects. But it is a lot of work. I'll leave it as an
exercise for the reader to come up with a better solution in terms
of writing and maintaining this sort of code.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e78" id="d0e78"></a>What Is Garbage
Collection?</h2>
</div>
<p>Garbage Collection replaces manual releasing of memory such that
'leaked' memory is automatically reclaimed by the system and is
then available for use.</p>
<p>It does this by finding objects that are no longer needed
(technically, objects that are unreachable from &quot;root&quot; objects such
as global variables and the stack by following member references)
and reclaims their memory for future use. It can be compared to
treating the program as having an infinite amount of memory - if
you can never run out, then you don't need to bother to delete
anything, and all objects can live forever and can be thought of as
immortal [<a href="#Griffiths">Griffiths</a>].</p>
<p>It is very tempting, when starting to use garbage collection, to
think that it means you don't have to worry any more about the
tedious work of keeping track of object ownership and lifetimes,
and the programmer can concentrate on more interesting and more
productive work.</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>&quot;... Garbage collection relieves you from the burden of freeing
allocated memory ... First, it can make you more productive...&quot;
[<a href="#gc">gc</a>]</p>
</blockquote>
</div>
<div class="blockquote">
<blockquote class="blockquote">
<p>&quot;A second benefit of garbage collection ... is that relying on
garbage collection to manage memory simplifies the interfaces
between components ... that no longer need expose memory management
details (&quot;who is responsible for recycling this memory&quot;).&quot;
[<a href="#GC-faq">GC-faq</a>]</p>
</blockquote>
</div>
<p>Unfortunately, this misses a subtle point in the relationship
between ownership, object lifetimes, and memory management - they
aren't the same thing. Garbage Collection frees you from having to
clean up the memory, true. But Ownership and Lifetime still have to
be carefully considered as part of design.</p>
<p>For example, holding on to object references for too long, or
giving them to global objects, will keep them locked into memory.
This is often referred to as a memory leak, although it is achieved
by incorrectly holding onto things for too long, and not by
forgetting to clean up as in C++.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e106" id="d0e106"></a>C# and
.Net</h2>
</div>
<p>In C# construction is very much like C++ in that the new keyword
combines allocating the memory and calling the constructor.</p>
<p>There is no explicit memory deallocation stage - that's done
automatically by the Garbage Collector - but is there something
that can destroy an object? Not for releasing memory for its
members - again that's done by the Collector - but something for
cleaning up non-memory resources at a specific time?</p>
<p>There is a special function called when an object is being
reclaimed - the finalizer. At first sight this looks very much like
a destructor (the C# syntax is the same, and the designers of
Managed C++ in VC7 thought they were the same - MC++ destructors
are actually the finalizer in disguise), but it has since become
clear that the finalizer can't be used to destroy an object, for
three reasons:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p><span class="bold"><b>You don't know when it gets
called.</b></span> Things get finalized when the garbage collector
runs, but all you know is that is may run at some unspecified point
in the future, so you can't rely on it being called at a specific
time</p>
</li>
<li>
<p><span class="bold"><b>You don't know how many times it gets
called, if at all.</b></span> It's possible that the program
finishes before the collector runs, in which case the finalizer is
never called. Also, an object that has been finalized can be kept
alive using the <tt class="methodname">ReRegisterForFinalize</tt>
method, and then finalized again. And again. And again.</p>
</li>
<li>
<p><span class="bold"><b>You can't do much in it.</b></span> When
your finalizer is running, you don't know which other managed
objects you have references to have already been finalized, so
unless your design guarantees they're still living - in other
words, you've carefully thought out their lifetimes - you can't
touch any other objects. The only sensible thing you can do is to
log some information somewhere to say it's been finalized.</p>
</li>
</ul>
</div>
<p>It is sometimes recommended to use the finalizer to clean up
important unmanaged resources that need to be released, such as
handles from the operating system that the Garbage Collector
doesn't know about. Unfortunately you may have run out of these
resources before the collector runs and the finalizers get called,
so you can't rely on that<sup>[<a name="d0e136" href="#ftn.d0e136"
id="d0e136">1</a>]</sup>.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e140" id="d0e140"></a>Dispose</h2>
</div>
<p>Recall in my original problem, that I needed to tidy up a
particular object at a particular time. A common solution is to
write a teardown method, and the .Net designers have provided a
standard interface: <tt class="classname">IDisposable</tt>, which
has a single method <tt class="methodname">Dispose()</tt> to be
called when you want the object to clean up and &quot;die&quot;. However, as
there can be other references to the object, <tt class=
"methodname">Dispose</tt> may be called on an object multiple
times, and it is also allowed that a disposed object may be reused,
for example a disposed File Object could be reopened, and become
&quot;alive&quot; again, but I suggest that this would get too confusing to
recommend - keep it simple: <tt class="methodname">Dispose</tt>
destroys the object, and nothing else can use it afterwards.</p>
<p>Used like this, <tt class="methodname">Dispose</tt> is a
candidate for the equivalent of a destructor. If an object has
resources that must be released at a specific time, implement
<tt class="methodname">Dispose</tt> and remember to call it. C# has
even added help to the language to do this - using - which will
automatically call <tt class="methodname">Dispose</tt> on its
argument at the end of a statement block, in a similar way to
auto_ptr, or Boost's scoped_ptr.</p>
<p>So finally, here's the example in C#</p>
<p>We'll have our base class A inherited from a helper class
<tt class="methodname">Disposable</tt> - it's based on a pattern
for writing disposable objects where both the <tt class=
"methodname">Finalizer</tt> and the <tt class=
"methodname">Dispose</tt> methods are dispatched to a single
virtual helper [<a href="#msdn">msdn</a>]. Some classes in the .Net
framework such as <tt class="classname">UserControl</tt> use this
technique</p>
<pre class="programlisting">
public class Disposable : IDisposable {
  private bool isDisposed = false;
 
  public Disposable() {}
 
  ~Disposable() {
    Dispose(false);
  }
 
  public sealed virtual void Dispose() {
    if(!isDisposed) {
      isDisposed = true;
      Dispose(true);
      GC.SuppressFinalize(this);
    }
  }
 
  protected virtual void Dispose(
                         bool isDisposing) {}

  protected sealed void TryDispose(
                         Object object) {
    TryDispose((IDispose)object);
  }

  protected sealed void TryDispose(
                         IDispose idispose) {
    if(idispose != null)
      idispose.Dispose();
  }
}
 
public class B : A {
  public B(int b_param)
     : base(1) {
    try {
      c = new C(3);
      d = new D(4)
    }
    catch(Exception e) {
      Dispose();
      throw e;
    }
  }
 
  public override void Dispose(
                         bool isDisposing) {
    if(isDisposing) {
      // dispose of managed resources here
      TryDispose(d); d = null;   
      TryDispose(c); c = null;   
    } 
    // dispose of unmanaged resources here
    // and call the base class
    base.Dispose(isDisposing);
  }
  public static int main() {
    B b;
    using(b = new B(1)) {
      // use b;   
    } // b.Dispose called automatically
  }
}
</pre>
<p>Unfortunately using only works for objects whose lifetime is a
local scope, but not for members, and they have to be cleaned up by
hand.</p>
<p>C# doesn't allow objects embedded in other objects, only simple
types and references to objects on the heap, so c has to be created
on the heap, and this makes writing the constructor to cope with an
exception being thrown more difficult.</p>
<p><tt class="methodname">Dispose</tt> has to be written by hand
every time and if you forget to dispose of something, or it didn't
used to be disposable but now ought to be, the resources haven't
been disposed of at the right time.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e197" id="d0e197"></a>An
Improvement? C++/CLI</h2>
</div>
<p>Microsoft is about to release their new attempt at getting C++
to work with CLI (the common language part of .Net). Its previous
Managed C++ suffered from many problems, and is not widely
used.</p>
<p>In this language, the solution can use many familiar C++
idioms:</p>
<pre class="programlisting">
ref class B : public A {
public:
  B(int b_param)
     : A(1), c(3), d(gcnew D(4)) {} 
 
private:
   int b;
   C c;
   auto_ptr&lt;D&gt; d;
             // write one for CLI references
};
 
int main() {
  B b(1);
  // use b
}
</pre>
<p>The destructors here are <tt class="methodname">Dispose()</tt>,
and the compiler is generating the implementation and the calls,
just like C++.</p>
<p>I've assumed that there is an <tt class=
"classname">auto_ptr</tt> analogue that works with CLI references
and the rest is just the slightly different syntax for creating an
object on the managed heap.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e216" id="d0e216"></a>Back To The
Problem</h2>
</div>
<p>In the original system, storage for financial instruments was
managed by a simple Portfolio object, which had a <tt class=
"methodname">Close</tt> method to tidy it up. An instance of this
was shared between several Processor objects used to manipulate the
portfolio, instances of which were in turn shared between several
User Interface components.</p>
<p>The obvious first step was to make the Portfolio implement
<tt class="methodname">Dispose</tt>, and have that close the
storage.</p>
<p>But it was not obvious who should be disposing of this object or
when - there was no clear ownership and no notion of how long the
object would remain usable for - one Processor could dispose of the
Portfolio and the others could then try to use it again. My
solution was to push the issue of ownership and destruction up a
level, by making all the Processors that used the Portfolio
themselves disposable, and documented that they could use the
Portfolio given to them until they themselves were disposed of.</p>
<p>The User Interface objects were already disposable, so it was a
simple matter to pass in the Processor they needed, and again
define that they could use it throughout their own lifetime.</p>
<p>The top-level form created the Portfolio and Processors, hooked
them up to the User Interface and set everything going. Finally, in
response to the form needing to close, it was then a simple matter
to dispose of all the User Interface objects, dispose of the
Processors, and then dispose of the Portfolio.</p>
<p>So here we have an interesting consequence: if a resource must
be cleaned up promptly, then every object that uses it needs to
think about when it is no longer allowed to use it. In this case I
did it by imposing a lifetime on the Processors and User Interface
objects and guaranteeing that the Portfolio would outlive them.</p>
<p>The consequence of having a lifetime managed by calling
<tt class="methodname">Dispose</tt> has just spread from a low
level helper tucked away in some other objects, all the way up to a
top level object. It is very pervasive.</p>
<p>In this case, the solution resulted in virtually all non-trivial
classes needing to implement <tt class="methodname">Dispose</tt>,
and involved a non-trivial amount of design rework to make the
ownership relations and lifetime issues clear. The only classes
that were not affected were very simple &quot;value&quot; types used to group
together data items. The language and compiler provided no help as
I had to write all the <tt class="methodname">Dispose</tt> methods
by hand, call <tt class="methodname">Dispose</tt> for every
non-trivial member, and hope that if a new member is added in the
future or a class becomes disposable, then the writer remembers to
update the <tt class="methodname">Dispose</tt> method.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e256" id=
"d0e256"></a>Conclusions</h2>
</div>
<p>Far from Garbage Collection relieving the programmer of having
to think about ownership and lifetime, these issues still exist in
just the same way as in C++. Only relatively simple types have no
need of the Dispose idiom and can be left to the collector - any
type that uses, directly or indirectly, resources that need to be
released in a timely fashion, needs to have their relative
lifetimes thought about.</p>
<p>Current languages such as C# don't help the programmer in
writing the mechanics of these things, but the forthcoming C++/CLI
will bring many of the tools that C++ provides to improving this
area.</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e263" id="d0e263"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Griffiths" id="Griffiths"></a>
<p class="bibliomixed">[Griffiths] Alan Griffiths, &quot;Heretical Java
#1: Immortality - at a price&quot;, <span class="citetitle"><i class=
"citetitle">Overload 59</i></span></p>
</div>
<div class="bibliomixed"><a name="gc" id="gc"></a>
<p class="bibliomixed">[gc] <span class="bibliomisc"><a href=
"http://www.artima.com/insidejvm/ed2/gc.html" target=
"_top">http://www.artima.com/insidejvm/ed2/gc.html</a></span></p>
</div>
<div class="bibliomixed"><a name="GC-faq" id="GC-faq"></a>
<p class="bibliomixed">[GC-faq] <span class="bibliomisc"><a href=
"http://www.iecc.com/gclist/GC-faq.html" target=
"_top">http://www.iecc.com/gclist/GC-faq.html</a></span></p>
</div>
<div class="bibliomixed"><a name="msdn" id="msdn"></a>
<p class="bibliomixed">[msdn] <span class="bibliomisc"><a href=
"http://msdn.microsoft.com/library/en-us/cpref/%20html/frlrfsystemidisposableclassdisposetopic.asp"
target="_top">http://msdn.microsoft.com/library/en-us/cpref/
html/frlrfsystemidisposableclassdisposetopic.asp</a></span></p>
</div>
</div>
<div class="footnotes"><br>
<hr class="c4" width="100">
<div class="footnote">
<p><sup>[<a name="ftn.d0e136" href="#d0e136" id=
"ftn.d0e136">1</a>]</sup> As Java's Garbage Collection is very like
.Net's, this has led to some implementations of the Java library to
try and get around this for file handles by triggering the garbage
collector if an attempt to get a file handle fails, then trying
again. This helps that particular program avoid running out, but
may still be starving the system of the handles in the
meantime.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
