News for hackers. Stuff that matters.
Re:For Better Computing, Liberate CPUs from Garbage Collection by norswap on Thursday May 09, 2019 @09:36AM (#19868130)
Ridiculous.

The problem is not that you don't know when/where the lifetime will end — that can usually be characterized by a terse "English" description. The problem is that this lifetime is dynamic in nature.

The end of the lifetime of an object may coincide with some user input, for instance.

At this point, either you go back to manual management, with the potential for errors (and for what it's worth, I think manual management is fine in many cases) ... or .... or what, exactly?

Even Rust doesn't solve that problem at all. If your objects can survive outside of a "linear" execution, which is the case in interactive or multi-threaded programs, you're falling back on reference counting — garbage collection.

No one has ever devised a scheme that lets you specify very dynamic lifetimes AND statically checks that no leak can occur.

I'd stake a lot on that being impossible in general, and the best we can hope for being an AI that searches a way to insert the manual memory management calls (mostly, "free" and a minimal set of reference counts) assorted with a proof that this change will not cause any leaks or invalid accesses. We're a long way off that yet.

Re:For Better Computing, Liberate CPUs from Garbage Collection by simias on Thursday May 09, 2019 @10:20AM (#19868506)
I don't see your point. Of course sometimes the lifetime of an object is not tied to code scope but actually to something dynamic. Let's say for instance when you close a tab in your browser you expect the resources to be freed (ignoring caching to simplify the argument).

Clearly somewhere in your code you have to explicitly handle tab closing and break the references to allow the GC to do its job. Why not free the resources here while you're at it?

When you say "manual memory management" if you're thinking C-style malloc-free then you have a point, it's very easy to forget a free() somewhere. But any language with destructors can handle these situations without much more overhead than GC-based approaches. Just remove your object from whatever data structure was holding it and let the language automagically run the destructor for you. I find RAII a lot easier to model and think about than "you drop this reference and one day maybe your object is destroyed, but don't really rely on that".

>No one has ever devised a scheme that lets you specify very dynamic lifetimes AND statically checks that no leak can occur.

GC doesn't solve that either. If you're not careful and let a reference to old data dangle somewhere in your code you can effectively "leak" data just fine. When there's no clear ownership and nobody knows who's supposed to clean what it's very easy to end up with data dangling everywhere through complex ownership graphs. GC may mitigate some use-after-free problems (generally by masking the problem) but that is something that can be solved without GC either.

I'm not saying that applications with (justifiably) extremely complicated ownership semantics that also require very high performance don't potentially benefit from using a GC and that it could outperform manual memory management by, for instance, batching the object deletions. What I'm saying is that it's that such applications are few and far in-between, basically 100% of all the code I've ever wrote in my ~15years as a professional software developer didn't have such requirements.

GCs should be an opt-in niche tool used to solve specific problems, not a kludge to let developers write sloppy code and get away with it.

Re:For Better Computing, Liberate CPUs from Garbage Collection by kazinator on Thursday May 09, 2019 @01:33PM (#19870277)
> Why not free the resources here while you're at it?

Because you do not have an exclusive reference to all of them; we could be freeing something that is still in use somewhere.

So why not reference-count? Because reference counting is slow, and must be meticulously maintained (here, languages help with constructs like smart pointers), and doesn't handle cycles in the object structure.

> I find RAII a lot easier to model and think about than "you drop this reference and one day maybe your object is destroyed, but don't really rely on that".

The RAII model doesn't do away with this "maybe" in any shape or form. We drop this reference, and the smart pointer decrements the refcount; maybe it has hit zero so the object is destroyed, maybe not. Maybe the zero refcount triggers a whole cascade of thousands of other objects hitting their zero refcount, which takes many cycles to execute, or maybe it doesn't.

Re:For Better Computing, Liberate CPUs from Garbage Collection by Shikadi on Thursday May 09, 2019 @03:16PM (#19871291)
RAII has nothing to do with reference counting, it stands for resource acquisition is initialization. In C++ that means the lifetime of a resource is tied to the creation and destruction of an object. Often when people talk about RAII and C++ they are also referring to destructors being called when an object goes out of scope, so if an object that contains an object containing some allocated resource goes out of scope, the resource is released as the destructor path is called.

If an object with a smart pointer goes out of scope, then of course there's the overhead of the smart pointer, but the previous comment wasn't referring to that situation

Re:For Better Computing, Liberate CPUs from Garbage Collection by kazinator on Thursday May 09, 2019 @03:47PM (#19871626)
RAII is commonly used for representing smart pointers that manage references to objects that themselves have otherwise pervasive lifetimes.

RAII not managing any shared resources is trivial and uninteresting. The lifecycle of an object that is confined to a single lexical scope can be handled manually and verified by visual inspection (though of course it is more reliable to automate that with RAII or macros, not to mention less verbose).

Re:For Better Computing, Liberate CPUs from Garbage Collection by tomp on Thursday May 09, 2019 @12:52PM (#19869892)
> Let's say for instance when you close a tab in your browser you expect the resources to be freed

That's basically region-based memory management. Which is a great idea, but unfortunately only applicable in narrow scenarios (another example besides browser tabs is user requests in a HTTP server).

The real issue, however, is what if you can't keep all current task's data in RAM! E.g. browser tabs still have GC for JavaScript... Going a bit meta, you can easily think of a hierarch of such "regions" (e.g. computer > process > browser tab), each is self-contained in the sense that when you turn it off/terminate it/close it all its resources are reclaimed, but the real issue is when they need more resources than you have... Then you're back at needing GC.

I agree with your second point; GC doesn't solve the problem of reference leaks... Whereas regions mostly do! So maybe more language support for regions (finer-grained than "whole process" - think Erlang) would be useful (each region can still of course optionally have its own GC).

Re:For Better Computing, Liberate CPUs from Garbage Collection by lossolo on Thursday May 09, 2019 @10:59AM (#19868877)
> Clearly somewhere in your code you have to explicitly handle tab closing and break the references to allow the GC to do its job. Why not free the resources here while you're at it?

You are missing the point. The main point behind GC is removing the complexity of writing the software. Writing your own destructors, thinking about when to free your memory or writing lifetime annotations, needing to design your app in specific way to satisfy the borrow checker etc in languages like Ada/C/C++/Rust all are additional complexity and mental overhead that is removed by GC. Your point about "just do this X and you don't need to use GC" is a reason people use GC languages, because they don't want to think about or write this "X".

Re:For Better Computing, Liberate CPUs from Garbage Collection by simias on Thursday May 09, 2019 @11:40AM (#19869234)
My point is that you need a "destructor" even with a GC. Continuing with my example you need to pop your tab from the data structure containing your tabs and you have to make sure that all references are dropped so that the GC will do its work.

When I write Rust code I basically never have to explicitly free anything outside of FFI code dealing with C pointer or the like. The most straightforward way to allocate anything in Rust is to reify a Box which frees its contents when it's dropped. The data is freed when I remove it from containers or when a Box gets dropped. The closest I get to garbage collection is using a reference-counted pointer here and there (and only after careful consideration).

I really don't see what complexity the GC removes in this situation, besides letting you write code without having to care about ownership which, as I stated earlier, I consider to be an antipattern.

>"just do this X and you don't need to use GC" is a reason people use GC languages, because they don't want to think about or write this "X"

A lazy programmer is a good programmer but it's also important to recognize when something can't be pushed under the rug. I don't want to think about or write documentation and unit tests either, yet here we are. If I had to maintain an application and I ask the coder "when exactly are these objects deleted?" and they answer "dunno lol, I let the GC figure it out" I'd be extremely worried.

Re:For Better Computing, Liberate CPUs from Garbage Collection by kbenson on Thursday May 09, 2019 @12:10PM (#19869508)
> it's also important to recognize when something can't be pushed under the rug.

If you haven't heard of it, you might be interested in the Waterbed Theory of Complexity[1]. The idea behind it is that it's not that you can't push down complexity in an area, but often that complexity just pops up in a different area. For example, you can do away with the vast majority of allocating and freeing memory, but the complexity of doing that pops up again when you start having to tune your GC or use very special patterns to avoid/minimize GC freezes.

That's not to say there's not a benefit to that sometimes. If you can push the complexity away from the majority of use cases to a few special use cases, that might be a net win. It doesn't always feel like that though if you're the person stuck trying to coax that last few percent of performance optimizations out of a runtime that only gives you so many levers and dials to work with.

I think some of the same concerns that make a dynamic/scripting language a poor choice compared toa compiled and static one are the same things that might make a GC language a poor choice compared to a manually memory managed one, and it's just a matter of scale that separates them. Is performance important? A scripting language might be a poor choice. Is performance really important? Then a GC language might be a poor choice. On the other hand, if I'm writing some program to run correctly once or twice ever, and it might actually take more tome to write than it will ever run for (e.g. a script to parse and/or transform data on disk for me to fix a problem), then is anything really gained from manual memory management? Writing that in Perl or Bash is a valid option there, and that's about as far away from memory management as you can get. For portions of my career, I've spent half or more of my time writing programs like that to help with sysadmin tasks.

1: https://en.wikipedia.org/wiki/Waterbed_theory

Re:For Better Computing, Liberate CPUs from Garbage Collection by kazinator on Thursday May 09, 2019 @01:48PM (#19870445)
The Waterbed theory is stupid, because a given requirement can be handled in one place in the system, or it can proliferate into every module, giving rise to a more complexity to achieve the same functionality.

So that is to say, when we push down a "bump" of complexity in one place, seventeen bumps at least as large can crop up elsewhere. Moreover, that seventeen is just today; we might have paved the way for more such bumps now being necessary going forward as functionality is added, whereas if we kept the original bump, it would stay the same. Water is not a good model for complexity because it is incompressible for all practical purposes; if we push down a liter of water here, the places where it pops up add up to just a liter.

For exmaple, virtual memory is considerably simpler than individual modules of a program implementing a scheme for swapping code and data to disk and back.

Re:For Better Computing, Liberate CPUs from Garbage Collection by kbenson on Thursday May 09, 2019 @02:40PM (#19870922)
> when we push down a "bump" of complexity in one place, seventeen bumps at least as large can crop up elsewhere.

I think you've taken the metaphor about a waterbed and design advice a bit too literally. :)

When you push complexity down in one area and it pops up in another, there's no guarantee that it's of the same overall magnitude, as it would be in a real waterbed. Sometimes it's larger, and sometimes it's smaller.

The same idea applies to whether we should all write assembly or use a higher level language to accomplish our computing needs. We push down the complexity of knowing exactly what the processor is doing at any one moment for a easier to manage overall view of what we want to accomplish. Overall, the net effect is a lessening of complexity for most people, but for those that want or need to know exactly what is going on, there more complexity. Still, overall I think the net effect is less complexity for most people, vastly outweighing the time and effort a few put into figuring out why something isn't as expected.

On the other end of the spectrum, using a dynamically typed scripting language might save people a lot of time initially, or when prototyping, or on smaller programs, but as those programs become larger, the lack of constraints (which provide a small but immediate complexity hurdle) can build up and overwhelm a large project that might otherwise had an easier time if some more constraints were required along the way.

No one tool or paradigm is perfect for every task, and we should not expect one to be. We don't usually commute in dump trucks, and we don't usually haul lots of debris in compact cars. I see no reason why it should be any different for programming languages. Sometimes all you need is a 10 line bash script, and given that information, I doubt there's a huge level of complexity that's going to rear its head later because I didn't write it in some lower level language.

Re:For Better Computing, Liberate CPUs from Garbage Collection by kazinator on Thursday May 09, 2019 @03:57PM (#19871751)
The waterbed theory in fact explicitly states that the amount of complexity is preserved no matter how it is distributed. The Wikipedia definition of it explicitly talks about how the volume of water remains constant due to its incompressibility.

If that isn't taken literally, it informs of nothing we don't already suspect. It tells us that for each functional requirement, there is at least one piece of code in the system somewhere. If we remove these lines of code from one place, or places, something has to be added elsewhere (possibly way more lines, or way fewer) so that the requirement remains implemented.

Re:For Better Computing, Liberate CPUs from Garbage Collection by kbenson on Thursday May 09, 2019 @05:07PM (#19872434)
> If the waterbed metaphor isn't taken literally it informs of nothing we don't already suspect.

It informs you of nothing you didn't already suspect. I think it's useful to remind people of something that's fairly obvious ans self explanatory if you think about it, but not everyone does, and having an easy and slightly odd metaphor to point to helps the point to get across. The fact that you think it's obvious is actually one of its major benefits. You can immediately start exploring the consequences of it instead of spending time proving it. Honestly, of all the criticisms to level at a metaphor, "too obvious" isn't one I would consider worth bringing up. Obvious metaphors are used to good effect all the time.

Edit to address your prefixed paragraph:

> The waterbed theory in fact explicitly states that the amount of complexity is preserved no matter how it is distributed.

If you read carefully, it says that the minimum level of complexity may not be reduced. Who is to say the minimum level of complexity has already been reached? Or that pushing down a level above the minumum in some area won't cause only the minimum level to rise elsewhere (or vice-versa)?

Re:For Better Computing, Liberate CPUs from Garbage Collection by arcticbull on Thursday May 09, 2019 @01:40PM (#19870361)
I like the waterbed theory, thanks, I hadn't heard of it before.

You're absolutely right, in the GC context you:

(1) Have to manually reference count / manage external resources like sockets and files because you can't count on the destructor to ever run.

(2) Lose determinism and then have to spend huge amounts of effort tweaking GC parameters to fix your issue, but to your point, it becomes a Jenga tower where you touch one part and the whole thing falls down again.

(3) Can easily still leak huge portions of your object graph by, for instance, throwing them into long-lived dictionaries.

Re:For Better Computing, Liberate CPUs from Garbage Collection by kazinator on Thursday May 09, 2019 @01:52PM (#19870470)
In what memory management paradigm can you free the object in a long-lived dictionary without removing it from that dictionary, and do you advocate this paradigm?
Re:For Better Computing, Liberate CPUs from Garbage Collection by JudgeWapner on Thursday May 09, 2019 @01:26PM (#19870215)
> mental overhead that is removed by GC

and it is replaced by GC when your program behaves unexpectedly because, whoopsie, that's the one time the GC decided to run. Good luck debugging or even reproducing that.

Re:For Better Computing, Liberate CPUs from Garbage Collection by ernst_klim on Thursday May 09, 2019 @05:53PM (#19872805)
>that's the one time the GC decided to run. Good luck debugging or even reproducing that.

GC usually runs on allocations, so it's quite predictable. The amount of time spent on collection is pretty much as indeterminate as the time spent on malloc/free.

Re:For Better Computing, Liberate CPUs from Garbage Collection by fouric on Thursday May 09, 2019 @09:06PM (#19874159)
When will a correctly-implemented GC ever alter your program's functional correctness, aside from timing-related bugs that aren't fixed by manual memory management either?
Re:For Better Computing, Liberate CPUs from Garbage Collection by farisjarrah on Thursday May 09, 2019 @01:41PM (#19870377)
Just run your program normally and use eBPF/bpftrace with something like a flamegraph to track your system and its GC. This isn't an easy problem by any stretch of the imagination, however, it is a solved problem.
Re:For Better Computing, Liberate CPUs from Garbage Collection by naasking on Thursday May 09, 2019 @02:22PM (#19870764)
> The main point behind GC is removing the complexity of writing the software

And more importantly, reading it.

Re:For Better Computing, Liberate CPUs from Garbage Collection by irq11 on Thursday May 09, 2019 @11:12AM (#19868982)
Having spent years writing code in both GC’ed and non-GC’ed languages, it’s pretty clear that it programmers spend about the same amount of time worrying about memory management with either paradigm.

The main differences are that people using GC systems spend their time doing magical incantations to manage the GC wizard, and they’re a lot more smug about the goodness of their system.

Re:For Better Computing, Liberate CPUs from Garbage Collection by cblades on Thursday May 09, 2019 @11:37AM (#19869213)
>programmers spend about the same amount of time worrying about memory management with either paradigm

I have no idea how you've come to that conclusion. For the vast majority of memory allocation in any GC'd language I can think of, you spend zero time thinking about memory management 98% of the time.

whereas in a manual language, you must consider it every time you allocate memory. That's not a bad thing, it's often dead simple in the vast majority of contexts, but still.

I've also very rarely seen code in professional code that's mean to poke or prod the GC into running.

Re:For Better Computing, Liberate CPUs from Garbage Collection by simias on Thursday May 09, 2019 @12:00PM (#19869412)
>For the vast majority of memory allocation in any GC'd language I can think of, you spend zero time thinking about memory management 98% of the time.

>whereas in a manual language, you must consider it every time you allocate memory.

This is like bizarro world to me. I genuinely can't comprehend that.

Here's how memory management looks like to me, using concrete examples in languages featuring RAII like C++ or Rust:

- I have a function that needs to compute a hash so I need to allocate a hash context for it. If it's small I'll put it on the stack, otherwise it'll go on the heap. In either case when the function returns the destructor is automatically called.

- I have a function that takes the name of a file as parameter and returns its contents in a buffer. Clearly the buffer needs to be allocated on the heap, so I allocate a large enough vector or string read into it and return that. The caller will then either use it and drop it immediately or store it somewhere to be dropped later, either on its own or as part of the structure it belongs to. In either case the destructor will be called automatically and the memory will be freed.

That's easily 99.9% of what memory management looks like in the programs that I write. I have absolutely no idea what using a GC would change at any point here. Note that while this is technically "manual" memory management I never have to explicitly free anything and in the vast majority of cases I don't even have to bother implementing the destructor myself (I basically only need to implement them if I need to release external resources like, for instance, raw OpenGL handles, file descriptors and the like. The GC wouldn't help either here).

It's genuinely a non-issue as far as I'm concerned. I literally never think "uh, I have no idea when this object won't be used anymore, I wish I had a GC to figure it out". I can't even imagine when such a scenario would crop up.

Re:For Better Computing, Liberate CPUs from Garbage Collection by jhasse on Thursday May 09, 2019 @01:03PM (#19870006)
Here's you're example with GC:

- I have a function that needs to compute a hash so I need to allocate a hash context for it. At some point after the function has returned the destructor is automatically called.

- I have a function that takes the name of a file as parameter and returns its contents in a buffer. I allocate a large enough vector or string read into it and return that. The caller will then either use it or store it somewhere. In either case the destructor will be called automatically and the memory will be freed.

See how it's simpler?

Re:For Better Computing, Liberate CPUs from Garbage Collection by cblades on Thursday May 09, 2019 @12:04PM (#19869459)
>he caller will then either use it and drop it immediately or store it somewhere to be dropped later, either on its own or as part of the structure it belongs to.

You're glossing over a lot of complexity there. That's the entire point of GC, you don't have to think about when it's dropped.

Also, I wasn't arguing for the blanket necessity of GC in all contexts and for all problems.

Re:For Better Computing, Liberate CPUs from Garbage Collection by simias on Thursday May 09, 2019 @12:46PM (#19869836)
Can you point out a specific example then? I'm genuinely not trying to play dumb. I very honestly not see that complexity that should be obvious to me given that I spend most of my time writing code in non-GC languages.

Maybe it's just Stockholm syndrome and I'm so used to working that way that I don't even see the problem anymore but there are now a long string of replies talking abstractly about the overhead of programming in a non-GC environment and I simply don't relate at all. It's a complete non-issue as far as I'm concerned.

Re:For Better Computing, Liberate CPUs from Garbage Collection by cblades on Thursday May 09, 2019 @01:04PM (#19870025)
>store it somewhere to be dropped later, either on its own or as part of the structure it belongs to.

Wherever you're storing the reference, you have to eventually free the memory. That propagates memory-management code throughout your codebase, and is error-prone.

I totally agree that if you're allocating on the stack it's a non-issue. If you're returning a reference and always freeing in the caller, that's pretty easy too, but you have to have that logic in every single caller. If you're storing a reference on the heap somewhere, you suddenly have dynamic lifetimes with indeterminate reference counts, and it gets quite hard to 1) correctly manage memory and 2) verify that you're correctly managing memory.

The alternative is that in all three instances in a GC language, I don't have to care about memory management. Barring unusual edge cases, I can't forget to deallocate something and end up with a memory leak.

Re:For Better Computing, Liberate CPUs from Garbage Collection by irq11 on Thursday May 09, 2019 @04:14PM (#19871940)
”If you're storing a reference on the heap somewhere, you suddenly have dynamic lifetimes with indeterminate reference counts, and it gets quite hard to 1) correctly manage memory and 2) verify that you're correctly managing memory.”

It is axiomatic that GC programs leak memory. I’ve never encountered one that didn’t. The difference is that it’s harder to see, and most programmers who have grown up with GC systems don’t know how to fix it.

”The alternative is that in all three instances in a GC language, I don't have to care about memory management.”

Again, no. The alternative is thar you aren’t paying attention, and don’t see the leaks until they become fatal. Just like cockroaches, you have them, somewhere.

Re:For Better Computing, Liberate CPUs from Garbage Collection by dleslie on Thursday May 09, 2019 @11:51AM (#19869334)
For 95% of the code you write it's not a concern; that last 5% sure can eat up time if your software has any concern about performance.
Re:For Better Computing, Liberate CPUs from Garbage Collection by cblades on Thursday May 09, 2019 @12:03PM (#19869442)
Totally agree. I don't think that tradeoff is particularly out-balanced by the time it takes to do manual memory management.
Re:For Better Computing, Liberate CPUs from Garbage Collection by irq11 on Thursday May 09, 2019 @04:09PM (#19871879)
”I have no idea how you've come to that conclusion.”

30 years of experience.

”For the vast majority of memory allocation in any GC'd language I can think of, you spend zero time thinking about memory management 98% of the time.”

No, you think about 98% of the code 0% of the time. The remaining 2% ends up causing so many problems that your amortized time spent on memory management is the same.

Re:For Better Computing, Liberate CPUs from Garbage Collection by c-smile on Thursday May 09, 2019 @12:09PM (#19869500)
> GCs should be an opt-in niche tool used to solve specific problems.

That is true. Sciter (https://sciter.com) contains implementation of DOM(tree), CSS and script.

DOM is a regular tree structure - each element of the tree has strictly one parent and no cycles on the tree in principle. It does not need GC at all and is not using it for DOM tree management.

CSS is a collection of collections of name/value pairs. No loops. It does not need GC at all.

And only script uses GC for the simple reason: in UI, ownership graph of script objects frequently contains loops. Yet lifespan of objects is unknown at compile time - depends on user interactions.

Even purely native GUI application benefits from GC when that GC is used in places where it is needed. GC is just one way (of many) of doing memory management.

Ideally it should be a multi-paradigm programming language and environment that allows to use as explicit memory management (and so RAII, memory pools, etc) as a GC.

    int *ptr = …;
    gcable int* gptr = …;
 
But unfortunately such dualism is hard to achieve.
Re:For Better Computing, Liberate CPUs from Garbage Collection by skrimp on Friday May 10, 2019 @06:02PM (#19882682)
This is very hard to achieve because GC + RAII do not play nice together. It's hard to reason about whether an RAII resource has been cleaned up since managed objects have non-deterministic destruction. This non-determinism is unsuitable for common RAII patterns like scoped mutex locks. Now, the question being begged is "Can this be avoided by not using destructors with managed objects?" Yes, but not reasonably. Any unmanaged objects that are owned in the entire managed object graph now also have non-deterministic destruction. Thus, the only way to reasonably follow this constraint is to have the compiler disallow/warn when an object with a non-trivial destructor (explicit or implicit) is used with GC. If that wasn't enough of a pain, any runtime-polymorphic types may also need to be destructed and these can't be known at compile time if dynamic linking is used. In essence, these concepts are incompatible to a point where any language supporting both will likely make extensive trade-offs.

I find this really sad because many developers may never be exposed to how elegant RAII can be if they are never exposed to non-memory-managed languages. Sure it can be approximated using `with` constructs, but those don't allow resource allocations to cross scope boundaries.

Re:For Better Computing, Liberate CPUs from Garbage Collection by kazinator on Thursday May 09, 2019 @01:35PM (#19870306)
Rather, GC should be default, and you opt-out of GC for specific optimization problems.
Re:For Better Computing, Liberate CPUs from Garbage Collection by rayiner on Thursday May 09, 2019 @12:40PM (#19869778)
> When you say "manual memory management" if you're thinking C-style malloc-free then you have a point, it's very easy to forget a free() somewhere. But any language with destructors can handle these situations without much more overhead than GC-based approaches.

What happens when that object is shared and still referenced from somewhere else? Now you have a use after free error waiting to happen.

Re:For Better Computing, Liberate CPUs from Garbage Collection by Shikadi on Thursday May 09, 2019 @03:19PM (#19871319)
That's what smart pointers/ref counting is for
Re:For Better Computing, Liberate CPUs from Garbage Collection by tptacek on Thursday May 09, 2019 @03:52PM (#19871684)
C++ is the flagship language for smart pointers and UAF memory corruption flaws are endemic to it.
Re:For Better Computing, Liberate CPUs from Garbage Collection by rayiner on Thursday May 09, 2019 @05:52PM (#19872801)
Re:For Better Computing, Liberate CPUs from Garbage Collection by tjoff on Saturday May 11, 2019 @03:18AM (#19885030)
Doesn't seem related though?

Unless you want to pick on the specific implementation?

Re:For Better Computing, Liberate CPUs from Garbage Collection by UncleMeat on Friday May 10, 2019 @07:02PM (#19883092)
That's literally GC.
Re:For Better Computing, Liberate CPUs from Garbage Collection by norswap on Thursday May 09, 2019 @12:07PM (#19869488)
I mostly agree with your point on RAII.

But I don't think OP was advocating RAII.

The typical argument for GC is that it is less error-prone than even RAII. And I agree with that point. It's a trade-off on which you have to decide on. I think the tiny perf penalty is often worth it.

You can still retain stale data under a GC, but real leaks (unreachable allocated memory) are precluded — which is usually considered (not unreasonably) to be the more common and serious problem.

I think GC is a sane default because it's less error prone, and god knows the average developer will make enough errors as it is. I'd trust myself with manual memory management, but not a random group of developers I don't know. What about you?

Re:For Better Computing, Liberate CPUs from Garbage Collection by nine_k on Thursday May 09, 2019 @09:56PM (#19874395)
You can of course introduce a space leak in a honest GC environment. Make a linked list, and keep adding items to it while keeping its head referenced. Of course you do it by mistake: you kept two references, the main and the auxiliary, and the main reference is duly gone.

This is why weak references were invented.

Re:For Better Computing, Liberate CPUs from Garbage Collection by SilasX on Thursday May 09, 2019 @02:23PM (#19870777)
>I find RAII a lot easier to model and think about than "you drop this reference and one day maybe your object is destroyed, but don't really rely on that"

Heh, I just realized this is what we depend on to remove commits from Github. "Uh, just delete any branches that contain it, and, uh, it'll get removed ... like, eventually." (of course you have to assume any secret data is compromised)

Re:For Better Computing, Liberate CPUs from Garbage Collection by Arelius on Thursday May 09, 2019 @02:34PM (#19870864)
This is a fundamental git model. see `git help gc`
Re:For Better Computing, Liberate CPUs from Garbage Collection by SilasX on Thursday May 09, 2019 @02:36PM (#19870890)
I know I can clean up locally, I meant deleting the commits on the github server/repo.
Re:For Better Computing, Liberate CPUs from Garbage Collection by dleslie on Thursday May 09, 2019 @11:47AM (#19869295)
If the lifetime can be expressed with a state machine then it needn't be garbage collected so long as the language is sufficiently expressive.

Reference counting isn't garbage collection, as most people consider it. There is no need to sweep or otherwise traverse memory to discover objects that are not referenced.

Claiming that ref counting is gc is a bit like claiming malloc/free is gc.

Re:For Better Computing, Liberate CPUs from Garbage Collection by norswap on Thursday May 09, 2019 @12:00PM (#19869409)
I've always seen automatic reference counting classified as GC.

e.g. https://users.cecs.anu.edu.au/~steveb/pubs/papers/rc-ismm-20...

> Reference counting and tracing are the two fundamental approaches that have underpinned garbage collection since 1960.

https://www.cs.virginia.edu/~cs415/reading/bacon-garbage.pdf

> Tracing and reference counting are uniformly viewed as being fundamentally different approaches to garbage collection that possess very distinct performance properties.

This last one is great if you want to understand GC tradeoffs btw, highly recommend it.

---

And as the first paper implies, refcounting is often slower (because it trashes caches). The issue is having to propagate the diminution of refcounts amongst a graph of references. This also creates de facto "pauses", much like tracing GC (although arguably more predictable!).

And this "propagation of diminished refcount" is very much a memory traversal — (probably) smaller in scope than (non-incremental) tracing, but also much less local.

Re:For Better Computing, Liberate CPUs from Garbage Collection by dleslie on Thursday May 09, 2019 @12:04PM (#19869447)
Interesting! I'll be reading that today.
Re:For Better Computing, Liberate CPUs from Garbage Collection by nickpsecurity on Thursday May 09, 2019 @01:47PM (#19870428)
"If the lifetime can be expressed with a state machine then it needn't be garbage collected so long as the language is sufficiently expressive."

One programmer here told me he does that in Ada and/or SPARK Ada using enumerated types encoding states. Now there's two of you. Most stuff I see like this are linear/affine/dependent types or separation logic. I rarely see people talking about simple solutions like this. Do you have any links that go in-depth on it with examples? It would be good to have something to pass along to people interested in this.

Re:For Better Computing, Liberate CPUs from Garbage Collection by crimsonalucard on Thursday May 09, 2019 @03:27PM (#19871414)
Any lifetime that depends on an io loop or user input cannot be determined within the loop Period. Even if you have an asic designed for garbage collection. It cannot even be dynamic.

Basically in these cases you have to extend the lifetime to beyond the point of indeterminism into the point of determinism or in other words move the life time outside of the loop.

Worst case scenario you move the lifetime to right before termination of the program.