by ajross · 7y ago · 0 comments
> Garbage collection is by no means a solved problem.
I was reading articles like this one, about new ideas in JVM garbage collection, literally two decades ago. And it's still "not a solved problem".
I mean, it's mostly solved. Managed heaps work great for lots of applications and have been very successful. But that last 5% has stretched out so long it seems almost like a joke. We'll never get there, for the mythical "there" where GC overhead and latency isn't an issue that needs to be tuned in deployment. And IMHO it's time to start recognizing that fact instead of trying to make the JVM and .NET do what we get from explicit heaps in C and Rust. They just aren't going to get there.
I think that for the most part we're "there" already. Such improvements in GCs, like Shenandoah, C4 and ZGC, are more than keeping up with most applications' evolving requirements, and yield tremendous benefits for the relatively low cost of added RAM footprint.
> it's time to start recognizing that fact instead of trying to make the JVM and .NET do what we get from explicit heaps in C and Rust.
To do better than the JVM, especially in concurrent applications, you have to work hard. What we "get" from manual memory management we actually buy for a rather steep price (and remember that for shared data structures, which are extremely important in concurrent applications, Rust also uses a GC, just a particularly primitive one -- reference counting).
And even what we buy is not universally "better." I just saw this nice benchmark the other day, that compares allocation costs (even without concurrency). As always, it gives a small part of the picture, but a very real one: https://github.com/rbehrends/btree-alloc. I don't think anyone can reasonably claim that manual memory management is "generally better" than modern GCs, even once the effort is invested. It's better in some respects and worse in others. So it's not even clear which of them has that "last 5%" (and sometimes more) advantage.
The outrageous amount of work that's gone into two new GC ideas over the last half century has, almost exclusively, been trying to address the latency excursions inherent in trying to collect a giant heap. And it's a completely manufactured problem, because refcounting (and manual allocation strategies more generally) never head it to begin with!
So... I just don't see the point you're trying to make. I mean, yeah, there are a handful of workloads where a good GC will beat naive heap allocation (though the heap solution can always be optimized to beat a GC, by definition). But everything has its sweet spot.
My point is that Java puts its bet on the idea that the GC will work better for effectively all useful workloads. And that's not true. It hasn't been true for 20 years (c.f. all the GC work we're discussing), and it won't be true in the next 20 years either. "We won't get there", as it were.
What space is that? The big applications that the JVM targets have very large working sets (hundreds of GBs or even a couple TBs).
> The outrageous amount of work that's gone into two new GC ideas over the last half century has, almost exclusively, been trying to address the latency excursions inherent in trying to collect a giant heap.
But that work has been successful! We're now at 1.5ms max pause with an acceptable cost to throughput.
> And it's a completely manufactured problem, because refcounting (and manual allocation strategies more generally) never head it to begin with!
This is just false. Ref-counted deallocation of a large data structure also introduces latency. Unless you're also controlling the allocator (which is more work), it's also not entirely deterministic.
> there are a handful of workloads
Those "handful of workloads" are the ones that interest those companies running on the JDK, like Google, Netflix, Apple, Twitter, Amazon and many, many others. I would agree that some use cases benefit more from manual/refcount-collection and some benefit more from tracing, but the market for tracing GCs is humongous.
> It hasn't been true for 20 years (c.f. all the GC work we're discussing), and it won't be true in the next 20 years either. "We won't get there", as it were.
Well, I strongly disagree, and the millions of happy JDK users will likely disagree, as well.
Further, manual heaps tend to scatter your small blocks all over your address space, making your heap work harder as well as doing bad things to your caches. (Unless you go to the work of building cache-aware data structures, which most developers are not capable of considering that they have problems with the discipline needed to make manual management work at all.)
There's no free lunch, but if you know heap behavior is a performance issue in a giant C++ app or whatever, you have a big list of tools with which to address it in software. And in practice this works. Large deployments of, say, PostgreSQL don't require that the admins tune the allocation strategy of the process or carefully size the heap memory internal to the process (obviously there is external tuning at the OS level, though).
I really like the Rust approach, but (I believe) it is impossible so have heap compacting at run time in practice and similar things.
There was here an article about SQL that pointed out how Oracle would give you estimates of run time for a query, choose the estimated best data structures and algorithms, and also switch on the fly if it realizes the query was taking to long. As another example of the kind of techniques that are possible only from a higher abstraction.
I am not sure how the thread situation is for GCs and Java, but Arc is indeed reference counting and I believe it is used quite often in rust.
This meme that reference counting and garbage collection are even remotely the same needs to die in a fire. Reference counting is not, and never has been, "primitive garbage collection", and garbage collection is not, and never has been, "super sophisticated reference counting".
Then you learn why it sucks and not for taking space for counter.
Reference counts are used to manage resources, one of which is memory (and I'm assuming you are referring to no-longer-referenced memory as "garbage").
But that's not the only use for reference counts! You can, and should, use reference counts for external resources, such as files, or memory buffers on a GPU, or….
Garbage collection is COMPLETELY USELESS for managing non-memory resources because you need to be able to deterministically release non-memory resources (and oftentimes, memory resources—e.g., on the GPU). And since all a GC can do is examine memory, well, it doesn't work.
Go ahead: point me to a 3D game engine using garbage collection for GPU resources. I'll wait.
So yeah: "reference counting is a subset of garbage collection" is absolutely not true, and even the most basic knowledge of how reference counting is used in practice should be enough to convince anyone of that.
Again: this meme needs to die. Garbage collection is not "super sophisticated reference counting", and in fact, can't even be used in most places where reference counting is used. It's not a substitution, or a superset, of reference counting.
GC is a compelling alternative to reference counting for resources just in the main memory heap in some situations where performance (esp. total memory usage) isn't an important concern relative to, say, programmer productivity.
Tracing and reference counting are two GC approaches that the paper shows are dual in some interesting ways and can have similar characteristic -- when both are sophisticated enough.
So here's how reference counting and garbage collection are duals: they both manage the memory lifetimes for objects not on the stack. If that blows your mind, you also might enjoy the above referenced paper and the meme that reference counting is "GC done badly".
I disagree.
Java's concurrency features are extremely rich. Parallel stream are basically magic: I add .parallel() and pow, near-linear speedups. No channels, no select statements, no realising I messed up a mutex somewhere. It just works.
Then there's reactive programming. The JVM can chew through truly absurd amounts of requests when a reactive approach is taken.
Citation needed.
The JVM has a lot of problems, mostly due to its concentration on Java and bytecode/JITting. Given those constraints, memory management isn't really one of them.
A fair part of "solving" gc is optimising for context-specific tradeoffs as application domains and environments change. That will continue forever unless we all agree to stick with one architecture to build all of our programs. That's the reason different gcs fare better now than in the 90's, for example, when nobody cared about concurrency.
The linked article is literally about a complete rewrite of the JVM garbage collector!
"Mature" technologies are things like compilers, where e.g. gcc was last significantly reworked 25 years ago and the "new upstart kid" competitor clang is in its second decade of life. Garbage collection isn't mature, it it was no one would be rewriting it and blogging about the results.
Also, GCC and Clang are not the only compilers in existence. There's plenty of new compiler research going on.
The GCs represent "good enough" for most use cases.
[Edit] Also, 1993: "The Measured Cost of Conservative Garbage Collection" (http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.14.1...) [Anyone seen any work on conservative GC for existing languages since Boehm?]
Throughput-style garbage collection has mostly been solved; I believe the only outstanding problem is to reduce the necessary memory footprint towards the minimum required by the application. (On the other hand, the best memory management scheme is still an old-style Unix proggy that does bump-allocations in an arena and never frees memory until it terminates. Just don't let it run too long.)
Most of the work in GC has been towards reducing latency, which typically means different GC algorithms for different tasks. The ideal of a fast, real-time GC is still an open research project.
Explicit heaps are still not a solved problem, either, and they've been around longer than GC work (ahem*). With no static checking for leaks, use-after-free, and other issues, C memory management can be a nightmare unless you have iron-willed development discipline. The only real tools for dealing with those issues are run-time testing tools like valgrind, which among other things are horribly slow if you want to run them against a real workload.
Rust is an interesting case: it does an amazing job, if you use it for C++-like RAII idioms. If you don't, or can't, you either can't get there from here, or you're in deep unsafe voodoo land.
(One issue I brought up the last time this came up was the perennial graph structure: the best representation of edges is a numeric index into a vector of nodes. It works, mostly. However, that numeric index is acting almost exactly like a raw pointer into the vector. (The only difference is that the vector will insure the index is currently valid when you dig out a node.) The index, specifically, doesn't track the lifetime of the node or the overall graph, so you may get a node, just not the node you were looking for.
Now, if you wanted to bring up explicit linear or affine memory management systems, ala ATS, where the allocation comes with a statically checked certificate that the pointer is valid and which must be explicitly destroyed by freeing the memory for the program to compile, I would be singing in the chorus with you. But those are research problems, too.
In the mean time, unless you are working on a problem that explicitly requires managing your own memory, you would probably be advised to use a garbage collector, and one of the many choices that works best with your problem, for a fair number of reasons.