    <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  :: Heretical Java #1</title>
        <link>https://members.accu.org/index.php/articles/316</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>




<div class="xar-mod-head"><span class="xar-mod-title">Design of applications and programs + Overload Journal #59 - Feb 2004</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/articles/">All</a>

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

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c67/">Design</a>
<br />

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

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

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

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c153/">59</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c67-153/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c67+153/">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;Heretical Java #1</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 02 February 2004 21:55:34 +00:00 or Mon, 02 February 2004 21:55:34 +00:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e20" id="d0e20"></a></h2>
</div>
<p>There is a widespread belief that because Java provides &quot;garbage
collection&quot; the programmer automatically avoids the memory
management problems that plague the users of other languages. This
opinion continues to exist despite there being plenty of material
that attempts to correct this impression - and the existence of
tools to address memory management problems.</p>
<p>The typical Java developer of my experience either doesn't know
there is a problem or, more rarely, knows there is a problem but
doesn't know how to address it. While it may be that I'm stretching
a point to class this as the &quot;orthodox&quot; view I still feel justified
in addressing the topic because it is far better not to create a
mess than to have to sort one out.</p>
<p>Part of the problem stems from the treatment of the topic in
numerous introductory texts. These tend to present this uncritical
viewpoint of &quot;garbage collection&quot; - and early beliefs are always
the hardest to challenge. I can remember realising that there was
something wrong with the way I was taught about the lifecycle of
Java objects. The books I had read about Java told me that their
life ended when they were destroyed by the garbage collector. For
example in <i class="citetitle">Java in a Nutshell</i> we have:</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>The technique Java uses to get rid of objects once they are no
longer needed is called garbage collection. It is a technique that
has been around for years in languages such as Lisp. The Java
interpreter knows what objects it has allocated. It can also figure
out which variables refer to which objects, and which objects refer
to which other objects. Thus, it can figure out when an allocated
object is no longer referred to by any other object or variable.
When it finds such an object, it knows that it can destroy it
safely, and does so. The garbage collector can also detect and
destroy &quot;cycles&quot; of objects that refer to each other, but are not
referred to by any other objects. [<a href=
"#Flanagan">Flanagan</a>]</p>
</blockquote>
</div>
<p>Or alternatively in <i class="citetitle">Exploring Java</i> we
have:</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>Now that we've seen how to create objects, it's time to talk
about their destruction. If you're accustomed to programming in C
or C++, you've probably spent time hunting down memory leaks in
your code. Java takes care of object destruction for you; you don't
have to worry about memory leaks, and you can concentrate on more
important programming tasks. [<a href=
"#Niemeyer-Peck97">Niemeyer-Peck97</a>]</p>
</blockquote>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e48" id="d0e48"></a>Object
Lifecycles</h2>
</div>
<p>In all object-oriented systems the design of object lifecycles
is important because objects have responsibilities to meet at
significant points in their lives. If an object is being destroyed
then it must ensure that any resources that it owns are either
released or passed on to a new owner. And sure enough, <i class=
"citetitle">Exploring Java</i> continues with:</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>Before a method is removed by garbage collection, its <tt class=
"computeroutput">finalize()</tt> method is invoked to give it a
last opportunity to clean up its act and free other kinds of
resources it may be holding. While the garbage collector can
reclaim memory resources, it may not take care of things like
closing files and terminating network connections very gracefully
or efficiently. That's what the <tt class=
"computeroutput">finalize()</tt> method is for. [<a href=
"#Niemeyer-Peck97">Niemeyer-Peck97</a>]</p>
</blockquote>
</div>
<p>When I first read this it all seemed to make sense, but while
working with Java, I became increasingly conscious that reality
didn't accord with this view. For example, when working with
instances of the AWT Graphics class I learnt very quickly that I
needed to call <tt class="computeroutput">dispose</tt> - and not to
rely on the object to do so when it was destroyed.</p>
<p>I'm clearly not the only one to see that there is a problem. It
is common to see advice like &quot;only put debug code in the <tt class=
"computeroutput">finalize</tt> method&quot; (which directly contradicts
the last quote). One of the books that tries to address the problem
is <i class="citetitle">Thinking in Java</i> which says:</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>This is a potential programming pitfall because some
programmers, especially C++ programmers, might initially mistake
<tt class="computeroutput">finalize()</tt> for the destructor in
C++, which is a function that is always called when an object is
destroyed. But it is important to distinguish between C++ and Java
here, because in C++ objects always get destroyed (in a bug-free
program), whereas in Java objects do not always get
garbage-collected. Or, put another way: Garbage collection is not
destruction. [<a href="#Eckel98">Eckel98</a>]</p>
</blockquote>
</div>
<p>Over time I accumulated an assorted collection of rules of thumb
that dealt with most circumstances. But they lacked conceptual
elegance and when new circumstances occurred they required new
rules to be worked out carefully.</p>
<p>Then one day I was reading something by Bjarne Stroustrup about
the use of garbage collection. Stroustrup wasn't writing about Java
(he's the creator of C++) but, despite what you may have heard in
some of the Java texts, garbage collection is available to C++
programmers. What Stroustrup said made sense of these rules:</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>Garbage collection can be seen as a way of simulating an
infinite memory in a limited memory. With this in mind we can
answer a common question: Should a garbage collector call the
destructor for an object it recycles? The answer is no, because an
object placed on the free store and never deleted is never
destroyed. [<a href="#Stroustrup91">Stroustrup91</a>]</p>
</blockquote>
</div>
<p>This realisation bound all my rules of thumb together as a
single idea: in Java objects are immortal - they are never
destroyed. All that should happen in garbage collection is that the
memory &quot;owned&quot; by the object is recycled. While important for the
application as a whole this isn't a significant event in the
object's lifecycle - and we shouldn't expect the object to respond
in any significant way. (Which is consistent with the difficulty of
writing effective <tt class="computeroutput">finalize</tt> methods
- which Stroustrup also alludes to later in the same passage.)</p>
<p>The idea of an object continuing to exist without its memory may
sound a little strange - but objects exist without other resources
that make them useful (a Graphics object still exists after its
native peer has been released by calling <tt class=
"computeroutput">dispose</tt>). In any case, the rules of garbage
collection ensure that a program cannot tell if the object is there
or not. While from the point of view of designing my programs I
find the idea that the object is going to sit there forever holding
onto any resources I haven't told it to release compelling.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e110" id="d0e110"></a>The Design of
Java's Garbage Collection</h2>
</div>
<p>One of the important points taken up by the &quot;Patterns&quot; movement
of software design is that a &quot;solution&quot; has consequences. With any
design decision there are tradeoffs: resolving one problem may make
others worse or even introduce new ones. When the designers of Java
adopted garbage collection to manage memory they didn't provide a
solution to all the resource management problems a developer will
ever encounter. Nor did they believe that they had:</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>Garbage collection (GC) is probably the most widely
misunderstood feature of the Java platform. GC is typically
advertised as removing all memory management responsibility from
the application developer. This just isn't the case. On the other
hand, some developers bend over backwards trying to please the
collector, and often wind up doing much more work than is required.
A solid understanding of the garbage collection model is essential
to writing robust, high-performance software for the Java platform.
[<a href="#JPAppGC">JPAppGC</a>]</p>
</blockquote>
</div>
<p>There are memory management problems that Java's garbage
collection based model for object lifetimes doesn't solve, but
nothing is harder to fix than a problem people won't believe in.
And the orthodox belief that &quot;you don't need to worry about memory
leaks&quot; denies the obvious - memory is a finite resource that needs
to be managed.</p>
<p>The following will explore the &quot;infinite memory/immortal
objects&quot; design model of the object lifecycle and the way in which
it helps to deal with the management of resources in a Java
program.</p>
<p>The next section &quot;Garbage Collection and the Object Lifecycle&quot;
provides necessary detail of how garbage collection operates for
discussing the problems that it does solve and those that are left
for the programmer to address.</p>
<p>In &quot;Managing Memory&quot; we will be looking at the memory management
problems that programmers can still encounter and the solutions to
them. (The solutions are easy once you realise that the problems
and solutions exist.)</p>
<p>In &quot;Managing Other Resources&quot; we will examine the management of
other resources. In many programming languages (and C++ is often
cited in the literature) these can be dealt with by the same
mechanisms as managing memory. However, in Java, the commonality in
the way these problems are addressed is less obvious - garbage
collection addresses a lot of memory management issues but leaves
other resource management issues to the developer.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e131" id="d0e131"></a>Garbage
Collection and the Object Lifecycle</h2>
</div>
<p>In this section I explain what &quot;garbage collection&quot; does for the
developer and why it fails to be the &quot;silver bullet&quot; that many
think it is. With this knowledge we will be then equipped to tackle
the problems of managing both memory and other resources that may
be associated with an object.</p>
<p>In abstract terms &quot;garbage collection&quot; is a service provided by
the Java runtime environment to reclaim memory from objects that
the program is not going to use again. How does it know which
objects are not going to be used again? It doesn't - if it knew
instantly, and with complete accuracy, which objects are not going
to be used then the orthodox view would be valid. But, even in
theory, it is not possible to achieve complete accuracy in a useful
timeframe. Instead garbage collection follows rules that can
quickly identify objects that definitely won't be used again.</p>
<p>Although the details of how the runtime environment works out
which objects the program can or cannot access depend upon the
implementation of Java, there are certain rules that it must
follow. (I cannot give a single reference for these rules as they
are spread around the Java Language Specification and the JVM
specification and are not always stated explicitly.) These rules
tell the runtime environment which objects the program cannot use -
the trouble is that they can sometimes indicate that an object is
&quot;in use&quot; when the programmer has forgotten all about it and will,
in practice, never use it again. The object is dead but still
consuming resources - such &quot;zombie objects&quot; can lead to a program
consuming more and more memory until it fails (by slowing to a
crawl or by crashing).</p>
<p>The rules the collector has to follow are as these:</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>There are some references referred to as the &quot;root set&quot; (I'll
get back to this) - objects referenced by these are in use.</p>
</li>
<li>
<p>Any object referenced by a reference in an object that is in use
is also in use. (This is obviously recursive.)</p>
</li>
<li>
<p>Don't collect memory from any object that is in use.
<span class="emphasis"><em>There is no requirement to collect
memory from objects that are not in use.</em></span></p>
</li>
<li>
<p>Before collecting memory from an object call its <tt class=
"computeroutput">finalize</tt> method - remembering that <tt class=
"computeroutput">finalize</tt> might set a reference somewhere to
the object that changes it to be in use.</p>
</li>
<li>
<p>Don't call <tt class="computeroutput">finalize</tt> on the same
object twice.</p>
</li>
</ol>
</div>
<p>The definition of the &quot;root set&quot; is key to the working of the
collector and has changed subtly since the early days of Java (as
users of the SINGLETON anti-pattern may have discovered). But for
the programmer it suffices to assume that it includes any objects
whose methods are active on a call stack, those referenced by local
variables on a call stack, and by static member variables.
(Actually the latter are not really in the root set - they are &quot;in
use&quot; indirectly, by way of the corresponding class and classloader
objects - they could become unused if the class was not loaded by
the default classloader.)</p>
<p>It is significant that there is no requirement to collect memory
from objects that are not in use. There is no guarantee that an
object's memory will be collected - or that <tt class=
"computeroutput">finalize</tt> will ever be called. There is a
deprecated API - <tt class=
"computeroutput">System.runFinalizersOnExit</tt> - that purports to
ensure that all <tt class="computeroutput">finalizer</tt>s will be
called, but this has proved problematic:</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>This method is inherently unsafe. It may result in <tt class=
"computeroutput">finalizer</tt>s being called on live objects while
other threads are concurrently manipulating those objects,
resulting in erratic behavior or deadlock. [JDK1.4.1]</p>
</blockquote>
</div>
<p>Because one can never be sure that a Java object will be
finalised or have its memory collected it is never a good idea (as
already noted) to put any functional code in a <tt class=
"computeroutput">finalizer</tt>. And, unless its <tt class=
"computeroutput">finalizer</tt> contains functional code, once an
object becomes eligible for collection, it will have no further
effect on the state of the program - it can be forgotten.</p>
<p>Because comparisons are often drawn with C++ it is worth
expanding on this point of difference between the lifecycle of a
C++ object and that of a Java object. A typical C++ object has a
lifecycle like that of a Java variable of primitive type (like an
<tt class="computeroutput">int</tt>): it is created where it is
declared and no longer exists after the program leaves the scope in
which it is declared. In C++ objects are notified when their life
comes to an end (a special destructor &quot;method&quot; is called). In C++
programmers use this to free resources when an object goes out of
scope<sup>[<a name="d0e201" href="#ftn.d0e201" id=
"d0e201">1</a>]</sup>. The result of this is that the lifecycle of
a C++ object has a clear and predictable beginning (it is created)
and a clear and predictable end (it is destroyed). In Java objects
also have a clear and predictable beginning but they don't have a
clear end: instead the program just stops using them - after which
they <span class="emphasis"><em>may</em></span> be finalised, after
which their memory <span class="emphasis"><em>may</em></span> be
collected.</p>
<p>Instead of dying (like a C++ object) it has attained a form of
immortality - but this is at the cost of the programmer needing to
ensure that it frees up any resources it might be holding when she
finishes with it.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e216" id="d0e216"></a>Managing
memory</h2>
</div>
<p>When a programmer needs an object she gets it from somewhere
(usually by creating it using new), probably stores a reference to
it in a local variable, uses it for something (typically by calling
some methods on it), and then she forgets about it. This is a very
natural way to behave - we all do it unless we have had a reason to
learn to do otherwise. Parents will recognise this as the way young
children treat objects in the real world: they pick up a toy (or a
piece of cutlery), make use of it for a while and then forget about
it. Once a child has forgotten a toy it won't be long before they
want to use their hands for something else and the toy will be left
unattended. Eventually, a parent acts and, having decided that the
object isn't being used will either send a message to the child
(&quot;pick that up and put it away!&quot;) or collect and deal with it.</p>
<p>Programmers (and children) get an important benefit from having
things cleared up for them: it makes their life a lot simpler. Most
of the time this is good - it allows them time to concentrate on
matters that are important (most Java programmers are trying to
solve problems, not to manage memory). The Java programmers I know
were obviously children once and learnt some of the same
strategies. But there are differences: instead of holding objects
in their hands they now have reference variables. Children run out
of hands, while programmers can endlessly create more variables,
which means that there is no practical limit to the number of
forgotten objects that a programmer might be &quot;carrying around&quot;.</p>
<p>Java garbage collection is also different to most parents: it
will never ask the programmer to &quot;put that away&quot; it simply deals
with those objects that have been left lying around. Children don't
like being nagged and neither do programmers, so this might seem
great - except that putting things away is sometimes necessary. My
youngest son recently negotiated a &quot;no nagging&quot; deal for his
bedroom and has demonstrated that it becomes unusable in about a
week - and he has no idea how to resolve this problem. He tried
putting a couple of things away, but that didn't look any better -
so he gave up.</p>
<p>Programs can get into the same state as that bedroom if the
programmer relies on the garbage collector to magically take care
of everything. Java developers who believe that you don't have to
worry about memory management cause more problems than does the
need to do it &quot;by hand&quot; in C or C++. These programs might function
correctly for a while, but they use increasing amounts of memory
until they collapse. Once this happens I've seen developers make a
token effort at addressing the problem and then give up in the hope
that it won't matter.</p>
<p>This doesn't need to be the case: there are idiomatic ways to
manage memory in Java - it is ignorance of these idioms that causes
problems, not the language. Java's garbage collection is a tool for
managing memory - not a substitute. Garbage collection isn't unique
to Java and, despite it frequently being cited as an advantage over
C++ one may choose to use &quot;garbage collection&quot; in C++ but, by
deliberate intent or accident, most developments in these languages
don't use it. Each language provides a context and the developer
must learn the idioms that work in that context.</p>
<p>In Java the programmer needs to ensure that there are no &quot;live&quot;
references to objects that are no longer in use. This doesn't come
naturally - the lessons learned in childhood are not automatically
transferred into the made-up world of the JVM. There are parallels:
in the real world children have to learn to behave responsibly with
the objects they come in contact with, in the JVM world programmers
have to learn to behave responsibly with the objects they come in
contact with. The difference is that in the real world children
learn from adults whereas in JVM world programmers usually learn
from other programmers. (Science fiction writers such as William
Golding [<a href="#Golding">Golding</a>] have speculated on the
effect that a lack of adult input might have on children.) Why do
these differences arise? In part it is because in the made-up world
of the JVM we are freed from the constraints that arise in the real
world. In part it is because the rules of the JVM world have been
devised for the convenience of the implementers of that world.</p>
<p>Programmers are not stupid (and neither are children) but they
are in the business of producing simplified descriptions of things
(usually represented in code). Sometimes, however, they come up
with descriptions that are too simple to work. One such description
is a lifecycle of a Java object that goes: &quot;creation (using
<tt class="computeroutput">new</tt> or <tt class=
"computeroutput">createInstance</tt>), use (by accessing its
methods or member variables) and forget about it (garbage
collection will sort it out)&quot;. This is similar to a child in the
real world: &quot;create a game (by finding some toys), use (play with
them) and forget about it (parents will sort them out)&quot;. In the
real world parents will soon indicate that there is some learning
to do, in the JVM world programmers need to discover this for
themselves.</p>
<p>How does all this affect us as programmers? Well, the first
thing that is clear is that if we have any references to unwanted
objects then those objects will lurk around in limbo. This is quite
easy to do unintentionally: all we have to do put the object into a
collection and forget to remove it, or into a long lived variable
and forget to reset it, or...</p>
<p>The answer is simple: ensure that any long-lived references are
set to <tt class="computeroutput">null</tt> when the object they
reference is no longer in use. The problem the programmer has to
address is deciding when use of an object is complete.</p>
<p>References that exist in function scope are rarely a problem:
unless the code is written in a perverse style (e.g. excessively
long methods) then the scope of the reference will be approximately
the scope of use for the corresponding object. When this happens
the reference will go out of scope in a timely manner and the
object will become eligible for collection.</p>
<p>Instance reference members live as long as the instance that
contains them - which can be a very long time (e.g. a SINGLETON
lives &quot;forever&quot;). The problem for the developer implementing the
class is that it is the user of the class that controls its
lifetime - and must be relied upon to initiate any action that
resets the reference. In many cases this is not worth the effort of
solving (an obsolete reference will only hold memory for a single
forgotten object) unless the referenced object also holds
non-memory resources that must be released in a timely manner. (A
subject we'll revisit later.)</p>
<p>It becomes more important to deal with problems when there is a
possibility of holding references to multiple objects. For example,
it is possible to implement a stack using an array and a &quot;top&quot;
index. If references to multiple objects are pushed onto such a
stack the objects will be held in memory until the entries in the
array are cleared (or the array itself becomes eligible for garbage
collection). This implies that the &quot;pop&quot; operation should reset the
reference to the old &quot;top&quot; element.</p>
<p>Collections are where the problem becomes severe: for example,
there is a long-lived collection buried in the depths of the Swing
library that maps events to listeners. User code that adds
listeners to Swing objects causes these objects to be added to the
collection. In practice user code to remove these objects is rather
rare and, in early versions of Swing, this used to lead to the
collection becoming progressively larger each time a dialog (for
instance) was displayed. More recent versions of Swing addressed
this problem by using a special type of container: one that holds
<span class="emphasis"><em>weak references</em></span>.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e260" id="d0e260"></a>Weak
References</h2>
</div>
<p>Weak references are a feature introduced with JDK 1.2 with
<tt class="computeroutput">WeakHashMap</tt> and <tt class=
"computeroutput">WeakReference</tt>. These allow some additional
flexibility in garbage collection. A weak reference is recognised
by the garbage collector as one that does not prevent an object
being collected (but that must be reset should the object be
collected). They allow the developer to keep track of an object for
as long as there is a use of it somewhere else in the program, but
to release it once that use is complete.</p>
<p>The use of <tt class="computeroutput">WeakHashMap</tt> in the
Swing library is typical of the scenarios where weak references are
useful: in the OBSERVER pattern the subject should rarely affect
the lifetime of the observers.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e276" id="d0e276"></a>Managing
other resources</h2>
</div>
<p>In addition to the need to put objects away, there are also
objects that need to be switched off. It took a lot of batteries
going flat before my children learnt to switch off torches,
walkie-talkies, Gameboys and other toys that contain batteries.
Java objects can also contain resources that need to be &quot;switched
off&quot; - graphic contexts, file handles, etc. These are objects that
need to know when the programmer has done with them. For these
objects the programmer needs to develop the discipline needed to
supply the required notification.</p>
<p>In the vast majority of cases this is simply a matter of putting
a call to a release method where it will be executed on exit from a
block of code. There is even a convenient language construct for
doing this: the <tt class="computeroutput">finally</tt> block. It
looks like this:</p>
<pre class="programlisting">
public void repaint() {
  Graphics g = getGraphics(); // Allocate
  if (null != g) {
    try { paint(g); }
    finally { g.dispose(); } // Release
  }
}
</pre>
<p>In this code the scope rules are used to ensure that the paired
operations of allocation and deletion always occur as a pair (and
in sequence). This isn't hard - although correct examples are rare
in the literature.</p>
<p>As with managing memory the issue becomes problematic when
references to the owner of the resource are long lived (i.e. have
instance or class scope). If the graphics object in the above
example were referenced by a class member and accessed by a number
of methods then it could be difficult to determine when use was
completed. It may become necessary for the owning object to provide
its own equivalent of the <tt class="computeroutput">dispose</tt>
method and to rely on its user to call it.</p>
<p>The management of resources is at its worst when there are
longlived references to the same resource-owning object in
independent parts of the system. If this happens it can be very
difficult to release the resource at the right time - without
either implementing some convention for communicating between them
or electing one to be &quot;the boss&quot; none of these can confidently
release the resource. Fortunately, in real code this is rare.
(There are options: weak references, proxy objects that hold a use
count, etc.)</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e297" id=
"d0e297"></a>Conclusion</h2>
</div>
<p>I hope this has shown that there are memory management issues to
be addressed in Java and that these issues can be addressed. As so
often in our profession it is the acknowledgement that there is a
problem that is the key step to finding a solution.</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e302" id="d0e302"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Stroustrup91" id=
"Stroustrup91"></a>
<p class="bibliomixed">[Stroustrup91] Bjarne Stroustrup, 1991,
<span class="citetitle"><i class="citetitle">The C++ Programming
Language (2nd edition)</i></span>, Addison Wesley</p>
</div>
<div class="bibliomixed"><a name="Golding" id="Golding"></a>
<p class="bibliomixed">[Golding] William Golding, Lord of The
Flies, Faber and Faber</p>
</div>
<div class="bibliomixed"><a name="Flanagan" id="Flanagan"></a>
<p class="bibliomixed">[Flanagan] David Flanagan, 1997, Java in a
Nutshell</p>
</div>
<div class="bibliomixed"><a name="Niemeyer-Peck97" id=
"Niemeyer-Peck97"></a>
<p class="bibliomixed">[Niemeyer-Peck97] Patrick Niemeyer &amp;
Joshua Peck, 1997, Exploring Java (2nd edition), O'Reilly</p>
</div>
<div class="bibliomixed"><a name="Eckel98" id="Eckel98"></a>
<p class="bibliomixed">[Eckel98] Bruce Eckel, 1998, Thinking in
Java, Prentice Hall</p>
</div>
<div class="bibliomixed"><a name="JPAppGC" id="JPAppGC"></a>
<p class="bibliomixed">[JPAppGC] <span class="bibliomisc"><a href=
"http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html"
target=
"_top">http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html</a></span></p>
</div>
</div>
<div class="footnotes"><br>
<hr class="c3" width="100">
<div class="footnote">
<p><sup>[<a name="ftn.d0e201" href="#d0e201" id=
"ftn.d0e201">1</a>]</sup> Although C++ objects may also be created
dynamically like Java objects (that is, by using <tt class=
"computeroutput">new</tt>) these are idiomatically managed by
special &quot;smart pointer&quot; classes that ensure that the memory doesn't
leak. But, just as there are many Java books that fail to teach
idiomatic memory management, there are many C++ books that fail to
teach this.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
