The Ramifications of a Garbage Collected C++
by
Dare Obasanjo
Ever since I read C++ Answers From
Bjarne Stroustrop on Slashdot a few months ago I have been struck by this
quote "When (not if) automatic garbage collection becomes part of C++, it
will be optional". I am drawn by the fact that C++ may become a garbage
collected language and have decided to investigate what exists in C++ garbage
collection at the current time and also whether this means that maybe more new
server applications will start being written in C++ and not Java. My conclusions
and speculations are below.
What Exists Now
After clicking around Bjarne Stroustrop's site, I came across links to Hans Boehm's site on garbage
collection. There he has a well written page that dissects the advantages and disadvantages
of garbage collection in C++. From my readings it is clear that there are
several third-party garbage collectors for C++, but currently the C++ standard
makes no mention of garbage collection (although I heard that a proposal from
Bjarne Stroustrop was voted down sometime ago).
Basically the advantages of Garbage Collection are:
- 30 to 40 percent faster development time.
- Less restrictive interfaces and more reusable code.
- Easier implementation of sophisticated data structures.
- Eliminates some premature deallocation errors.
- Uses equivalent or less CPU-time than a program that uses explicit memory
deallocation.
While the disadvantages are
- More interaction with the hard disk (virtual memory/paging) due to
examining all pointers in the application.
- May not work if programmers use various tricks and hacks while coding
(e.g. casting pointers to ints and back)
- May utilize more space than a program that uses explicit memory
deallocation.
- Unpredictable nature of collector runs may create unexpected latency and
time lags in the system.
In my opinion, the advantages of using garbage collection in C++ greatly
outweigh any disadvantages and i may begin to user Boehm's toolkit.
What
Will Exist In The Future
From Bjarne Stroustrop's comments it is
clear that the C++ standard will eventually have garbage collection and even
better that it won't violate the "pay for it only if you use it" doctrine
of C++. Also from this article on
the MSDN site, Visual C++ will have automatic garbage collection provided by
the .NET environment.
Java vs. C++
Currently a lot of
server side applications are being built with Java due to factors like garbage
collection, a large number of libraries and buzzword compliance. In my opinion,
the one thing that is slowing development of C++ is the fact that every large
C++ application leaks memory (that's why we need Purify and BoundsChecker). I believe
that once C++ garbage collection becomes widespread (as it shall with both the
C++ standard and MSFT's .NET pushing for it soon) we will see a larger number of
reusable libraries spring up as well as better applications. Frankly Java™ is
cool but lack of asynchronous I/O, templates, global variables or enumerated
types will always make it second to C++ for me.
© 2000 Dare Obasanjo