News for hackers. Stuff that matters.
Re:So We've Got a Memory Leak by smallstepforman on Saturday May 11, 2024 @09:04AM (#40327791)
I just dont understand the fear with manual memory management. With RAII and and simple diligance (clear ownership rules), managing memory is an easy engineering task. I actually find it *more* challenging to deal with frameworks that insist or reference counting and shared pointers since ownership is now obscure.

I create it, I free it. I transfer, I no longer care. Its part of engineering discipline. Memory bugs are no worse than logic bugs, we fix the logic bugs, makes sense to fix the memory bugs. Disclaimer: I do embedded complex systems that run 24/7.

We do the same for OS resources (handles, sockets, etc) and dont use automatic resource managers, we do it manually. So why complicate the design with automatic memory management?

Re:So We've Got a Memory Leak by gizmo686 on Saturday May 11, 2024 @10:50AM (#40328430)
Memory bugs is an entire class of bugs that we have simply solved. If you use a language with a modern garbage collector (e.g. one that can handle cycles), you will very likely go an entire project without running into a single memory bug. To a first approximation, these bugs were not replaced with other bugs; they are simply gone. Further, we do not ask anything more of the programmer do accomplish this. Instead, we need the programmer to do less work than they would need to do with manual memory management.

That is not to say that garbage collection is an unambigous win. There are real downsides to using it. But for most programs, those modern garbage collectors are good enough that those downsides just don't matter.

Re:So We've Got a Memory Leak by mattpallissard on Saturday May 11, 2024 @12:08PM (#40328976)
> you will very likely go an entire project without running into a single memory bug

Sure, you haven't lost the handle to the memory, but you can still "leak" memory with a GC. Happens all the time. Add to data structure at state 1, do something in state 2, remove data in state 3. What happens you never hit state 3?

> But for most programs, those modern garbage collectors are good enough that those downsides just don't matter.

I mostly agree with this. Although, most programs hit a point where you have to be aware what the GC is doing and try to avoid additional allocations. Which isn't very ergonomic at times. And is often more fucking around than manual memory management, just concentrated to a smaller portion of the code base.

Re:So We've Got a Memory Leak by oivey on Saturday May 11, 2024 @12:35PM (#40329122)
Are you calling just allocating memory you’ll never use leaking? It is not. It is recoverable because for it not to be GC’d it has to be someone accessible. A memory leak means you lost the pointer to the memory, so now you can’t free it.
Re:So We've Got a Memory Leak by Maxatar on Saturday May 11, 2024 @12:59PM (#40329281)
>Are you calling just allocating memory you’ll never use leaking? It is not.

Yes it is, by definition a memory leak is any memory that has been reserved but won't be read from or written to. If you allocate memory into a data structure and after some point in time you will never read from or write to that memory, then that memory constitutes a leak.

Re:So We've Got a Memory Leak by cobbal on Saturday May 11, 2024 @01:35PM (#40329498)
There are (at least) 2 definitions of memory leak.

The upsides of the definition you gave are that it's simple, well defined, and maximally precise (nothing that is safe to collect is considered live)

The significant downside to this definition is that it's uncomputable. To know if memory is used requires knowing if a loop halts.

The second definition of memory leak is "unreachability" which is a bit harder to nail down. It's a conservative approximation of the first definition, but is more popular because it's computable, and it's practical to write programs with or without GC that don't leak

Re:So We've Got a Memory Leak by loeg on Saturday May 11, 2024 @05:50PM (#40330853)
The latter definition is not particularly useful for writing programs that run on hardware with finite memory. End users don't care whether or not the allocations are reachable when your program uses all the memory in the system and crowds out other programs, slows, and/or crashes.
Re:So We've Got a Memory Leak by coding123 on Sunday May 12, 2024 @01:51AM (#40332487)
That could also potentially be a cache in a highly unused program.
Re:So We've Got a Memory Leak by arccy on Saturday May 11, 2024 @12:50PM (#40329222)
lost can include you don't know where to find it even though you know it exists...
Re:So We've Got a Memory Leak by jrockway on Saturday May 11, 2024 @04:38PM (#40330499)
I'm OK with people using the expression "memory leak" to mean "unbounded growth in heap size over time". The case that I see most frequently is a routine that's set up like "use some memory, wait until an event that never happens happens, free memory". Technically if you let time run indefinitely, the memory would be freed. But since memory is finite, eventually you run out and your program crashes, which is annoying in this case.
Re:So We've Got a Memory Leak by gpderetta on Monday May 13, 2024 @11:03AM (#40344147)
> A memory leak means you lost the pointer to the memory, so now you can’t free it.

that's an useless definition as otherwise it would be trivial to augment, say, malloc to prevent memory leaks.

I would define a leak as unused memory that it is not otherwise reusable by a program.

Re:So We've Got a Memory Leak by loeg on Saturday May 11, 2024 @01:03PM (#40329311)
And this kind of leak is the same kind that is actually difficult to prevent and debug with manual allocation -- it's present / reachable in some structure, but not intentionally. Say you have a cache of items -- how big should it grow? Can it free space under memory pressure? If you reuse items, do they own collections (e.g., std::vec) that hold on to reserved but unused space (e.g. clear() doesn't free memory)? These are the hard problems and they are approximately the same with or without GC.
Re:So We've Got a Memory Leak by sa46 on Saturday May 11, 2024 @01:07PM (#40329332)
Joshua Bloch, of Effective Java fame, used the term unintentional object retention as a more precise term for memory “leaks” in garbage collected languages.
Re:So We've Got a Memory Leak by DowsingSpoon on Saturday May 11, 2024 @03:13PM (#40330049)
I’ve also heard this referred to as Abandoned Memory, or sometimes a Space Leak. It’s a common class of bug in a reference counted language, like Objective-C, where cycles need special consideration.
Re:So We've Got a Memory Leak by magicalhippo on Sunday May 12, 2024 @06:12AM (#40333317)
Interesting, not really heard those terms, always "live leak". It's quite possible to introduce a live leak with manual memory management as well, though perhaps less seldom.
Re:So We've Got a Memory Leak by mattpallissard on Saturday May 11, 2024 @01:45PM (#40329548)
Seemes a bit pedantic, but so am I. I like that term.
Re:So We've Got a Memory Leak by anon-3988 on Sunday May 12, 2024 @02:19AM (#40332564)
> Sure, you haven't lost the handle to the memory, but you can still "leak" memory with a GC. Happens all the time. Add to data structure at state 1, do something in state 2, remove data in state 3. What happens you never hit state 3?

What happens if you never escape a while loop? Should we ban while loop then? Bugs happen, we fix them. But I haven't had a memory bug in years outside of C and C++.

Re:So We've Got a Memory Leak by barbariangrunge on Saturday May 11, 2024 @10:56AM (#40328478)
Even game engines (including unreal) use gc these days, which is nuts. Still though, it’s best to be careful with your allocations, use pooling, etc
Re:So We've Got a Memory Leak by im3w1l on Saturday May 11, 2024 @11:41AM (#40328778)
There is one downside, though it's not inherent to garbage collection, it just happens to correlate in current languages. Gc'd languages don't have raii, and for that reason they actually make you do more work when managing non-memory resources. The have been some attempts to work around this with e.g. pythons with, but In my opinion it's less ergonomic due to forcing indentation.
Re:So We've Got a Memory Leak by vbezhenar on Saturday May 11, 2024 @12:00PM (#40328931)
Go's defer works like RAII and does not introduce indentations.

    f := createFile("/tmp/defer.txt")
    defer closeFile(f)
    writeFile(f)
Re:So We've Got a Memory Leak by seabrookmx on Saturday May 11, 2024 @12:09PM (#40328979)
Even when the syntax does require indentation ("with" in python, "using" in C#) it's still pretty clean IMO.
Re:So We've Got a Memory Leak by david_allison on Saturday May 11, 2024 @01:28PM (#40329464)
Additionally, `using` no longer requires indentation in C#

https://learn.microsoft.com/en-us/dotnet/csharp/language-ref...

Re:So We've Got a Memory Leak by seabrookmx on Saturday May 11, 2024 @03:47PM (#40330229)
TIL!

I just recently updated a few small services to C# 12 and there's a bunch of little niceties like this I'm finding (spread operator for instance).

Re:So We've Got a Memory Leak by pjmlp on Saturday May 11, 2024 @03:55PM (#40330281)
Using also no longer requires inheritance from IDispose, it suffices that the type does support Dispose pattern, which is great when coupled with extension methods.
Re:So We've Got a Memory Leak by HideousKojima on Saturday May 11, 2024 @01:32PM (#40329487)
Using no longer requires a new scope block in more recent versions of C#
Re:So We've Got a Memory Leak by valicord on Saturday May 11, 2024 @04:01PM (#40330310)
This misses the main benefit of RAII which is that you can't forget to close the file.
Re:So We've Got a Memory Leak by pjmlp on Sunday May 12, 2024 @06:15AM (#40333334)
While not perfect, static analysis takes care of that, in similar veins C and C++ devs reach out to them for the language flaws.
Re:So We've Got a Memory Leak by rcxdude on Monday May 13, 2024 @04:04AM (#40340828)
Defer and context managers are a pale approximation of RAII. They are constrained to lexical scope, as opposed to RAII which works with object lifetimes. And it's the latter case which is much more error prone than 'oops, I forgot to close a file I opened this function'.
Re:So We've Got a Memory Leak by neonsunset on Saturday May 11, 2024 @01:18PM (#40329402)
Do they?

    // Disposed when goes out of scope
    using var http = new HttpClient();
    var text = await http.GetStringAsync("https://example.org/");
Re:So We've Got a Memory Leak by pjmlp on Sunday May 12, 2024 @06:14AM (#40333325)
They surely do, D for example.

Swift as another one, RC is a GC algorithm, before the usual clueless reply from people not educated in CS.

Followed by FP languages now adopting effects and linear type systems, alongside their automatic memory management approaches.

Re:So We've Got a Memory Leak by Jasper_ on Saturday May 11, 2024 @01:21PM (#40329418)
Uncollectable reference cycles are shockingly easy to make in JS, especially with React. A classic example:

    function closure() {
        var smallObject = 3;
        var largeObject = Array(1000000);

        function longLived() { return smallObject; }
        function shortLived() { return largeObject; }
        shortLived(); return longLived;
    }
Will keep largeObject alive.
Re:So We've Got a Memory Leak by AgentME on Saturday May 11, 2024 @03:34PM (#40330144)
This isn't an uncollectable reference cycle. It's true that with this code in most/all JS engines, if there's a reference to the function `longLived` then `largeObject` will be kept in memory, but reference cycles are collectable in standard garbage collection systems. Both of the values will be garbage-collectable once no outside references to `longLived` still exist. Pure reference counting systems (Rust Rc, C++ shared_ptr, etc) are the kind of system that fail to automatically handle cycles.

You could test this with your code by setting a FinalizationRegistry to log when they're both finalized, unset any outside reference to `longLived`, and then do something to force a GC (like run Node with --expose-gc and call `global.gc()`, or just allocate a big Uint8Array).

Re:So We've Got a Memory Leak by cobbal on Saturday May 11, 2024 @01:39PM (#40329522)
Is this a property of JavaScript or the engine running it? This feels like something a sufficiently-smart™ closure implementation should be able to prevent.
Re:So We've Got a Memory Leak by Jasper_ on Saturday May 11, 2024 @02:04PM (#40329659)
This is an artifact of V8's GC. Effectively, largeObject and smallObject are tracked together, as a unit. Splitting it out into two separate records increases average memory usage. They keep saying they want to fix it eventually, but it's been this way for 10+ years at this point.

You really do have to know the quirks of what you're targeting.

Re:So We've Got a Memory Leak by Dylan16807 on Sunday May 12, 2024 @12:29AM (#40332287)
> sufficiently-smart™

Well, the reference lua implementation handles this case fine, and it's a solid but not at all smart™ codebase.

Re:So We've Got a Memory Leak by kevingadd on Saturday May 11, 2024 @01:53PM (#40329600)
JS runtimes are allowed to optimize this out, IIRC, and will often do so.
Re:So We've Got a Memory Leak by moonchild on Saturday May 11, 2024 @11:26PM (#40332096)
Geez! Didn't henry baker warn against implementing closures this way decades ago? This is an implementation bug—the code is fine.
Re:So We've Got a Memory Leak by patrick451 on Saturday May 11, 2024 @04:46PM (#40330545)
Performance regression is a bug and GC has a horrific overhead in the average program. My computer is orders of magnitude faster than it was 15 years ago but it spends all of it's time wondering around in the wilderness hunting for memory to free. We could have just told it.

> those modern garbage collectors are good enough that those downsides just don't matter.

This is not what I see. Every I look at a profile of a program written in a GCed language, most of the time is spent in the garbage collector. I can't recall the last time I looked at a c++ profile where more than 10% of the time spent in new/delete. I have seen 100x speedups by disabling GC. You can't ship that, but it proves there is massive overhead to garbage collection.

Re:So We've Got a Memory Leak by macintux on Saturday May 11, 2024 @05:51PM (#40330860)
> GC has a horrific overhead in the average program

It doesn't have to be that way. The BEAM is designed for tiny processes, and GC is cheap.

Re:So We've Got a Memory Leak by toast0 on Saturday May 11, 2024 @06:08PM (#40330941)
I love Erlang and BEAM, but the reason the GC is (mostly) cheap is because self-referential data structures are impossible, so you can have a very simple and yet still very effective GC. One heap per process also helps immensely.

Also, when your process has a giant heap, the GC gets expensive. There's been lots of improvement over the years, but I used to have processes that could usually go through N messages/sec, but if they ever got a backlog, the throughput would drop, and that would tend towards more backlog and even less throughput, and pretty soon it will never catch up.

That sometimes happens with other GC systems, but it never feels quite so dire.

Re:So We've Got a Memory Leak by pjmlp on Sunday May 12, 2024 @06:17AM (#40333338)
Depends pretty much on what implementation one is specifically talking about.

Just like not all malloc()/free() were born equal, to the point companies did made big bucks selling specialised implementations.

Re:So We've Got a Memory Leak by lomase on Monday May 13, 2024 @11:38AM (#40344539)
I also speed up a function 100 times. It was concatenatign strings inside a loop. I changed it to used StringBuilder instead.

I could have disabled the GC to get the same speed up.

Re:So We've Got a Memory Leak by jules on Saturday May 11, 2024 @11:36PM (#40332121)
The article in question is about Ruby, which is garbage collected.

It's easy to get in trouble with an interconnected object graph: you mess up and hold onto a pointer that you shouldn't be holding onto, which results in a leak.

Re:So We've Got a Memory Leak by armchairhacker on Saturday May 11, 2024 @12:09PM (#40328978)
IMO it’s not that memory management is hard, it’s that developers are imperfect, so writing a 100% UB-free, leak-free program is hard. And it only takes a single oversight to cause havoc, be it a CVE, leak that causes a long-running program to slowly grow memory, or hair-pulling bug that occurs 1 in 1000 times.

Yes, logic bugs have the same issues, and even in languages like Java we can sometimes (albeit rarely IIRC) get memory leaks. But memory-safe languages are an improvement. Just like TypeScript is an improvement over JavaScript, even though they have the same semantics. We have automated systems that can decrease the amount of memory errors from 1% to 0.01%, why keep leak and UB prevention a manual concern? Moreover, the drawbacks of memory-safe languages are often not an issue: you can use a GC-based language like Java which is easy but has overhead, or an enforced-ownership language like Rust which has a learning curve but no overhead. Meanwhile, while logic bugs can be a PITA, memory bugs are particularly notorious, since they don’t give you a clear error message or in some cases even cause your program to halt when they occur.

Tangential: another solution that practically eliminated a class of bugs is formal verification. And currently we don’t see it in any system but those where correctness is most important, but that’s because unlike memory management the downsides are very large (extremely verbose, tricky code that must be structured a certain way or it’s even more verbose and tricky). But if formal verification gets better I believe that will also start to become more mainstream.

Re:So We've Got a Memory Leak by elondaits on Saturday May 11, 2024 @09:40AM (#40327974)
I worked with manual memory management for a decade (24/7 systems) and don’t miss it. It’s not per-se hard, it’s not scary, but if you’re dealing with structures that may contain reference loops, or using an architecture based on event handlers that may be moving references around, you need to do some very careful design around memory management instead of just thinking of the problem domain.
Re:So We've Got a Memory Leak by hedora on Saturday May 11, 2024 @10:32AM (#40328333)
By far, the worst memory leak I’ve ever had to debug involved a cycle like you are describing, but it was in a Java program (swing encourages/encouraged such leaks, and “memory leaks in java are impossible”, so there weren’t decent heap profilers at the time).

For the last few decades, I’ve been writing c/c++/rust code, and the tooling there makes it trivial to find such things.

One good approach is to use a C++ custom allocator (that wraps a standard allocator) that gets a reference to a call site specific counter (or type specific counter) at compile time. When an object is allocated it increments the counter. When deleted, it decrements.

Every few minutes, it logs the top 100 allocation sites, sorted by object count or memory usage. At process exit, return an error code if any counters are non-zero.

With that, people can’t check in memory leaks that are encountered by tests.

In practice, the overhead of such a thing is too low to be measured, so it can be on all the time. That lets it find leaks that only occur in customer environments.

Re:So We've Got a Memory Leak by billjings on Saturday May 11, 2024 @11:58AM (#40328916)
But circular references don't leak in Java. You have to have a GC root (e.g. a static, or something in your runtime) somewhere pointing at the thing to actually leak it.

There is one case where a "circular" reference can appear to cause a leak that I know of: WeakHashMap. But that's because the keys, which are indeed cleaned up at some point once the associated value is GC'd, are themselves strongly retained references.

Re:So We've Got a Memory Leak by chopin on Sunday May 12, 2024 @12:45PM (#40335776)
ThreadLocal can be another problem. Tomcat kills and recreates threads for this reason.

Edit: another one is swapping code by swapping class loaders. That can retain static references in the runtime class loader.

Re:So We've Got a Memory Leak by astrobe_ on Saturday May 11, 2024 @07:03PM (#40331169)
Excluding from the problem domain the tool - and the constrains that come with it - namely the computer, isn't it a bit arbitrary?

I think nowadays the only major languages that don't provide GC are C/C++, and one use them typically for their unrivaled performances, meaning performance is a more or less explicit requirement, so it is part of the problem domain.

Re:So We've Got a Memory Leak by pjmlp on Sunday May 12, 2024 @06:19AM (#40333345)
Even those have two major dialects with GC support, namely Unreal C++ and C++/CLI.

There was C++/CX as well, with WinRT/COM reference counting, but it got killed in MS internal politics.

Re:So We've Got a Memory Leak by yosefk on Saturday May 11, 2024 @09:11AM (#40327831)
35% of vulnerabilities in the biggest tech companies being due to use after free bugs are a part of the answer. (More than 90% of severe vulnerabilities are due to memory bugs impossible in memory-safe languages.)
Re:So We've Got a Memory Leak by samatman on Saturday May 11, 2024 @12:44PM (#40329188)
Every memory-safe language has a runtime written in a memory-managed language. Yes, even Rust: Rust is implemented using many (well-vetted) unsafe blocks.

So projects to improve the quality, and lower the defect rate, in memory-managed programming, are far from wasted. Even if they only get used to write fast garbage collectors so that line coders can get on with the work of delivering value to customers.

Re:So We've Got a Memory Leak by loeg on Saturday May 11, 2024 @01:06PM (#40329328)
Manual memory management vs GC is orthogonal to memory safe vs unsafe.
Re:So We've Got a Memory Leak by AgentME on Saturday May 11, 2024 @03:56PM (#40330287)
In practice the lines of each issue are placed very close together. Other than Rust, there are no popular memory safe languages that use manual memory management.
Re:So We've Got a Memory Leak by saagarjha on Sunday May 12, 2024 @05:43AM (#40333237)
Rust doesn’t use manual memory management?
Re:So We've Got a Memory Leak by maleldil on Sunday May 12, 2024 @06:05AM (#40333299)
It does because you're still responsible for managing the lifetimes, even if you don't explicitly call malloc/free. Automatic memory management is reference counting and GC.
Re:So We've Got a Memory Leak by mrkeen on Saturday May 11, 2024 @09:16AM (#40327862)
> We do the same for OS resources (handles, sockets, etc) and dont use automatic resource managers

1) Modern languages have made inroads here.

2) The OS is my automatic resource manager. When I hit ctrl-c on my running C program, my free() is never hit, yet it is cleaned up for me.

> So why complicate the design with automatic memory management?

I don't know beforehand who is going to be reading my memory, how many times, in what order, or whether they're going to copy out of it, or hold onto pointers into it.

> I just dont understand the fear with manual memory management. With RAII and and simple diligance (clear ownership rules), managing memory is an easy engineering task.

I claim that if managing memory is that straightforward, then it makes more sense to leave it to the compiler (Rust-style, not Java-style.) rather than let a human risk messing it up.

Re:So We've Got a Memory Leak by jeremyjh on Saturday May 11, 2024 @09:29AM (#40327919)
Memory leaks can be hard to track down, but overflows and use after free bugs can take a project weeks off schedule. Depending on what is overwritten and where, the effects show up very far from the source of the problem. For an engineering or product manager this is a terrifying prospect. Managed memory more or less solves those problems - it introduces a couple of others and there are still a possibility of resource leaks but these problems are both more rare and generally easier to pin down.
Re:So We've Got a Memory Leak by inetknght on Saturday May 11, 2024 @10:14AM (#40328204)
> just dont understand the fear with manual memory management. With RAII and and simple diligance (clear ownership rules), managing memory is an easy engineering task. I actually find it more* challenging to deal with frameworks that insist or reference counting and shared pointers since ownership is now obscure.*

I mostly agree.

With RAII, memory management is simplified. "Just" evaluate the lifetime of the object. Super easy if you're good at software design.

Reference counting and shared pointers still have their niche use case. But I've most often seen referencing counting used as a crutch where reference-counting is _easy_ but designing around object lifetimes is more appropriate.

> Memory bugs are no worse than logic bugs

It's true. A memory bug is "just" another logic bug. Memory bugs lead to null pointers and wild pointers. They're just as dangerous as logic bugs. Memory bugs are also far more preventable with RAII.

> We do the same for OS resources (handles, sockets, etc) and dont use automatic resource managers, we do it manually. So why complicate the design with automatic memory management?

I'm just going to point out that I don't do manual allocations for OS resources. I wrap OS resources into RAII objects. I've made lots (!) of custom RAII wrappers for OS objects and library objects. It's trivial to do and saves a feckton of headaches.

C++ has std::fstream. I'm not saying it's great. But it's definitely RAII for files and has been around for... well I don't know exactly but certainly 25+ years.

Re:So We've Got a Memory Leak by xedrac on Saturday May 11, 2024 @10:27AM (#40328301)
RAII was a huge improvement over C and I was shocked to see Zig forego such an improvement. Rust adopted RAII, and took it all to the next level with no data races and a lot more bugs caught at compile time.
Re:So We've Got a Memory Leak by pjmlp on Sunday May 12, 2024 @06:21AM (#40333349)
Rust helped making affine types acceptable in mainstream.

The next level is going into linear types, which is what some GC first languages are now slowly looking into.

Re:So We've Got a Memory Leak by watermelon0 on Saturday May 11, 2024 @10:00AM (#40328107)
> We do the same for OS resources (handles, sockets, etc) and dont use automatic resource managers, we do it manually.

Generally, in most languages, file handles and sockets are automatically closed, when the variable holding them gets out of scope.

Re:So We've Got a Memory Leak by pylua on Saturday May 11, 2024 @09:50AM (#40328040)
Businesses need software to be less complex and easier to develop so it is cheaper.

I can’t imagine manual memory management on these large scale projects with a high variance of developer scale and opinions.

Re:So We've Got a Memory Leak by n4r9 on Saturday May 11, 2024 @09:45AM (#40328005)
> Memory bugs are no worse than logic bugs

I guess it comes down to what you mean by "worse". It seems that memory bugs carry a higher risk of being completely invisible except in deliberately contrived situations. This makes them more dangerous because there's less of an incentive to fix something that clients will never notice.

Re:So We've Got a Memory Leak by _gabe_ on Saturday May 11, 2024 @11:34AM (#40328742)
> It seems that memory bugs carry a higher risk of being completely invisible except in deliberately contrived situations.

Do they really carry a higher risk of being invisible though? You don’t need to “contrive” situations either. My company just spent 6+ months trying to solve a JavaScript undefined symbol bug stemming from a library. When we finally tracked it down, it was because we were using import instead of require, which the documentation didn’t clarify was important. The fix we implemented was nowhere near where we expected the bug, and the only reason we finally solved it was because we had exhausted all other options. Sounds the same, if not worse, as a difficult to track memory bug to me.

Re:So We've Got a Memory Leak by n4r9 on Sunday May 12, 2024 @05:22PM (#40337693)
I'm not sure if I get your point here, but it's possible that "invisible" was too ambiguous. What I meant was that a memory bug may be less likely to cause noticeable functional changes to the behaviour of the program during everyday usage. I.e. the client won't notice them.
Re:So We've Got a Memory Leak by jandrewrogers on Saturday May 11, 2024 @12:55PM (#40329254)
The use of manual memory management increases the cognitive load when reasoning about software. Working memory capacity varies considerably between people and is a performance limiting factor when designing complex systems. You see analogues of this in other engineering disciplines too.

Over my many years of working in software development I have come around to the idea that most software developers do not have sufficient working memory capacity to reason about memory management in addition to everything else they must reason about at the same time. They may know mechanically how to properly do manual memory management but when writing code they drop things if they are juggling too many things at once mentally. It isn't a matter of effort, there simply is a complexity threshold past which something has to give. Automatic memory management has its tradeoffs but it also enables some people to be effective who otherwise would not be.

On the other side of that you have the minority that get manual memory management right every time almost effortlessly, who don't grok why it is so difficult for everyone else because it is literally easy for them. I think some people can't imagine that such a group of engineers exist because they are not in that group and their brain doesn't work that way. If you are in this group, automatic memory management will seem to make things worse for unclear benefit.

I've observed this bifurcation in systems software my entire career. In discussions about memory safety and memory management, it is often erroneously assumed that the latter population simply doesn't exist.

Re:So We've Got a Memory Leak by mattpallissard on Saturday May 11, 2024 @01:55PM (#40329606)
I think the rift comes from people thinking manual memory management is a bunch of random allocs and frees all over the place. That is gross and those of us who don't mind managing memory don't like it either.

Personally my gripe is when people don't think about memory or space complexity at all. I don't care if it's a custom memory management strategy or GC'd language, you need to think about it. I have the same gripe about persistence, socket programming, and database queries.

Abstractions are great, use them, love them. But understanding what the hell they are doing under the hood not only improves efficiency, it prevents bugs, and gives you a really solid base when reasoning about unexpected behavior.

Re:So We've Got a Memory Leak by Dylan16807 on Saturday May 11, 2024 @03:50PM (#40330244)
If the automatic allocation tools are good, then they're doing the same thing as quality manual management but with much more compiler enforcement.
Re:So We've Got a Memory Leak by mattpallissard on Sunday May 12, 2024 @11:08AM (#40335090)
Not exactly, GC is a general solution. Manual management can be tailor made. But to your point more compiler enforcement is a good thing.
Re:So We've Got a Memory Leak by Dylan16807 on Sunday May 12, 2024 @11:17AM (#40335154)
I didn't mean just GC. Automated memory management comes in many forms and can be pretty specific.

And it can be tailored too if a situation really deserves it. If I'm doing something custom I want the smallest amount of code to verify, just the special memory manager and not everything that uses it.

Re:So We've Got a Memory Leak by mattpallissard on Sunday May 12, 2024 @01:32PM (#40336085)
True, I misread your original reply.
Re:So We've Got a Memory Leak by pjmlp on Saturday May 11, 2024 @03:51PM (#40330253)
Which is actually the case in enterprise source code, touched by hundreds of offshoring companies.
Re:So We've Got a Memory Leak by mirsadm on Saturday May 11, 2024 @02:11PM (#40329697)
Any professional developer should understand these things. It was taught at first year of my computer science degree. As you say it isn't particularly difficult if you have a strategy. If you can't manage memory then what about any other resource that needs to be manually closed (sockets, file handles etc ).
Re:So We've Got a Memory Leak by da_chicken on Saturday May 11, 2024 @02:53PM (#40329919)
I'm not sure I understand the causal link between something having a proven track record of being error-prone and not understanding it.
Re:So We've Got a Memory Leak by prerok on Saturday May 11, 2024 @05:00PM (#40330604)
The problem is that automatic memory management means to some newcomers that you don't have to worry about it. Which is not true at all.

These newcomers may have heard about it, may even understand it at some level, but it's not in their rote knowledge. GC is great if you know that every allocation you do is expensive and that you know that it will be taken care of properly, because it will be short-lived.

But I have seen many cases where people just don't have it in their conciousness why allocating huge arrays is a problem or why allocating a new Character object for every single character in a string might be bad. As soon as I point it out, they get it, but it's not like they thought of it while writing their algorithm.

Re:So We've Got a Memory Leak by danielledias204 on Sunday May 12, 2024 @10:34PM (#40339301)
But for most developers using GC (Garbage Collected) languages, not understanding these concepts does not affect their ability to earn a salary and continue working.
Re:So We've Got a Memory Leak by fragmede on Saturday May 11, 2024 @08:14PM (#40331449)
> Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

-Brian Kernighan.

Re:So We've Got a Memory Leak by fransje26 on Sunday May 12, 2024 @06:07AM (#40333304)
I fully agree, and I've learnt that humbling lesson the difficult way. The transition from "I'm elated and proud of the clever solution I wrote" to "I'm too dumb to debug this so I have to rewrite my not-so-clever solution" was sobering. :-) We live and learn.
Re:So We've Got a Memory Leak by hinkley on Sunday May 12, 2024 @02:24PM (#40336399)
Even if you understand every bit of the code, when obscure bugs pop up you have to consider the whole system, and that will quickly overwhelm most people. That’s how pernicious bugs stick around for ages and make the team look like idiots.

Sometimes all you can do is refactor around the problem until you can see it. Or push people to stop being that sort of clever in the first place and write more accessible code.

Re:So We've Got a Memory Leak by artemonster on Saturday May 11, 2024 @01:13PM (#40329369)
Oh yes! And also dont forget about the hand holding static type system that wont let you fart unless you explicitly convince it that you will not shit your pants!
Re:So We've Got a Memory Leak by bigstrat2003 on Saturday May 11, 2024 @01:57PM (#40329616)
If manually managing memory were in fact an easy engineering task, software developers wouldn't be so demonstrably bad at it.
Re:So We've Got a Memory Leak by derriz on Saturday May 11, 2024 @03:15PM (#40330055)
Sure, for simple flows-of-control, GC buys you little and an RAII type approach is fine. But RAII really only works if lifecycle and ownership spans are reflected in the lexical structure of the code base. RAII relies on creation and cleanup happening within a block/lexical scope.

Unfortunately in the real C++ codebases I've had to work with, the flow of control is never so simple. Any correspondence between lexical scope and lifecycle is broken if the code uses exceptions, callbacks, any type of async, threads, etc.

Re:So We've Got a Memory Leak by tlarkworthy on Saturday May 11, 2024 @03:30PM (#40330125)
... or a persistent data-structure [1].

[1] https://en.wikipedia.org/wiki/Persistent_data_structure

Re:So We've Got a Memory Leak by pixelfarmer on Sunday May 12, 2024 @01:38AM (#40332459)
No, it still applies. You have to think on a higher abstraction level, like components with interfaces between them of sorts. If one of them creates a resource (because this goes far beyond memory allocations which are also only resources), it MUST take care to delete that resource again. Whether that happens in a different thread or whatever: Who cares. (And yes, more care needs to be taken around creation/deletion/use of resources then.) The Linux kernel is loaded with examples of it.

Any deviation from that MUST be documented which includes seeing that as comments in code so people who read/maintain that code know what this is all about.

Thing is, many memory leaks are actually logic bugs and no (resource-)GC will save your bacon against these, in fact, you made your life more complicated through it because now you have to debug through another (abstraction) layer before getting to the true problem.

This is what this ownership in Rust is all about: It makes ownership visible to the programmer so they have to take care about it in a very explicit manner.

Re:So We've Got a Memory Leak by pjmlp on Saturday May 11, 2024 @03:50PM (#40330247)
It doesn't scale in large teams of various skill levels.
Re:So We've Got a Memory Leak by kelnos on Saturday May 11, 2024 @07:05PM (#40331176)
So you never, ever, ever make a mistake? No, don't bother answering that. Of course you do. No amount of "engineering discipline" in the world can stamp out the human propensity to occasionally make a mistake.
Re:So We've Got a Memory Leak by Brian_K_White on Saturday May 11, 2024 @07:34PM (#40331280)
Where did they say that?

Why do you jump to that?

Why didn't you assume that they make errors like everyone, and simply deal with them?

Your ego is so fragile that it can't survive un the the same universe with the idea that someone somewhere might be living just fine without a crutch you apparently consider indispensable?

The fact is there are more than one way to detect and avoid errors reaching all the way to production. Every other human activity since humans existed employs them for thousands of years before golang existed. Rigor and discipline and methodology are not actually impossible. Shock!

Re:So We've Got a Memory Leak by anon-3988 on Sunday May 12, 2024 @02:14AM (#40332553)
You can literally replace memory management in your post with stack management.

You wouldn't say the same thing about stack push and pop would you? Memory management is kind of a solved problem for 99% of program. It only happens very rarely and under very constrained environment.

Yes, I did encountered a stack overflow error because I didn't realize the size of the struct is gigantic. So I solved it by creating the object on the heap instead. Similarly, memory "bugs" in managed systems is very rare and they can mostly be solved.

Every time a managed systems have issues, we get blog posts like this exactly because they are rare. If I write a blog post every time I have encountered a memory leak in a C program, well no one really cares. It's kind of expected.

Re:So We've Got a Memory Leak by klysm on Sunday May 12, 2024 @12:00PM (#40335463)
History has repeatedly demonstrated that it is not easy. Simple diligence is clearly insufficient.