5 ms·
In general the speed is lost because we make the CPU consume cycles to do things that are not necessary in order to solve a particular problem: -unneeded alloc
by DeathArrow 5y ago
In general the speed is lost because we make the CPU consume cycles to do things that are not necessary in order to solve a particular problem:
-unneeded allocations
-boxing/unboxing
-garbage collection
-interpreting
- kaba0 5y agoIf garbage collection happens on a separate thread, and makes allocation much faster, is it really “slower”? You have to call malloc which will try to defragment your memory, and then later you will have to call free. Those block the calling thread, if anything, for certain problems they are slower than GC.
- ygra 5y agoGarbage collection itself isn't really slow, per se. But allocating a lot of short-lived objects on the heap still means that a lot of objects have to be reclaimed fairly frequently. And at least .NET's garbage collector isn't able to do all that without pauses. And those add up. But even if the GC is really concurrent: If there's a way of not doing that work it's still better, IMHO. One interesting profile I've seen at work recently spent about 30 % creating objects, and another 35 % in garbage collection (of pretty much the same objects that have been created all the time). So if there was a way of not allocating that much, or not doing it on the heap, the algorithm could be about twice as fast.
- kaba0 5y agoBut comparing it to not doing that work is somewhat dishonest — for that you use compare a malloc for each call and a destructor at the end of the scope — and surprisingly, malloc will often do much worse than a good GC implementation, with trying to defragment a bit, etc. Also, Java can often accumulate big heaps because it only runs the GC when it absolutely must — as you mentioned, it would be unnecessarily work otherwise. It might be interesting to mention that OpenJDK is the “greenest” out of the managed languages due to that.