3 ms·
> Garbage collection usually works out to a performance / latency tradeoff. (A GC app runs faster than a non-GC app, but has latency.) I think there are severa
by alkonaut 9y ago
> Garbage collection usually works out to a performance / latency tradeoff. (A GC app runs faster than a non-GC app, but has latency.)
I think there are several layers to this. First of all, GC'd data is on the heap, and often you are already doomed if you do stuff (in spread out places) on the heap at all because of cache issues.
Secondly, if we consider "normal" applications that have small parts of perf sensitive code, while a majority of the codebase is not (such as e.g. a 3D rendering application where you hit a button and it goes of and renders an image for 1 minute), then you'd have plenty of time to set up your data, and then you just "operate" on it for the critical part of time. The same isn't true in all scenarios.
Same thing with e.g. a game engine. It's fine to allocate massive amounts of memory, but then in each frame when you have a 16ms budget you allocate nothing. Because the delay loading a level doesn't matter whether it's 5 sec or 10, but you can't spend 17ms in a frame instead of 16ms.
Another problem (and benefit) in most GC'd languages is that the GC is there for developer convenience, and the class libraries know its there so all standard library features (such as just iterating over a collection of items) risks creating new heap allocated objects, which might trigger new garbage collections. In e.g. C# I can't make a standard list with a custom allocator. And I can't "foreach" over something and be sure I didnt' allocate etc.
> A GC app runs faster than a non-GC app, but has latency
I didn't mean to compare an app that does "new Foo()" in the inner loop with one that does "malloc(...)" in the inner loop, I was trying to make the point that you can't do either in the critical part.
> then I know I chose the wrong language.
Right, but the thing is you might have chosen the perfect language for 90% of your code. The language choice might be too late to change and you just need that concurrent inner loop (rendering loop, encryption pass, whatever) to be quick, and the solution is a lot of time to just allocate nothing.
- stmw 9y agoThe issue is that the tradeoffs are unclear, it's not like you pay 10% performance penalty for using a GC - it's often much worse than that, and then you have to do manual work to "tune it". Often the tuning, following production problems, is not that different than using malloc()/free() to begin with.
- zaarn 9y agoWhen using a GC you can get 1:1 performance to non-GC if you preallocate. Both don't do anything to the memory, hence they have provably the same (possible) performance. Most applications don't require tuning and have acceptable performance for real world usage. The reason people write in GC languages is because malloc() and free() have error cases you can avoid with a GC. A GC is a well tested structure and less likely to leak memory by forgetting a free() or doing a use-after-free. Of course we have static analysis tools for this but those aren't perfect. There are perfectly legit reasons to use GC, one of them being not having to do malloc()/free() at the cost of some performance characteristica which can be "tuned" as you describe it.
- stmw 9y ago> "When using a GC you can get 1:1 performance to non-GC if you preallocate." That is not always true. Also, preallocation can be impossible (due to limit on heap size) if there is no way to free memory without the GC running. Of course, you're right that there are tradeoffs, the only argument here is about the magnitude of the tradeoffs - the OP shows a hidden cost of GC that is not talked about often enough.
- weberc2 9y agoWhat are the cases where the is untrue? Are they mainstream or niche? It would be a shame to caution people away from GC by default because there exist niche cases where GC performance doesn't match manual management.
- vvanders 9y agoIt's not always possible to get cache coeherancy with a ref based language which can be 10-50x difference in performance.
- zaarn 9y agoRefs might be but they aren't inherent to GC, you can have Refs without GC. I would love to see a benchmark of GC vs non-GC using the same compiler and language for an accurate comparison. Probably also GC vs non-GC naive vs non-GC optimized.