Good memory management is essential for writing software applications that perform well. If the application takes too long to start or frustrates you as it completes operations, it doesn't make for a good experience. And there are many factors such as response time, working set, and hardware requirements to consider when dealing with performance. However memory management is a key ingredient, and deciding between manual and automatic systems can make a big difference.
This is such a large topic. Where should I start? ...
Lets start with some definitions. Manual memory management is when the programmer manually controls the lifetime of allocated memory by specifically allocating and freeing it in a deterministic fashion. Alternatively, automatic memory management tries to determine what memory is no longer used and frees it automatically instead of relying on the programmer to identify it. Automatic memory management is sometimes referred to as Garbage Collection (GC), however "garbage" could be defined as anything, so the term is a little vague. GC often refers to tracing garbage collection, one form of automatic memory management. Reference counting is an alternative automatic memory management method (when you Release an object it isn't necessarily freed, it all depends on the reference count, so as a consumer you do not control memory deallocation). The choice here is mostly independent of programming language. There are some languages that support manual management (such as C, C++), others that support automatic management with tracing GCs (such as Java, and C#), and others still that support both (like D).
So which one is better? Well the truth is that it all depends. There are many pros and cons to each method (discussed at length on wikipedia). In the end you have to pick the solution based on your specific requirements. However today lets talk about performance in particular. If you have some crazy high performance requirements (perhaps a real-time application), what do you do? .... you get more control.
By using manual memory management you are gaining more control over when memory is allocated and deallocated, giving you, the developer, more control over how to deal with it. You can then be mindful of such things as memory locality, consumption in tight loops, and memory reuse, while avoiding indeterministic deallocation (tracing garbage collectors). You can still have enough control with automatic memory management if you stick with ref counting as a means to control memory/object lifetime. However there is a cost to be paid for these advantages, mostly in development difficulty - the more control you have, the more likely you are to make mistakes (mistakes here lead to memory leaks). And mistakes are bugs.. some bad, some really bad.
- Login to post comments
-


radoshi | Wed, 2007-01-17 12:59
If you consider reference counting to be automatic memory management, C++ completely supports it as well. Have a look at boost::shared_ptr for details.
There are also some really fantastic GC survey papers which detail GC techniques such as mark and sweep and generational GC'ing.
Finally, you have to bear in mind that "manual" memory management is still not completely manual memory management - you're relying on the C or C++ std libraries to give you some reasonable chunk of space when you call malloc or new and to deal with fragmentation (or rather minimize it) and so on. There is yet another step of mem-management control you can get by using memory pools - the idea being that you allocate a huge pool of memory at program startup and then do all your memory management yourself (by overloading new and delete and other rather complicated things).
One reason that you missed out for doing manual memory management (besides high performance computing) is fault tolerant computing. If you have a service that really, really, really needs to be up, you don't want to be dealing with memory allocation failures and so you allocate a bunch of memory up front and then use your own pool. That way you know your memory limit and you can start failing things nicely if you're approaching the barrier (rather than just having a random new bork).
Good post though; memory management is a super-interesting topic!
-Rushabh
http://redefine.dyndns.org/~radoshi/blog
- Login to post comments
»