LINUX TIPS AND TRICKS --- July 21, 2000 

Published by ITworld.com, the IT problem-solving network 
http://www.itworld.com/newsletters 

*********************************************************************

Reflections on Garbage Collection 
by Danny Kalev 

Many C/C++ programmers think of automatic garbage collection as the Holy 
Grail of programming languages that will free them from every 
conceivable bug. This approach is wrong, though. A garbage collector 
can't handle every resource that might leak. A typical garbage collector 
takes care of recycling unreferenced memory. In certain programming 
languages, it can reclaim leaked threads and even close open files; 
However, many other platform- and application-specific resources still 
need to be released explicitly by the programmer. 

Think of a modem. An application that uses it must release it explicitly 
so that other applications can use it; how many garbage collectors are 
modem-aware? Take another example:  a database lock. Here again, you 
can't expect a garbage collector to close an unused database lock 
automatically (indeed, most RDBM systems set a timeout limit on each 
transaction but relying on timeouts instead of closing a lock as soon as 
possible results in poor performance and constant deadlocks). Thus, a 
garbage collector can be useful as long as it takes care of reclaiming 
unreferenced memory. When it comes to reclaiming other time-critical or 
scarce system resources, you still need to release them explicitly. 

When you think of dynamically allocated objects as an instance of such 
resources, you may realize that garbage collection isn't really that 
useful after all. Furthermore, using standard C++, you almost never need 
to deal with dynamic memory directly. Instead of using char arrays, bare 
pointers and dynamic memory allocation, use string objects, auto_ptr 
template, and STL containers. These classes manage their storage 
automatically. Note also that, in many cases, you can create objects on 
the stack rather than allocate them on the heap. 

That said, for certain applications (especially ones developed in C), 
you can install a free garbage collector such as the Boehm-Demers-Weiser 
conservative garbage collector (see 
http://www.hpl.hp.com/personal/Hans_Boehm/gc/). Unlike a copying garbage 
collector, a conservative garbage collector doesn't move objects from 
one memory location to another. It uses constant sized memory chunks. 
This way, the heap doesn't become fragmented. 

Resources 

Taking out the trash 
Make sure you understand Java garbage collection before you design your 
next Web application. 
http://www.sunworld.com/sunworldonline/swol-09-1998/swol-09-itarchitect.html 

The evolution of storage clustering 
We've come a long way from the days of manual disk copying. Take a trip 
through the progression of storage clustering -- from disk sharing to 
the storage area network. 
http://www.sunworld.com/sunworldonline/swol-09-1998/swol-09-connectivity_p.html 

************************************************************************ 

About the author 
---------------- 
Danny Kalev is a system analyst and software engineer with more 
than 10 years of experience, specializing in C++ and 
object-oriented analysis and design on various platforms including 
VMS, DOS, Windows, Unix, and Linux. His technical interests involve 
code optimization, networking, and distributed computing. He is 
also a member of the ANSI C++ standardization committee and the 
author of ANSI/ISO C++ Professional Programmer's Handbook (Que, 
1999). Contact him at linuxnl@excite.com. 
  
*********************************************************************

CUSTOMER SERVICE 

You can subscribe or unsubscribe to any of your e-mail newsletters by 
updating your form at: 
http://www.itworld.com/cgi-bin/w3-msql/newsletters/subcontent12.html? 

For subscription changes that cannot be handled via the web, please send 
an email to our customer service dept: support@itworld.com 

*********************************************************************

CONTACTS 

* For editorial comments, write Andrew Santosusso, Associate Editor, 
Newsletters at: andrew_santosusso@itworld.com 
* For advertising information, write Dan Chupka, Account Executive at: 
dan_chupka@itworld.com 
* For all other inquiries, write Jodie Naze, Product Manager, 
Newsletters at: jodie_naze@itworld.com 

********************************************************************* 

Copyright 2000 ITworld.com, Inc., All Rights Reserved. 

http://www.itworld.com 
