News for hackers. Stuff that matters.
Re:GC Tuning Confessions of a Performance Engineer by banachtarski on Friday May 29, 2015 @12:11PM (#9625451)
Having worked with all sorts of GCs in the past, I basically stopped using them altogether some years ago in favor of manual memory management. I have to say, it's been liberating and so much easier to write performant code since I know what memory I need instead of relying on the computer to guess (and giving the computer hints as to how to guess). I have no doubt that GC research and systems is making good forward progress, but I can't imagine myself using them again.
Re:GC Tuning Confessions of a Performance Engineer by pjmlp on Friday May 29, 2015 @02:30PM (#9626410)
Manual memory management only works in small teams with highly skilled developers, and even then there is a reason why so many CVE exploits are related to memory corruption.

Even those highly skilled developers aren't failure prof.

Re:GC Tuning Confessions of a Performance Engineer by banachtarski on Friday May 29, 2015 @03:16PM (#9626672)
Man, the new generation of programmers are going to be in bad shape if the thought of stack unwinding or managing the heap yourself is left to the "highly skilled." Even if I was interviewing someone who only worked in a memory-managed language, I would hope that they'd at least be capable of understanding how to code without it.

These should be the fundamentals.

Re:GC Tuning Confessions of a Performance Engineer by pjmlp on Friday May 29, 2015 @03:24PM (#9626736)
Well I started coding in 1986, and have seen quite a few screw ups.

So yeah, small teams it works great.

Now scale that to developer teams > 30 on average, with high turnaround and multiple outside partners coming and going on project basis.

I have seen what off-shoring does to C and C++ code bases...

Re:GC Tuning Confessions of a Performance Engineer by banachtarski on Friday May 29, 2015 @07:29PM (#9628160)
How many engineers work on, say, the Unreal Engine. The linux kernel? Windows? Do you think any of that stuff would work even marginally well running on the JVM (assuming the JVM had native driver support)?

I don't quite follow your claims, nor do I put any stock in the tenure of your programming career.

As for the off-shoring point, off-shoring is going to create questionable quality code regardless of the programming language.

Re:GC Tuning Confessions of a Performance Engineer by pjmlp on Saturday May 30, 2015 @01:24AM (#9629128)
> How many engineers work on, say, the Unreal Engine. The linux kernel? Windows?

So you just provided me examples where developers tend to be highly skilled just to get a foot in the door.

Yet, the CVE list gets updated regularly with memory corruption exploits for them.

https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=double+free

Or if you prefer, just for Linux

https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=linux+doubl...

> Do you think any of that stuff would work even marginally well running on the JVM (assuming the JVM had native driver support)?

Actually, are you aware that some military weapon systems are being driven with JVMs?

Anyway automatic memory management doesn't mean automatically a JVM, there are quite a few other ways.

Another fun fact, Unreal Engine and Windows are written mostly in C++. A language considered too bloated and slow to be usable for anything serious by mainstream developers in the early 90's.

Also both use automatic memory management on their systems. Unreal has a kind of GC library. Windows nowadays has COM almost everywhere with reference counting.

> I don't quite follow your claims, nor do I put any stock in the tenure of your programming career.

Apparently some well known Fortune 100 companies and research institutes had another opinion.

> As for the off-shoring point, off-shoring is going to create questionable quality code regardless of the programming language.

The point being that those projects tend to go for cheap developers, so no, not everyone can deal with manual memory management.

Re:GC Tuning Confessions of a Performance Engineer by banachtarski on Saturday May 30, 2015 @04:42AM (#9629500)
> So you just provided me examples where developers tend to be highly skilled just to get a foot in the door.

Yep these are people I want to work with :)

> Yet, the CVE list gets updated regularly with memory corruption exploits for them

Security is not the concern of every piece of software. You can harden the pieces you care about. Honestly, not all of us are doing crypto, and for that, the recommendation is to leverage an existing library and sandbox it anyways (regardless of memory management strategy).

> Actually, are you aware that some military weapon systems are being driven with JVMs?

God help them

> Another fun fact, Unreal Engine and Windows are written mostly in C++. A language considered too bloated and slow to be usable for anything serious by mainstream developers in the early 90's.

I was developing in C and C++ in the 90s. C++ is a strict subset of C. The criticisms made no sense then and they make no sense now. C++03 was arguable an small incremental improvement over C++98 but C++11 is a huge step function in usability without sacrificing performance (which Rust has taken many good cues from). The point is, if something bad is happening, I can look at the dissassembly and see exactly what's going on. I can't do this with Java, Python, Ruby, etc.

> Also both use automatic memory management on their systems. Unreal has a kind of GC library. Windows nowadays has COM almost everywhere with reference counting.

The scripting engine in Unreal supports GC if you want it. It's trivial to embed Lua or something. That's part of the point. For the non-performance sensitive bits, sure do whatever. The COM interface is terrible, and a necessary evil for those in the industry. There's a reason DirectX12 is looked forward to. I don't want Windows reference counting my things any more unless I tell it to.

> Anyway automatic memory management doesn't mean automatically a JVM, there are quite a few other ways.

... yes, I honestly can't imagine anyone in this thread would think otherwise. Heck, I've written a GC for a homebrew scripting language.

> Apparently some well known Fortune 100 companies and research institutes had another opinion.

My point is that how long you worked is sort of irrelevant in an actual academic discussion. Where you worked is just as irrelevant.

Re:GC Tuning Confessions of a Performance Engineer by pjmlp on Saturday May 30, 2015 @05:44AM (#9629605)
> Yep these are people I want to work with :)

Which comes back to my point that only developers able to get hired at that level are able to do manual memory management in large scale.

And even then, they resort to automatic memory management at higher levels in their stacks.

> I can look at the dissassembly and see exactly what's going on. I can't do this with Java, Python, Ruby, etc.

Just get a commercial JVM compiler, JIT Watch, Intel Amplifier. All of them provide the option to show the generated machine code.

> There's a reason DirectX12 is looked forward to. I don't want Windows reference counting my things any more unless I tell it to.

Better stop developing for Windows then.

The Universal Windows Application model is the continuation of WinRT, an evolution of COM.

In case you missed the note, DirectX 12 is still based on COM.

Also the new User Driver Model is based on COM.

Re:GC Tuning Confessions of a Performance Engineer by banachtarski on Saturday May 30, 2015 @01:17PM (#9630850)
Yea still COM based but part of what is being removed is a huge swath of underlying automatic reference counting for a huge number of things. Constant buffers resources etc.

Sure you can look at machine code generated from a virtualized language but I'd have a tough time understanding it in the context of the whole runtime, let alone being able to practically do anything about it.

Re:GC Tuning Confessions of a Performance Engineer by pjmlp on Saturday May 30, 2015 @02:10PM (#9631116)
> Sure you can look at machine code generated from a virtualized language but I'd have a tough time understanding it in the context of the whole runtime, let alone being able to practically do anything about it.

It is no different than using -S and check what happens for several code patterns.

Re:GC Tuning Confessions of a Performance Engineer by banachtarski on Saturday May 30, 2015 @02:54PM (#9631323)
Look man, I don't know what to tell you except that you view code fundamentally differently. My point is that yes, obviously everything gets turned into x64/x86 assembly at some point, and that yes, I'm sure seeing it is very easy. But if I need to prevent a GC pause from happening at some point in the frame my hands are tied. Debating this is like pulling teeth, and I'm inclined to say that if you can't follow at this point, just use your GC everywhere.
Re:GC Tuning Confessions of a Performance Engineer by pjmlp on Saturday May 30, 2015 @03:45PM (#9631515)
This whole thread started by my statement that I haven't seen large projects without errors doing manual memory management.

I doubt very seriously that anyone can write code in large teams where Valgrind will state there aren't double frees, bad frees, or dangling pointers happening.

Or a run with Coverity will state everything is nice and shinning.

Once upon a time I had to write a tool in a well known particle accelerator research institute to track down C++ memory errors in a multi-thread environment for cluster algorithms used in data analysis. As one common problem on that specific team was plugins bringing the cluster down due to memory corruption.

So I also do know one or two things about manual memory management.

And with what I know, I rather use automatic memory management, be it in the form of GC, RC, affine types or dependent types.

Re:GC Tuning Confessions of a Performance Engineer by banachtarski on Saturday May 30, 2015 @06:27PM (#9632007)
Dangling resources are common in any language. Ports don't get reclaimed, file descriptors are opened and never closed... the same principles apply.

Its funny since I actually started writing code for particle accelerator research as well, although it was a mixture of terrible C and F77. Physicists and mathematicians are good at their respective fields, but not necessarily coding sadly (I used to be both). Would the code have been better with a different language? Doubtful. They used literally zero established patterns and wrote everything with copy paste and with zero regard for what the machine actually did.

To each their own though. I'm glad your projects and line of work afford you the ability to work with a GC. Mine do not.

Re:GC Tuning Confessions of a Performance Engineer by duaneb on Friday May 29, 2015 @02:40PM (#9626470)
Manual memory management (i.e. not GC) is a little disingenuous because it implies that it's entirely manual. In fact, there are many patterns (e.g. RAII) which make "brain dead" manual memory management possible.

I'm not necessarily arguing this is a great way to go for a new team, but there are manual memory management strategies that can definitely scale. Among other things, it's not the only type of resource that needs managing, and GC only handles memory.

Re:GC Tuning Confessions of a Performance Engineer by banachtarski on Friday May 29, 2015 @03:14PM (#9626659)
Agreed. Sometimes, you don't even need RAII. You just let the stack unwind :)

But yes, RAII can make it pretty braindead (which is good)

Re:GC Tuning Confessions of a Performance Engineer by PopeOfNope on Saturday May 30, 2015 @06:08PM (#9631941)
Do you have any book suggestions for learning modern C++ memory management techniques? I'm beginning to think maybe it's time to fill that gap in my programming knowledge.
Re:GC Tuning Confessions of a Performance Engineer by banachtarski on Sunday May 31, 2015 @02:29AM (#9633292)
Hmm, modern C++ memory management is not entirely different from before. You have std::unique_ptr which is a game changer. I recommend just learning the language itself well. The C++ Primer (latest edition) plus Effective Modern C++ would be a good start. Also, read that doc floating around about everything a programmer needs to know about memory.
Re:GC Tuning Confessions of a Performance Engineer by TheLoneWolfling on Friday May 29, 2015 @04:32PM (#9627138)
Or arena allocation - which, by the way, is something I wish was more supported in mainstream languages
Re:GC Tuning Confessions of a Performance Engineer by bd_at_rivenhill on Friday May 29, 2015 @02:16PM (#9626328)
I think we will start to see less use of GCs in mobile going forward, especially now that Apple has deprecated GC in favor of automatic reference counting. The issues caused by the interaction between the memory hierarchy and the fragmentation that is naturally introduced by GC are difficult to measure, but nevertheless an important component of performance.
Re:GC Tuning Confessions of a Performance Engineer by banachtarski on Friday May 29, 2015 @07:26PM (#9628148)
Reference counting is still not the same as manual memory management, and there is a heavy cost to reference counting as well. Reference counting trades throughput for latency relative to a garbage collected runtime.
Re:GC Tuning Confessions of a Performance Engineer by pjmlp on Friday May 29, 2015 @02:24PM (#9626381)
Lets not forget they did that, because they failed to produce a stable working GC for Objective-C.
Re:GC Tuning Confessions of a Performance Engineer by Drdrdrq on Friday May 29, 2015 @03:20PM (#9626705)
Lets not forget Java failed to produce that too, they just dont seem to care. :)
Re:GC Tuning Confessions of a Performance Engineer by pjmlp on Friday May 29, 2015 @03:26PM (#9626748)
I didn't notice that JVM's GC do segfault all the time, with erratic behaviour depending on the libraries being linked.
Re:GC Tuning Confessions of a Performance Engineer by vitalyd on Friday May 29, 2015 @05:10PM (#9627469)
A performant GC for mobile is inherently a much harder problem than for servers.
Re:GC Tuning Confessions of a Performance Engineer by pron on Friday May 29, 2015 @12:15PM (#9625477)
I love GCs. They give you better memory throughput (in exchange for more footprint and higher latency, although latency can be made rather low), and they let you build and use very scalable concurrent data structures. On large machines with lots of cores and lots of RAM, they let you work with large, in-memory data sets very efficiently.
Re:GC Tuning Confessions of a Performance Engineer by banachtarski on Friday May 29, 2015 @12:34PM (#9625589)
You will never beat a tuned system without GC with a GC system because of all the instructions necessary to traverse references and such. Having lots of cores and lots of RAM just means you can eat the cost more easily. It just means you can afford to let the VM do the work instead of the programmer.

Also, no matter how much RAM you have, cache sizes are more or less the same, and cache line misses hurt.

Re:GC Tuning Confessions of a Performance Engineer by pron on Friday May 29, 2015 @12:42PM (#9625647)
> You will never beat a tuned system without GC with a GC system because of all the instructions necessary to traverse references and such.

That's not at all how it works. The generational hypothesis means that most objects die young. Allocating them is a simple, uncontended pointer bump in the thread-local allocation buffer (as fast as stack allocation), and freeing them is free, as they are never traversed. They are much, much (much) faster than malloc/free. They do however increase the frequency of young-gen so they have an indirect cost.

When it comes to throughput (i.e. total time the application spends doing memory management), modern GCs handily beat malloc/free. What you can do, however, with manual memory management is all sorts of arena allocations, but then you have to be careful when sharing pointers (Rust helps with that). Then there's the question of concurrent data structures, that are very, very hard to do well without a GC.

> cache line misses hurt.

What does that have to do with GCs? If anything, copying GCs bring related objects together, so the prefetcher can help. What affects cache misses (in Java) is the lack of arrays-of-structs, which is scheduled to be fixed, with the addition of value types, in Java 10.

Re:GC Tuning Confessions of a Performance Engineer by banachtarski on Friday May 29, 2015 @12:58PM (#9625752)
Comparing to malloc and free directly isn't very useful because that's pretty much the worst way to manage memory. Aside from memory arenas, you can just pool memory or preallocate too. I think concurrent data structures have very niche usage in practice (compared to say, a thread safe system).

>> cache line misses hurt.

> What does that have to do with GCs?

Everything. Sure you can use a copying GC and hope related objects go together. I tell the compiler to lump them together and exploit the prefetcher wherever possible.

Re:GC Tuning Confessions of a Performance Engineer by pron on Saturday May 30, 2015 @04:26AM (#9629476)
> I tell the compiler to lump them together and exploit the prefetcher wherever possible.

That's a memory layout issue. What does a GC have to do with that? .NET and Go already allow good memory layout control, and Java will, too, once it gets value types. Conversely, one could create a manually-managed language that doesn't support object/array embedding, either. Memory layout control and GCs are completely orthogonal issues.

Re:GC Tuning Confessions of a Performance Engineer by vitalyd on Friday May 29, 2015 @01:19PM (#9625898)
I really dislike the comparisons of TLAB to stack allocation. Stack, by its nature, is going to be hot in cache. TLAB, once filled up, will be retired and possibly assigned to a different thread. But even if it's not assigned, it's constantly moving forwards, and not revisiting the same space. You'd need prefetch to be perfect, and then on top of that, you'd need to make sure that by the time you go to allocate again, the prefetched cachelines have not been evicted.

Languages/platforms with a GC should still use, support, and encourage stack allocation for temporary memory -- this is your TLAB!

There is a cost to traversing references; card marking and generational collectors just reduce the amount of references you need to visit, but it doesn't mean reference chasing isn't requiring extra instructions. Finally, don't forget that card marking requires write barriers, which is extra instructions (and possible cache misses) on each reference store (modulo trivial ones, such as new allocations, where JIT knows it's not required).

Re:GC Tuning Confessions of a Performance Engineer by pron on Friday May 29, 2015 @03:02PM (#9626585)
While everything you say is 100% true, it is also a second-order effect, with a much lower magnitude than the primary GC performance behavior for short-lived objects.

Obviously stack allocation is preferable to TLAB allocation (and there's no reason to allocate objects with stack scope on the heap), if only for the fact that it never triggers a collection. Nevertheless, Java allocation/collection of short-lived object is much closer in cost to stack allocation than to malloc/free.

Re:GC Tuning Confessions of a Performance Engineer by vitalyd on Friday May 29, 2015 @05:07PM (#9627449)
I'm not sure memory locality effects can be considered secondary, unless their effects are completely dwarfed by something else the app is doing (e.g. there's no point in discussing this topic for i/o bound workloads).

I don't think malloc/free should enter this conversation because languages that use malloc/free do so very infrequently, and for the cases where dynamic memory needs to be allocated frequently, they use specialized memory managers within the application. This is also subject to which allocator is used and what the application's allocation pattern is. There're suboptimal GC mechanics as well in some cases, such as CMS tenured space using free chunk lists with no compaction, so any young GC that requires promotion can possibly increase the young GC time because the GC needs to find appropriate free block size, and if there's fragmentation, this may take a while. Point being is that malloc/free vs GC isn't quite as clear cut on its own, nevermind that malloc/free aren't called that often. Generally speaking, though, if you can give GC ample headroom in terms of RAM, it'll have better throughput than incessant malloc/free use (which, I argue, is rare in properly written applications).

Re:GC Tuning Confessions of a Performance Engineer by pron on Friday May 29, 2015 @06:57PM (#9628029)
> I'm not sure memory locality effects can be considered secondary

What is secondary isn't the TLAB/stack performance ratio, but that ratio vs malloc/TLAB.

Also, I'm not sure why you think locality matters much here in the case of stack reuse. Within each frame, the stores always come first, and those go in the store buffer (and the reads are from the store buffer, too), so those are pretty benign cache misses.

> which, I argue, is rare in properly written applications

Sure, it is rare in "well written applications", but how costly is it to write a well-written application in a large team, and how much extra performance can you get? Remember, we're not talking about a DSP, a microcontroller or a net router when we're discussing GCs, but big, complex applications. Nobody is claiming you can't beat a GC given enough work (though it's harder the more concurrency is involved).

Also, think about what kind of data we're talking about. The interesting data is database data, and that has both arbitrary lifetime as well as concurrent read/write. And if you don't use malloc/free, at best you need to write your own manual memory allocator which is just as complex, and at worst you basically need to write your own GC.

Re:GC Tuning Confessions of a Performance Engineer by vitalyd on Friday May 29, 2015 @07:35PM (#9628185)
Ok, we keep talking about malloc -- which malloc impl are you specifically referring to? There are many allocators out there these days, so let's be a bit more concrete. If not specific name, at least the class of allocator. Most of the common ones you'll find support thread-local allocation buffers, for starters.

>Sure, it is rare in "well written applications", but how costly is it to write a well-written application in a large team, and how much extra performance can you get?

>Also, think about what kind of data we're talking about. The interesting data is database data, and that has both arbitrary lifetime as well as concurrent read/write. And if you don't use malloc/free, at best you need to write your own manual memory allocator which is just as complex, and at worst you basically need to write your own GC.

If we're going to talk about databases, then "well-written" better be one of the top concerns, and team size should be irrelevant to that. And the more mechanically sympathetic of a product you're building (and db's are right up there in pretty much all aspects: cpu, i/o, net, mem, etc), the more you need to have control over those resources.

Have you, for example, looked at how postgresql manages memory? sqlite? redis? memcached? nginx? varnish? And, as I mentioned in the other reply, most of the big data java solutions end up rolling their own off-heap mem management infra using the same techniques as you'd use without GC.

Re:GC Tuning Confessions of a Performance Engineer by pron on Saturday May 30, 2015 @04:39AM (#9629497)
> Most of the common ones you'll find support thread-local allocation buffers, for starters.

And what about concurrent deallocation?

> Have you, for example, looked at how postgresql manages memory? sqlite? redis? memcached?

Not too well (basically lots and lots of locking, much of it is very coarse-grained). Our spatial in-memory Java database (SpaceBase) offers an order-of-magnitude better performance in concurrent usage (and much better scalability with core number). We do over 200K transactions (with lots of contention) per second on a single 4-core laptop without breaking a sweat (concurrently with the application itself), and over a million on a large server (with some careful tuning).

But even for less super-concurrent databases, C++ databases don't outperform Java ones. In this benchmark, the Java databases (H2 and HSQLDB) almost always outperform MySQL and Postgres: http://www.h2database.com/html/performance.html (and I don't even know how the Java solutions handle concurrency, whether they do locking, optimistic locking or a clever combination, like SpaceBase).

In both cases, the amount of effort put into the Java solutions is orders of magnitude less than the C/C++ solutions.

> nginx? varnish?

Those are (virtually) read-only use cases. That is very easy to do concurrently no matter how you manage concurrency. The trick is concurrent writes, not reads.

Re:GC Tuning Confessions of a Performance Engineer by anarazel on Sunday May 31, 2015 @04:36PM (#9635441)
> But even for less super-concurrent databases, C++ databases don't outperform Java ones. In this benchmark, the Java databases (H2 and HSQLDB) almost always outperform MySQL and Postgres: http://www.h2database.com/html/performance.html (and I don't even know how the Java solutions handle concurrency, whether they do locking, optimistic locking or a clever combination, like SpaceBase).

It's hardly surprising that a vendor's own benchmark shows it as winning against the competition.

Re:GC Tuning Confessions of a Performance Engineer by pron on Monday June 01, 2015 @10:50PM (#9643547)
Let's suppose for a second that the numbers are biased in the Java databases' favor. I doubt that other benchmarks would show such dramatically different results that would have those databases losing by much. So maybe they're not ahead as those number show (though I have little reason to doubt them), but a little behind. The difference would still not justify the claim that C++ provides superior performance for databases.

Also, it's an open source database with no commercial support by the authors, so I wouldn't really call them "vendors".

Re:GC Tuning Confessions of a Performance Engineer by mkpankov on Friday May 29, 2015 @02:42PM (#9626482)
A stupid question: what's TLAB?
Re:GC Tuning Confessions of a Performance Engineer by Jweb_Guru on Friday May 29, 2015 @04:05PM (#9626942)
Thread local allocation buffer.
Re:GC Tuning Confessions of a Performance Engineer by dumael on Friday May 29, 2015 @07:40PM (#9628206)
I have to completely disagree. Generation GC beats malloc/free in the case of 'all objects allocated on the heap' when: sizeof(survivors) + cost of traversing survivors pointer fields + rewriting the remembered set < traversing malloc/free trees for allocation/freeing.

Freeing objects in Generational GC requires computing the transitive closure relation of the stack and any "global variable" for the set of objects in the generational allocation arena so that all live objects there can be identified. I. E. For the stack, all "Global" variables, and the set of marked cards/(The record of objects updated with old to new references) : Find all live objects, copy them into the next arena while rewriting references.

Oh, were write barriers that not mentioned? Generation GC requires write barriers. Every update through a pointer unless provably required by a compiler turns "a->b = c"; into "if(b is in generational region) { record update of b;} a->b = c".

If you want to be able to move objects arounds cheaply, writes through pointers transform into small subroutines. For some GCs, reads through pointers are also small subroutines.

And some Generational GCs do card marking over object marking. Let's traverse $CHUNKOFMEMORY on the probabilistic notion that if something was updated, something close by was updated. (Otherwise we can have Sequential Store Buffers which record exactly which objects were changed)

Stack allocation (either explicit or deduced) is probably the fastest method of object allocation there is. Generational GC is on average going to be fast but can suffer horrendous worst case scenarios unless you GC is designed/engineered to switch between thread local allocation arenas.

Full disclosure: Despite my whining about GCs, I did do my Phd in them.

Re:GC Tuning Confessions of a Performance Engineer by pron on Saturday May 30, 2015 @12:58AM (#9629053)
You mean sizeof(survivors whose references changed since last collection). You pay for those write barriers for a reason.

> Stack allocation (either explicit or deduced) is probably the fastest method of object allocation there is. Generational GC is on average going to be fast but can suffer horrendous worst case scenarios unless you GC is designed/engineered to switch between thread local allocation arenas.

I agree, but on large servers with lots of RAM, the total amount of memory that can possibly be managed on stacks is < 3% of total RAM. What do you do then? Most of the RAM will be filled with database data, with arbitrary lifetime and concurrent access for both reads or writes, because that's precisely the kind of data that would most significantly help the program's performance if it's in RAM.

Now you can say that with all that data in RAM, the GC heap is a huge waste of RAM and the worst-case GC pause would be terrible, to which I say (I write in-memory databases in Java) that most of that data is user data and is kept off heap. Its lifetime is indeed arbitrary, but objects are deleted precisely when the user chooses to delete them, and that happens when they're protected by a lock. The much more interesting data is the indices, for which a GC helps greatly by allowing optimistic locking and other forms of scalable concurrency, and only those are kept on the heap.

Re:GC Tuning Confessions of a Performance Engineer by alkonaut on Friday May 29, 2015 @01:09PM (#9625820)
The pragmatic solution is to write the critical parts of the code either completely allocation-free or using some kind of automatic memory management. To write large amounts of boilerplate and non performance sensitive code using manual memory management is likely time that could be better spent elsewhere. Similarly, tweaking a compute intensive part of a CLR/Jvm/Python/ruby program to fix GC performance is probably more work than just rewriting that particular piece of code with allocations in mind, in C if necessary.
Re:GC Tuning Confessions of a Performance Engineer by banachtarski on Friday May 29, 2015 @01:25PM (#9625950)
The great thing about stack unwinding and constructor/destructor semantics is that they are automatic. Except they are done at compile time instead of runtime.

Write the performance critical system that needs to do the bookkeepping. Then write the easy stuff on top of that.

Re:GC Tuning Confessions of a Performance Engineer by duaneb on Friday May 29, 2015 @12:33PM (#9625577)
As with everything in engineering, this is a tradeoff. Not all access patterns work well with mark-and-sweep (or equivalent full traversal patterns). In particular, without needing to release or allocate memory, a GC pass wastes cycles. Furthermore, data structures must be compatible with the GC: traversal is proportional to the number of pointers (rooted or dangling) in the heap and stack at the time of running.

Generational garbage collection helps this, but for large numbers of pointers, you're going to see performance decrease unless at some point you move the pointers out of collection contention--think an allocation pool to reduce the depth of traversal early. I believe google just released a library to help with this, though the semantics are specific enough you could easily make your problems much worse.

GC is never a flat win.

Re:GC Tuning Confessions of a Performance Engineer by pron on Friday May 29, 2015 @12:44PM (#9625665)
> traversal is proportional to the number of pointers (rooted or dangling) in the heap and stack at the time of running.

That's not quite how it works. Traversal is proportional to the number of pointers changed since the last collection (HotSpot's GCs do card marking).

I do agree there are tradeoffs, but they're much more nuanced than that. There is certainly a footprint tradeoff, and there is a latency tradeoff (that can be mitigated).

Re:GC Tuning Confessions of a Performance Engineer by duaneb on Friday May 29, 2015 @04:11PM (#9626978)
> Traversal is proportional to the number of pointers changed since the last collection (HotSpot's GCs do card marking).

True. I was just trying to make the point that GC comes with complexity cost, and this should be considered—it's not "free" for an arbitrary algorithm.

Re:GC Tuning Confessions of a Performance Engineer by vitalyd on Friday May 29, 2015 @01:24PM (#9625937)
A card in the card table covers more than one object, so the entire dirty card needs to be scanned. Card marks only help with old->young references, and stack based roots still need to be visited each time, irrespective of what happened in the last collection.
Re:GC Tuning Confessions of a Performance Engineer by dumael on Friday May 29, 2015 @09:05PM (#9628427)
Except card marking (this region of the heap was changed) is only one way of flagging changes. It's entire possible to instead record the address of objects unconditionally and then flag them as they are visiting during a generation.
Re:GC Tuning Confessions of a Performance Engineer by vitalyd on Friday May 29, 2015 @09:49PM (#9628533)
You could but it's likely to perform worse for common case. Where are you going to record the addresses? How are you going to size that buffer? Card table is fixed size, and doesn't care whether you're using compressed pointers or full blown word sized ones. Cards also benefit from cache locality if recording objects in the same card. Marking a card clean is dirt cheap, and incremental cleaning is cheaper than removing entries individually from an exact buffer.

The downside to card marking is the impreciseness but with java being very reference heavy, I don't know how much that is a problem. Secondary problem is false sharing of memory when multiple mutators write in there; Hotspot, e.g. has a special flag to enable conditional marking, but it's not on by default.

Re:GC Tuning Confessions of a Performance Engineer by pron on Friday May 29, 2015 @04:52PM (#9627324)
That's why I said proportional.
Re:GC Tuning Confessions of a Performance Engineer by Dewie3 on Friday May 29, 2015 @12:56PM (#9625736)
Not to mention that you get to hire performance engineers.

People may have overestimated their utility (that they are good for less things then commonly thought). Maybe not to the degree that the stereotypical C/++ would believe, but still an overestimation.

Maybe we just need to use more abstractions in the middle between manual and fully automatic memory management, like region-based memory management (just an example).

Re:GC Tuning Confessions of a Performance Engineer by pcwalton on Friday May 29, 2015 @01:52PM (#9626172)
I think the solution is to use the right memory management tool for the job. pron is right that GC can be helpful for concurrent data structures (where the non-GC approach, hazard pointers, is basically just a GC), for instance. GC is also great when your objects have truly dynamic lifetime, such as open files in Unix from the kernel's point of view. But GC for all data tends to be overkill when your goal is to maximize performance: a pure number-crunching workload like a triangle rasterizer has no need for GC, since the memory management is so trivial that it's better for the programmer to manually specify it (and, ideally, have the compiler check that) than for the program to spend cycles figuring it out at runtime. So, in my view, the best memory management discipline is a flexible one that allows the choice between automatic and manual memory management on a case-by-case basis.
Re:GC Tuning Confessions of a Performance Engineer by pron on Friday May 29, 2015 @06:27PM (#9627879)
I think we should look at the problem the other way around: it is manual memory management that is overkill because there are exactly four kinds of object memory scope:

* stack scope * transaction scope (for some definition of transaction -- it can be, say, a frame in a game, or a request in a web server) * arbitrary (database or any shared data structure) * permanent

For the stack scope, we have the stack. For the arbitrary scope, a GC is invaluable. For the permanent scope, it doesn't matter too much whether you have a GC or not (yes, vitalyd is going to mention traversals, but if the traversals are interesting, they point to objects in the arbitrary cost anyway). This leaves us the transaction cost. Now, I think it is far easier to manage a stack scope in a GCed environment than an arbitrary scope in a manual environment, and, in fact, some GCs, such as HotSpot's G1 should (yes, vitalyd, it doesn't always work) figure out the tranaction boundary automatically, but even if they don't, it's fairly easy to get that functionality with object pooling. However, to get the absolute best we shouldn't add a GC to a manually managed environment, but add arena collections to a GCed environment, which is exactly what RTSJ (realtime Java) does with scoped memory (making sure there are no references from potentially longer scopes into the arena).

To sum up, for the absolute best performance, a GCed environment + arenas has all you need. There is absolutely no need for a per-object malloc/free, and reference counting is neither here nor there (contention, cycles).

Re:GC Tuning Confessions of a Performance Engineer by pcwalton on Friday May 29, 2015 @06:33PM (#9627912)
> yes, vitalyd is going to mention traversals, but if the traversals are interesting, they point to objects in the arbitrary cost anyway

I'm going to mention this too. I don't understand what you mean by "they point to objects in the arbitrary cost anyway". The fact of the matter is that you have to trace all objects at some point.

> Now, I think it is far easier to manage a stack scope in a GCed environment than an arbitrary scope in a manual environment, and, in fact, some GCs, such as HotSpot's G1 should (yes, vitalyd, it doesn't always work) figure out the tranaction boundary automatically, but even if they don't, it's fairly easy to get that functionality with object pooling.

Object pooling is just a (limited, error-prone, poorly-performing) form of manual memory management.

> reference counting is neither here nor there (contention, cycles).

Contention isn't a problem if you don't touch the reference counts much. Cycles aren't a problem if you don't have cycles.

Re:GC Tuning Confessions of a Performance Engineer by pron on Saturday May 30, 2015 @01:47PM (#9630991)
> I'm going to mention this too.

What I mean is that either those refs are permanent -- in which case they won't be traced -- or arbitrary -- in which case it's really more of an "arbitrary" problem than a "permanent" one.

> Object pooling is just a (limited, error-prone, poorly-performing) form of manual memory management.

... And reference counting is just a (limited, error-prone, poorly-performing) form of garbage collection.

> Contention isn't a problem if you don't touch the reference counts much. Cycles aren't a problem if you don't have cycles.

But now you've added restrictions that are much more onerous than those of object-pools.

In any case, my main question is this: if absolute top performance is what you need, why not have a GC + arenas (like RTSJ) without ref counting/manual management? In my opinion, there's only one downside to that approach is RAM footprint. If that's your answer, then I agree (I think that that's pretty much the main reson not to use a GC).

Re:GC Tuning Confessions of a Performance Engineer by vitalyd on Friday May 29, 2015 @07:23PM (#9628140)
>I'm going to mention this too. I don't understand what you mean by "they point to objects in the arbitrary cost anyway". The fact of the matter is that you have to trace all objects at some point.

I think he means that if you have object references, then those references are interesting for the application itself, and not just for the GC tracer. However, I don't buy this statement simply because, even if you elect to store things as references rather than interior allocs, your app will only pointer chase the things it cares about, whereas the GC tracer may chase pointers that your app wouldn't otherwise.

@Ron:

The "arbitrary" scope better not be really arbitrary since even GCs are typically tuned for the generational hypothesis: your objects better be either short lived or long lived, anything in between is likely to degrade GC performance. In fact, if you like at some of the big data java solutions, once they reach a certain heap size, GC starts killing them, and they end up building their own semi-hybrid solution of moving stuff off-heap, and then managing that memory themselves (using the exact same block/slab/arena allocation techniques being downplayed here!).

For things like databases where object lifetime is in the hands of the user, arena/slab allocators work just fine. The additional advantage here is that you rarely need to destroy just a single object somewhere, typically it's an entire blob of related stuff. Arena destruction is more efficient here than GC because it's it inherently has more context than GC (the engineer wrote it, afterall). Is it more challenging/harder to implement than simply punting to the GC? Probably yes. But if you run afoul of GC's ergonomics and/or need to start tuning the finer points of GC behavior (i.e. see above comment about most of the big data java projects hitting this wall), the time spent there may end up exceeding arena impl costs.

Re:GC Tuning Confessions of a Performance Engineer by pron on Saturday May 30, 2015 @12:46AM (#9629015)
> I think he means that if you have object references, then those references are interesting for the application itself, and not just for the GC tracer.

What I mean is that either those refs are permanent -- in which case they won't be traced -- or arbitrary -- in which case it's really more of an "arbitrary" problem than a "permanent" one.

> The "arbitrary" scope better not be really arbitrary since even GCs are typically tuned for the generational hypothesis

By arbitrary I mean long-lived (old-gen) but not permanent. In short: database data.

> For things like databases where object lifetime is in the hands of the user, arena/slab allocators work just fine.

What you are saying is that you never truly have arbitrary-lifetime objects, and I strongly disagree with that.

> the time spent there may end up exceeding arena impl costs.

Even if you're right about arenas (which I don't agree with), you believe that a runtime designed to target the vast majority of server software should force developers to figure out lifetimes and that that may be beneficial to them? I mean, I even mentioned arena just to get that much closer to 100% performance. Even without them Java gets to at least 90%. Are you suggesting that the vast majority of projects in the world are interested in doing more work that is likely error prone just to get that 10% at best?

Re:GC Tuning Confessions of a Performance Engineer by Manishearth on Friday May 29, 2015 @02:17PM (#9626333)
+1. This is why I'm so excited about GC libs in Rust (and am working on a GC myself with @mystor). Rust has a nice undertone about choosing your costs and your guarantees, and if it got a good GC* people would have a really complete ecosystem for memory management.

*so, not the one I'm working on :P

Re:GC Tuning Confessions of a Performance Engineer by vitalyd on Friday May 29, 2015 @01:59PM (#9626233)
GC does help with ABA style concurrent problems, and it also simplifies the places where you simply don't care (slow/uncommon paths). But concurrent data structures are a special case, and likely, not always the best anyway since even though they scale much better than locks, with enough writers and cores, the cache coherence traffic is going to swamp the application.

I do agree with the flexibility comment. A hybrid environment where you can pick and choose the mem mgmt strategy (and, very important, not pay in perf for things you're not using) would be great.

Re:GC Tuning Confessions of a Performance Engineer by pron on Saturday May 30, 2015 @12:50AM (#9629034)
> GC does help with ABA style concurrent problems

I wasn't just referring to that. GC helps with pretty much any concurrent data structure, lock-free or not. For example, read-write locks don't scale too well (certainly no beyond the core count we're quickly coming up against on servers), and GC enables easy optimistic locking.

Re:GC Tuning Confessions of a Performance Engineer by Dewie3 on Wednesday June 03, 2015 @11:16AM (#9653172)
> I think the solution is to use the right memory management tool for the job.

That's like a generalization of my own argument. Generalized to the point where we are in the domain of truisms like "right tool for the job" (as opposed to what, the wrong tool for the job?).