29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

30. Memory M<strong>an</strong>agement <strong><strong>an</strong>d</strong> Garbage Collection<br />

30.1 <strong>Introduction</strong><br />

This chapter describes how <strong>Smalltalk</strong> m<strong>an</strong>ages its memory <strong><strong>an</strong>d</strong> how automatic garbage collection is<br />

achieved. Automatic garbage collection <strong><strong>an</strong>d</strong> memory m<strong>an</strong>agement are one of the m ain features lacking<br />

from some other object oriented l<strong>an</strong>guages (such as C++ ). We therefore begin the chapter by<br />

considering why high level object oriented l<strong>an</strong>guages should provide automatic memory m<strong>an</strong>agement<br />

<strong><strong>an</strong>d</strong> garbage collection. A discussion is then pr esented of the way in which VisualWorks m<strong>an</strong>ages its<br />

memory <strong><strong>an</strong>d</strong> identifies those objects whose memory c<strong>an</strong> be collected <strong><strong>an</strong>d</strong> reused. The process, by which<br />

obejcts c<strong>an</strong> be relocated into perm<strong>an</strong>ent memory, is described. This c<strong>an</strong> improve the efficiency of the<br />

garbage collector. Following this we consider the exception h<strong><strong>an</strong>d</strong>ling facilities in <strong>Smalltalk</strong>.<br />

30.2 Why have automatic memory m<strong>an</strong>agement?<br />

One of the m<strong>an</strong>y adv<strong>an</strong>tages of <strong>Smalltalk</strong> over l<strong>an</strong>guages such as C++ is that <strong>Smalltalk</strong> automatically<br />

m<strong>an</strong>ages memory allocation <strong>an</strong> d reuse. It is not uncommon to hear C++ programmers complaining of<br />

the problems they have had spending m<strong>an</strong>y hours attempting to track down a particularly awkward bug<br />

only to find it was a problem associated with memory allocation or pointer m<strong>an</strong>ipulation. S imilarly, <strong>an</strong><br />

often heard problem from C++ developers is that of memory creep - a problem which occurs when<br />

memory is allocated at some point but never freed up. The application then either eats up all available<br />

memory or runs out of space thus producing a run-time error.<br />

Most of the problems associated with memory allocation in l<strong>an</strong>guages such as C++ are because the<br />

programmer must not only concentrate on the (often complex) application logic but also on memory<br />

m<strong>an</strong>agement. They must ensure that they allocat e only that memory which is required <strong><strong>an</strong>d</strong> deallocate it<br />

when it is no -longer required. This may sound simple, but it is no me<strong>an</strong> feat in a large complex<br />

application.<br />

An interesting question to ask is “why do programmers have to m<strong>an</strong>age memory allocation?”. Th is<br />

is a reasonable question to ask. For example, there are few programmers today who would expect to<br />

have to m<strong>an</strong>age the registers being used by their programs, however 20 or 30 years ago the situation<br />

was very different. One <strong>an</strong>swer to the memory m<strong>an</strong>agement question, often cited by those who like to<br />

m<strong>an</strong>age their own memory, is that “it is more efficient, you have more control, it is faster <strong><strong>an</strong>d</strong> leads to<br />

more compact code”. Of course, if you wish to take these comments to their extreme, then we should all<br />

be p rogramming in assembler. This would enable us all to produce faster, more efficient <strong><strong>an</strong>d</strong> more<br />

compact code th<strong>an</strong> that produced by Pascal, C++ or <strong>Smalltalk</strong>.<br />

The point about high level l<strong>an</strong>guages, however, is that they are more productive, introduce fewer<br />

errors, are more expressive <strong><strong>an</strong>d</strong> are efficient enough (given modern computers <strong><strong>an</strong>d</strong> compiler<br />

technology). The memory m<strong>an</strong>agement issue is somewhat similar. If the system automatically h<strong><strong>an</strong>d</strong>les<br />

the allocation <strong><strong>an</strong>d</strong> deallocation of memory, then the programmer c<strong>an</strong> concen trate on the application<br />

logic. This makes them more productive, removes problems due to poor memory m<strong>an</strong>agement <strong><strong>an</strong>d</strong><br />

when implemented efficiently, c<strong>an</strong> still provide acceptable perform<strong>an</strong>ce.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!