tags:

views:

281

answers:

7

5% of execution time spent on GC? 10%? 25%?

Thanks.

+2  A: 

It depends entirely on the application. The garbage collection is done as required, so the more often you allocate large amounts of memory which later becomes garbage, the more often it must run.

It could even go as low as 0% if you allocate everything up front and the never allocate any new objects.

In typical applications I would think the answer is very close to 0% of the time is spent in the garbage collector.

Mark Byers
I understand that it's depends on memory usage a lot, but maybe there are some typical overhead expectations?
Vladimir
@vladimir a typical expectation is not having to care at all
sfussenegger
+2  A: 

This blog post has an interesting investigation into this area.

The posters conclusion? That the overhead was negligible for his example.

So the GC heap is so fast that in a real program, even in tight loops, you can use closures and delegates without even giving it a second’s thought (or even a few nanosecond’s thought). As always, work on a clean, safe design, then profile to find out where the overhead is.

ChrisF
His conclusion is very clear. His final point to right clean, safe code and then profile to find issues is probably the best advice. To know your actual overhead, throw in some tests and some memory profiles. Then you'll know for sure.
Tim C
I think this blog post is a bit misleading. The author is running a single-threaded test and uses simple start and end time differences to "measure" GC overhead. Actually, he's measuring overhead for lambda's while GC will run in a different thread. (At least that's what I'd expect it to do. Could anybody confirm or rebut this?)
sfussenegger
@sfussenegger, as far as I can remember, I ran the test in a VM that was configured to use a single core of the physical machine. In any case, if it performs even better on multiple cores, that's yet another argument in favour of GC over manual memory management.
Daniel Earwicker
+8  A: 

As others have said, it depends. But, just for your reading:

Also, my question to you would be - why are you worried about the overhead (or are you just curious from an academic standpoint)? If there is a specific situation that you think the garbage collector will affect the performance of your application, please ask about that situation and you may be able to get better responses. Otherwise, there are more important things to worry about (from a performance standpoint) than the garbage collector - the first being your own code/algorithms/etc.

JasCav
Thanks, both articles are great!It's mostly an academic interest - in some paper was mentioned like 20% overhead on couple of benchmarks, and it was really surprising for me. So I just want to find what others think on GC performance losses.
Vladimir
What a fine blog post that first one is! :)
Daniel Earwicker
@Earwicker - Haha...did I get a +1 for it. ;-) (And, it was an interesting read, so, thanks!)
JasCav
+3  A: 

Watch Red Gate's video here. The best GC explnation I've encountered with.

sagie
+1  A: 

The overhead varies widely. It's not really practical to reduce the problem domain into "typical scenarios" because the overhead of GC (and related functions, like finalization) depend on several factors:

  • The GC flavor your application uses (impacts how your threads may be blocked during a GC).
  • Your allocation profile, including how often you allocate (GC triggers automatically when an allocation request needs more memory) and the lifetime profile of objects (gen 0 collections are fastest, gen 2 collections are slower, if you induce a lot of gen 2 collections your overhead will increase).
  • The lifetime profile of finalizable objects, because they must have their finalizers complete before they will be eligible for collection.

The impact of various points on each of those axes of relevancy can be analyzed (and there are probably more relevant areas I'm not recalling off the top of my head) -- so the problem is really "how can you reduce those axes of relevancy to a 'common scenario?'"

Basically, as others said, it depends. Or, "low enough that you shouldn't worry about it until it shows up on a profiler report."

Josh Petrie
+1  A: 

Yes, the Garbage Collector will spend some X% of time collecting when averaged over all applications everywhere. But that doesn't necessarily means that time is overhead. For overhead, you can really only count the time that would be left after releasing an equivalent amount of memory on an unmanaged platform.

With that in mind, the actual overhead is negative, but the Garbage collector will save time be release several chunks of memory in batches. That means fewer context switches and an overall improvement in efficiency.

Additionally, starting with .Net 4 the garbage collector does a lot of it's work on a different thread that doesn't interrupt your currently running code as much. As we work more and more with mutli-core machines where a core might even be sitting idle now and then, this is a big deal.

Joel Coehoorn
A: 

It really can vary. Look at this demonstration short-but-complete program that I wrote:

http://nomorehacks.wordpress.com/2008/11/27/forcing-the-garbage-collector/

that shows the effect of large gen2 garbage collections.

No More Hacks