CS 4448 - Spring 1998
Object-Oriented Programming and Design
Discussion 7.2.1
by
Charles Griffis
Garbage Collection in C++: No panacea, but
useful
and
Memory allocation and C compatibility
by
Andrew Koenig
Garbage Collection in C++ addresses
issues concerning the utilization of garbage collection in C++
and the benefits and pitfalls derived therefrom. Benefits
include: increased programmability and software
reliability. Some concerns raised in the class discussion
and in the paper itself include: memory management/system
performance and pointer maintenance.
- Garbage collection reduces programming overhead wrt
memory allocation and de-allocation. In general
this reduces programming error while increasing software
maintainability.
While the benefits to the programmer are obvious, problems
associated with garbage collection are generally difficult to
discover and/or correct.
- Garbage collection is often implemented differently from
one system to another.
- Garbage collectors run periodically to reclaim memory
resources, often running when memory is demanded
most, thus increasing load and possibly crippling a
system with limited resources.
- Software which utilizes garbage collection tends to be
less efficient than software that doesn't.
- Garbage collection maintains a reference count to
allocated resources that prevents an object from being
deleted when a reference to it is removed and others
still exist. Although this is an important benefit
it can prevent a resource from being deleted altogether
if the object(s) in question represent a link looping
back upon itself or from one object to another.
C++: Memory allocation and C compatibility
addresses issues concerning the portability and
interchangeability of C's "malloc" and "free"
and the C++ methods "new" and "delete".
- In general these functions are not mutually compatible.
- C's "malloc" should not be used to allocate
memory in C++ for anything but "built-in" types
and scalars since it cannot determine the size of user
defined objects which require constructors for memory
allocation. The same is true for releasing
"non-built-in" types. C's
"delete" cannot determine the size of an object
allocated memory through the C++ "new" method.
- Great care must be taken by the programmer to ensure
portability between code utilizing both languages' memory
allocation and release operations.
Copyright © University of Colorado. All rights reserved.
Revised: March 5, 1998