Is garbage collection slow, or is that a myth? Here we discuss the plusses and minuses of garbage collection and its performance statistics. It is interesting to think about when it is better and when it is worse than reference-counting or regular malloc. We want to respond to an interesting post at Lambda the Ultimate (linked below) about this topic.
Lambda the Ultimate is a site about programming languages and many of the articles deal with more abstract or theoretical concepts than just tutorials. There is an interesting post about the performance of garbage collection in computer languages there. We will look at the summary of the research paper and see what we can learn from it.
The researchers in the study tested Java programs that were written originally with the JVM, which uses garbage collection. The twist here is that they ran the Java apps with a regular memory allocator. This is the "novel experimental methodology". Sometimes it is possible for developers to "swap" memory allocators, and this is what they did.
As .NET developers, we have an interest in garbage collection being fast. However, this article reveals an interesting factor: because garbage collection (in the mark and sweep) visits so many different pages in memory, it has bad locality of reference, and virtual memory, which is much slower, suffers tremendously.
| If your program uses | Then garbage collection is |
| Mainly regular memory | 50% larger in memory 5-10% slower |
| Virtual memory | 10-15 times slower |
Yes and no. I know many programs that would not really be affected by a substantial slowdown. Additionally, as computers move to 64-bit, we will become less reliant on virtual memory, and have more regular memory. Because of this, it seems to me that these problems will become less pronounced in the future.
I don't feel that the difference between garbage collected programs and reference-counted ones is significant in modern programming most of the time. In fact, projects such as Mozilla Firefox seem to be moving to more garbage collection. This will improve the serious memory concerns many browsers have. Garbage collection can provide a more robust framework that is more resistant to "bloat".
Wikipedia, as usual, has good resources on garbage collection. At Wikipedia, read about garbage collection and virtual memory. Additionally, Lambda the Ultimate is an interesting site that deals with many parts of advanced programming. Look for this article at Lambda to see the reference in this post.
Garbage collection's merits have been debated by computer scientists for many years. .NET and Java has shown that practical applications can thrive on GC. However, high-performance or sometimes scientific programs still often prefer other approaches. As computers become more powerful, I feel that garbage collection will become an even more compelling option.