7

I am just reading about the performance of several programming languages, and I noticed that garbage collection gets mentioned fairly often. Does garbage collection affect the performance of a language at all? If yes, how?

user3289157
  • 615
  • 1
  • 12
  • 24

3 Answers3

4

One thing to keep in mind here is that a language without garbage collection still has to allocate and free objects. Garbage collection allows the "free" part of the operation to happen in batches, and at times when the application is otherwise relatively idle. In other words, a highly-efficient garbage collector has the potential to be faster (not that we're quite that far yet, but they are improving all the time).

The trick is that when garbage collections do occur, the collector still has to spend resources doing the analysis of what is reachable, and what isn't, or what has a reference count > 0, and what doesn't. That is a performance penalty.

The result is that, for most garbage collected platforms, the collector is one factor to consider when evaluating performance. The net effect of the garbage collector may be minimal — or even a net positive! — over the life of an application session, but there can short-lived periods where there is significant negative impact.

Joel Coehoorn
  • 380,066
  • 110
  • 546
  • 781
3

It mostly depends on the language/runtime.

In some implementations, the GC can pause your application (suspend the threads), and then perform a collection, hence the performance problems.

Some implementations perform optimizations that let your code run while the GC is collecting unreferenced objects. For instance, the .Net GC can be tuned to use different collection modes and object heaps, and you also can give it a hint not to run in performance sensitive regions of code.

I can't really give more details as all of this is implementation specific. But I should mention that language runtimes most often try to optimize their GCs really hard, and most of the time you won't even notice it's there. It can become a problem though in very specific cases, and hopefully there's a possibility to work around that to minimize the GC pressure.

Community
  • 1
  • 1
Lucas Trzesniewski
  • 48,720
  • 10
  • 100
  • 148
  • To add to this: .Net optimizes things be separating _collection_ from _compaction_. The GC can do it's thing while other threads are running, because it handles consolidating the memory holes as part of a separate process, and therefore isn't going to move an object out from under another thread. – Joel Coehoorn Aug 14 '14 at 16:34
0

Garbage collection is a mechanism to clean unused object to free resource allocation, the garbage collector works base on an robust algorithm to allocate which object should be clean and which not. This process is resource consuming so it is always advice that developer should explicitly release or clean up unused object.

Using proper collection should be consider to improve performance and resource consuming of application. Strong-typed and array are always have high priority to use to avoid boxing/ unboxing as well as reduce resource costing to manage the collection.

Please refer to using Array, ArrayList, HashTable.

CodeSi
  • 351
  • 2
  • 6