3 ms·
I'm curious about the following: Here's how Java allocates an object of size N: round N up to a multiple of 8 bytes, and bump the allocation pointer by that mu
by chris_wot 11y ago
I'm curious about the following:
Here's how Java allocates an object of size N: round N up to a multiple of 8 bytes, and bump the allocation pointer by that much. That's it. By using a scheme called a thread-local heap (TLH), there is no contention between threads. There's some additional handling for large objects, and for the case when the TLH is exhausted and another one must be allocated, but generally speaking, the GC is self-tuning so that none of these activities appear on the hot path of an application. The overhead is generally negligible, and all you're left with is the pointer bump.
What is to stop someone from implementing something like this in a C library? It sounds like it could be relatively easily done... or is it the TLH that causes issues?
- Manishearth 11y agoYou are correct. As I mentioned in https://news.ycombinator.com/item?id=11426339 https://news.ycombinator.com/item?id=11426339, jemalloc does this (http://linux.die.net/man/3/jemalloc http://linux.die.net/man/3/jemalloc, see the implementation notes), and you can use it pervasively so that all your existing malloc-using code links to jemalloc instead of the regular malloc. Ultimately the allocation strategy is an orthogonal concern to garbage collection, it's just that many GCs spend a lot more time worrying about the allocation strategy since they need to. It actually goes one step further than the TLH and does this round-robin stuff sharing the load between your processes.
- PDoyle 11y agoThe difference is that free() needs to be able to free objects one-at-a-time without knowing their size, and without moving any live data around. This inevitably leads to fragmentation, one way or another.