4 ms·
> If you make something that is performance critical, and in particular if it's concurrent, the normal rule is you avoid allocating on the heap at all. Use obje
by gwbas1c 9y ago
> If you make something that is performance critical, and in particular if it's concurrent, the normal rule is you avoid allocating on the heap at all. Use object preallocated data (just operate on massive lists), or use pools of data etc.
Which really defeats the point of the garbage collector, because the good ones do that kind of work for you! The good garbage collectors re-use pre-allocated ranges and pools of data.
Garbage collection usually works out to a performance / latency tradeoff. (A GC app runs faster than a non-GC app, but has latency.) If I get to the point where latency, even with a pause-free of concurrent GC is a problem, then I know I chose the wrong language.
- 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.
- 9y ago
- klodolph 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.) If I get to the point where latency, even with a pause-free of concurrent GC is a problem, then I know I chose the wrong language. I think you mean "throughput / latency"? Latency itself is a measure of performance. It's a fair bit more complicated than that once you start tuning the GC, because the GC can be tuned towards either throughput or latency, and it can be tuned aggressively towards either end. For example, for a given program you can tune the GC so you get fantastic throughput for batch programs. Use a copying GC, and set the heap size large enough that the percentage of live pages is very low when you collect. On the other hand, you can use something like the tri-color collector, which runs in parallel with the main thread and kills throughput, but has minimal impact on latency (sub-millisecond pauses).