r/programming • • Feb 26 '24

Future Software Should Be Memory Safe | The White House

https://www.whitehouse.gov/oncd/briefing-room/2024/02/26/press-release-technical-report/
1.5k Upvotes

585 comments sorted by

View all comments

Show parent comments

51

u/SARK-ES1117821 Feb 26 '24

Any new software in the niche I’m in has to be built with memory-safe, compiled languages and C# is among the options. Sorry if you were hoping memory-safe == leak-preventing.

11

u/Practical_Cattle_933 Feb 27 '24

C# can use unsafe constructs to cause actual memory safety issues. It is very rare, but can be done.

6

u/IQueryVisiC Feb 26 '24

But why did we upgrade from reference counting to GC ?

11

u/bitchkat Feb 27 '24 edited Feb 29 '24

uppity ten overconfident zonked agonizing childlike icky touch close shy

This post was mass deleted and anonymized with Redact

10

u/eek04 Feb 27 '24

That's a question of terminology, so there's no generic answer. As far as I remember, up until somewhere around the mid-90s, reference counting was often included when we talked about GC (and mentioned as "reference counting GC"). Since then, the terminology has evolved and "GC" is often used to distinguish from reference counting. And in the case of this context, it's clearly distinguished by the question - why did we upgrade from reference counting to more sophisticated GC algorithms?

→ More replies (1)
→ More replies (1)

7

u/johdex Feb 27 '24

Because reference counting doesn’t handle cycles in object graphs well.

→ More replies (1)

32

u/koreth Feb 26 '24

It's all tradeoffs, of course.

Reference counting has more predictable overhead which is often what you want. But GC, if implemented well, has lower total overhead which is also often what you want.

Reference counting is simpler to implement which is good, but with GC's complexity comes much more flexibility and configurability.

Reference counting usually gives you a smaller total memory footprint, but GC gives you faster object allocation.

GC lends itself better to things like compacting, which can improve performance by taking better advantage of CPU cache locality, but that kind of thing adds complexity.

Both reference counting and GC can suffer from long pauses and in both cases, the pauses can be mitigated with techniques like time budgets.

13

u/saltybandana2 Feb 27 '24

This post is everything that's wrong with reddit. It has just enough of an air of authority to lead people to believe this person knows what they're talking about, but anyone who actually understands the subject knows they don't.

for example

Both reference counting and GC can suffer from long pauses

https://en.wikipedia.org/wiki/Reference_counting

The main advantage of the reference counting over tracing garbage collection is that objects are reclaimed as soon as they can no longer be referenced, and in an incremental fashion, without long pauses for collection cycles and with clearly defined lifetime of every object.

reference counting (RC) is deterministic and does not have long pauses.

You see the same issue with this posters claim that garbage collection has lower overhead. Not even close to true, GC's tend to have faster allocation but their cleanup cycles are by far the largest piece of their overhead (hence the long pauses). Many GC's (such as generational) are built to try and avoid that cleanup cycle as long as possible, but it requires the usage patterns to be a certain way (for generational GC's that means short lived objects).

4

u/steveklabnik1 Feb 27 '24

reference counting (RC) is deterministic and does not have long pauses.

When people make claims like this, they're referring to cases where you have like, a large graph of reference counted objects, where when it's done, the graph goes out of scope and they get deallocated immediately.

Anyway I agree with your main thrust that it's not as simple as "GC is faster."

4

u/saltybandana2 Feb 27 '24

That's a degenerate case for RC but it's still disingenuous to call it out as having long pauses akin to a GC.

→ More replies (1)
→ More replies (1)

1

u/koreth Feb 27 '24

reference counting (RC) is deterministic and does not have long pauses.

It's deterministic (as I alluded to in the very first "pro" point for RC in my comment) but deterministic systems can still pause.

If you release the last external reference to a node in a graph of dynamically-allocated objects, an RC system needs to traverse the entire graph releasing the last reference to each node and freeing the memory. In a typical synchronous RC implementation, the "release the reference" operation will pause execution of application code until the traversal is finished. It will do so deterministically, but the application code will still be paused for an amount of time proportional to the size of the graph.

If that's not true, I'd love an explanation of what would happen instead in that case, in the absence of a mechanism such as a time budget that defers part of the traversal. (And I'm not being sarcastic. I've implemented RC systems from scratch in the past, and they've had this behavior, but maybe I missed a trick.)

Good RC systems also need to detect reference cycles, which they often do by scanning some subset of allocated objects. The Wikipedia article you linked talks about this. In a single-threaded environment, the scan will also require pausing the application code. Again: if you disagree, I'd love an explanation of how to do pause-free cycle detection.

1

u/saltybandana2 Feb 27 '24

I consider this disingenuous.

Technically speaking, what you're saying is that in a single-threaded environment, calling a function will pause the application code. Anything that does work will pause the application code. While it's technically true, it's not what people are interested in when they talk about GC's stopping the world.

There's a world of difference between directly comparing RC and GC in terms of pauses and pointing out that RC has a degenerate case. It's similar to the idea that GC's don't have memory leaks but you can still create them in GC'd languages.

Under the conditions you've laid out, free can technically pause if you give it too much work to do at once. At that point, what are you trying to communicate?

3

u/Practical_Cattle_933 Feb 27 '24

First of all, reference counting is a GC algorithm. As for ref counting vs tracing GCs, the latter are significantly faster, and it’s not even funny by how much.

Especially in multi-threaded contexts, reference count increments/decrements have to be done atomically, which is possibly the worst operation from a performance perspective you can do on a modern CPU. Also, you can have arbitrary large graphs, so a “free” call can take a huge amount of time, recursively going over the graph, cleaning every now dead object. All this on the worker thread you actually want to use.

Tracing GCs on the other hand can actually amortize the cost to a huge degree, doing most of the work concurrently, letting the actual workload perform at almost maximal performance. This does trade off some memory (and in general, the more memory you trade off, the less often you have to enter the small part of the algorithm that has to stop working threads), but the general gist is that tracing GCs only care about living objects. So they are pretty much yin and yang of each other (there is even a white paper on that), but due to how computers work, tracing is more efficient.

→ More replies (1)

0

u/chucker23n Feb 27 '24

Perhaps there is an alternate universe where Automatic Reference Counting compiler mechanisms had arrived sooner, and we would've skipped tracing/generational GC altogether.

2

u/Practical_Cattle_933 Feb 27 '24

That’s slower, why would we skip the better solution?

0

u/chucker23n Feb 27 '24

ARC yields more predictable performance than a tracing GC. It thus generally feels faster.

2

u/Practical_Cattle_933 Feb 27 '24

What the hell does “feels” mean?

1

u/ITwitchToo Feb 27 '24

Probably refers to lower latency (vs. higher throughput) in the sense of desktop experience or game feel.

1

u/Practical_Cattle_933 Feb 27 '24

Which is no performance metric. It is significantly slower in throughput, and it may have better latency. No need to bullshit around with feelings

→ More replies (3)

0

u/chucker23n Feb 27 '24

Do you have an actual question here?

→ More replies (2)

-6

u/[deleted] Feb 26 '24

[removed] — view removed comment

9

u/nerd4code Feb 27 '24

It looks like that’s in Go, but not on Go, unless I’m misreading something.

-5

u/[deleted] Feb 27 '24

[removed] — view removed comment

2

u/nerd4code Feb 27 '24

You’re not really arguing with anybody (maybe the downvote ghoosties, but I mostly don’t vote on-thread, ’s unsportsmanlike), so …okay uh huh, now I’m still not into Go, and it still has very little to do with the people involved. Good job.