Live data from Hacker News

Go has added Valgrind support

go-review.googlesource.com

41–50 of 157 posts

Re: Go has added Valgrind support

#41

Earlier quoted context omitted.

> it worked fine after spending an hour making a suppressions file. So you are confirming the problem, but treating it as if ignoring it is the solution for all?

it's a rejection of the thesis that it "does not work". It does, but it requires investing into a suppression file.

plus, LLMs can generate suppressor files from logs. It's much faster these days.

I've had success with this approach.

Re: Go has added Valgrind support

#42
post #30
post #26

Earlier quoted context omitted.

A GC only deallocates unreferenced memory, if you keep unused references, that's a leak the GC won't catch, as it has no way to know that you won't need it later. It can happen when your variables have too long a lifespan, or when you have a cache where the entries are not properly evicted.

But how would Valgrind know more than the GC? Of course a program in a GCed language can leak memory, but it’s not clear to me how Valgrind would detect the kinds of memory leaks that you can create in pure Go code (without calling into C or using unsafe functions to allocate memory directly).

Valgrind can tell you what is still in use when the program exits, along with useful information, like where it comes from. You can then assess to situation and see if it is normal or not.

In addition, Valgrind is actually a complete toolsuite, not just a memory leak detector. Among these tools is "massif", a memory profiler, so you will have a graph of memory use over time and it can tell you from where these allocations come from.

Not, if your language is fully GCed you can have a debug GC that does the job more efficiently than Valgrind on this task, but I don't know if it is the case for Go.

Re: Go has added Valgrind support

#43
post #41

Earlier quoted context omitted.

it's a rejection of the thesis that it "does not work". It does, but it requires investing into a suppression file.

plus, LLMs can generate suppressor files from logs. It's much faster these days. I've had success with this approach.

Valgrind can generate suppression files directly.

Re: Go has added Valgrind support

#44
post #15
post #3

It only works if every package tests with it. Otherwise the relevant warnings get swamped by a huge amount by irrelevant warnings. This is why running Valgrind on Python code does not work.

If that were true it would also apply to C and C++. I have used Valgrind with Python + Boost C++ hybrid programs and it worked fine after spending an hour making a suppressions file.

For me, a waaay outdated suppressions file for Qt + a rough understanding what syscalls and frameworks do is enough. If my app crashes in a network request and a byte sent to the X server (old example, I use Wayland now) is uninitialized, I know to ignore it.

Valgrind(-memcheck) is an extremely important tool in memory-unsafe languages.

Re: Go has added Valgrind support

#45
post #7

Earlier quoted context omitted.

Ideally, they would have learnt from other languages, and offered explicit control over what goes into the stack instead of relying into escape analysis alone. As it is, the only way to currently handle that is with " -gcflags -m=3" or using something like VSCode Go plugin, via "ui.codelenses" and "ui.diagnostic.annotations" configurations.

I sometimes dream of a GCed language with a non-escaping pointer type. However to make it really useful (i.e. let you put it inside other non-escaping structs) you need something on the scale of the Rust borrow checker, which means adding a lot of complexity.

Which is why several GC languages (the CS meaning of GC), are rather going down the path to keeping their approach to automatic resource management, plus type systems improvements for low level high performance code when needed.

So you only go down into the complexity of affine types, linear types, effects, formal proofs, dependent types, if really needed, after spending time reasoning with a profiler.

Now, this does not need to be so complex, since languages like Interlisp-D and Cedar at Xerox, that many GC languages have offered value types and explicit stack allocation.

That alone is already good enough for most scenarios, provided people actually spend some time thinking about how to design their data structures instead of placing everything into the heap.

Re: Go has added Valgrind support

#46
post #42
post #30

Earlier quoted context omitted.

But how would Valgrind know more than the GC? Of course a program in a GCed language can leak memory, but it’s not clear to me how Valgrind would detect the kinds of memory leaks that you can create in pure Go code (without calling into C or using unsafe functions to allocate memory directly).

Valgrind can tell you what is still in use when the program exits, along with useful information, like where it comes from. You can then assess to situation and see if it is normal or not. In addition, Valgrind is actually a complete toolsuite, not just a memory leak detector. Among these tools is "massif", a memory profiler, so you will have a graph of memory use over time and it can tell you from where these alloca…

Pprof should cover most of that for pure Go code, though.

Re: Go has added Valgrind support

#47
post #8

Earlier quoted context omitted.

pprof is pretty good, what do you need?

yes, that's what I use, just wonder if there are alternatives. I am not sure how valgrind compares to it or the goref tool mentioned above, just asking around, does not hurt.

Alternative to solve what problem? pprof is very powerful, it's not missing much.

Re: Go has added Valgrind support

#48
post #4

> Instead of adding the Valgrind headers to the tree, and using cgo to call the various Valgrind client request macros, we just add an assembly function which emits the necessary instructions to trigger client requests. Love that they have taken this route, this is the way bootstraped toolchains should be, minimal building blocks and everything else on the language itself.

I am still curious, had they not gone this route, and avoided the other two routes mentioned, what could they have done to make this process as simple as the rest of Go tends to be, and nearly as performant? I guess this is an ongoing question to be solved at a future date.

It would be another scenario to use as ammunition for "see you can't implement a language toolchain without using C", usually voiced by folks without background in compiler design, and understanding that most of the time that is a decision that spurs out of convenience and nothing else.

Assembly isn't that hard, those of us that grown around 8 bit home computers were writing Z80 and 6502 Assembly aged 10 - 12 years old, while having fun cracking games and setting the roots of Demoscene.

Re: Go has added Valgrind support

#49

looks very promising, one of the biggest issue in golang for me is profiling and constant memory leaks/pressure. Not sure if there is an alternative of what people use now

How are you getting "constant memory leaks" in a GC'd language?

This was often a question asked in Java interviews as well.

In Java heap fragmentation is usually considered a separate issue but I understand go has a non-moving garbage collector so you can lose memory due to pathological allocations that overly fragment memory and require constantly allocating new pages. I could be wrong about this since I don't know a lot about go, but heap fragmentation can cause troubles for long running programs with certain types of memory allocation.

Beside that, applications can leak memory by stuffing things into a collection (map or list) and then not cleaning it up despite becoming "stale". The references are live from the perspective of the garbage collector but are dead from the application perspective. Weak references exist to solve this problem when you expose an API that stores something but won't be able to know when something goes out of scope. I wouldn't consider this to be common, but if you are building a framework or any kind of platform code you might need to reach for this at some point. Some crazy folks also intern every string they encounter "for performance reasons" and that can obviously lead to what less crazy folk would consider a memory leak. Other folk stick a cache around every client and might not tune the cache parameters leading to unnecessary memory pressure...

Re: Go has added Valgrind support

#50
post #41

Earlier quoted context omitted.

plus, LLMs can generate suppressor files from logs. It's much faster these days. I've had success with this approach.

Valgrind can generate suppression files directly.

yes but LLMs have access and understanding of my code and can better discern what should be suppressed.
Post reply on HN