LINUX TIPS AND TRICKS --- September 14, 2001

Published by ITworld.com -- changing the way you view IT
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; one 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 a database lock, for 
example. 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 could 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.


About the author(s)
-------------------
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). Danny can be reached at 
linuxnl@excite.com.
________________________________________________________________

ADDITIONAL RESOURCES

Explanation of "garbage collecting" Debug Message
http://www.cisco.com/warp/public/111/7.html

A garbage collecting technique
http://oop.rosweb.ru/users/v/vbelkin/agct.htm

The Memory Management Reference
Full Bibliography
http://www.memorymanagement.org/bib/f.html

ALLOCATE - a simple garbage collecting storage allocator
http://www.thinkage.on.ca/expl/b/lib/alloca.html
________________________________________________________________

CUSTOMER SERVICE

SUBSCRIBE/UNSUBSCRIBE:
- Go to: http://www.itworld.com/newsletters
- Click on "View my newsletters" to log in and manage your
  account
- To subscribe, check the box next to the newsletter
- To unsubscribe, uncheck the box next to the newsletter 
- When finished, click submit

Questions? Please e-mail customer service at: 
mailto:support@itworld.com
________________________________________________________________

CONTACTS

* Editorial: Andrew Santosusso, Associate Editor, Newsletters, 
  andrew_santosusso@itworld.com

* Advertising: Clare O'Brien, Vice President of Sales, 
  clare_obrien@itworld.com

* Recruitment advertising: Jamie Swartz, Eastern, Regional Sales 
  Manager, jamie_swartz@itworld.com or Paul Duthie, Western
  Regional Sales Manager, paul_duthie@itworld.com

* Other inquiries: Jodie Naze, Senior Product Marketing Manager, 
  jodie_naze@itworld.com
________________________________________________________________

PRIVACY POLICY
http://www.itworld.com/Privacy/

Copyright 2001 ITworld.com, Inc., All Rights Reserved.
http://www.itworld.com
