If you are experiencing heap-overflow problems in a language like Java, it's important to understand key principles of how such languages are designed to work: most importantly, garbage collection.
Unlike languages such as "C," you don't have to explicitly free anything. You can acquire a new object, use it, and forget about it. The garbage-collection routines will clean up after you, reclaiming the space you're no longer using.
The Java specification does not stipulate how the garbage collector will work, only that there must be one. The most common implementation for a garbage-collector uses reference counting. An object continues to exist until its reference-count becomes zero. Counter-based garbage collectors are favored because they are both reliable and efficient.
Heap-overflow problems often occur because of self-references. In other words, a chain of objects exists such that all of them refer to each other, so the reference-count for any of them never drops to zero.
The classical solution to this problem is to use one or more of what are called "weak references" when building the structure. These are references which do not prevent the referent from being garbage-collected. Instead, whenever the referent is reaped, the weak-references to it are "cleared."
Languages such as Java also highly-favor the notion that you should never have to build what you can simply get "off the shelf." Therefore, when you design things be sure that you're not being too clever; you're not being too manual. If there's an off-the-shelf package or class that does what you need to do, by all means use it. If you find yourself hand-building, say, "linked list" code in a system like Java, "friend, you are doing something very wrong."
Last edited by sundialsvcs; 02-05-2008 at 12:19 AM.