Live data from Hacker News

The curious case of a memory leak in a Zig program

iamkroot.github.io

51–60 of 80 posts

Re: The curious case of a memory leak in a Zig program

#51
post #31

Seems like a user problem more than anything else

It's foremost a naming problem, FixedBufferAllocator doesn't hint that it is actually is a bit of a weird mix of a bump and stack allocator (IMHO if it would be a bump allocator it shouldn't have a free function at all, and for a stack allocator the free function should probably be called pop).

However both doesn't match Zig's expected alloc/free allocator interface, which is an interesting design challenge on its own.

Re: The curious case of a memory leak in a Zig program

#52
> If you are hell-bent on using FixedBufferAllocator only and you want to avoid copies, there is a way. Using two buffers (and separate allocators backed by them), it is possible to keep swapping between them after every iteration.

I found this bit lovely: the author has independently reinvented the core idea of semispace copying garbage collectors (see eg https://wingolog.org/archives/2022/12/10/a-simple-semi-space...).

Re: The curious case of a memory leak in a Zig program

#53

Earlier quoted context omitted.

Will do, I have just been procrastinating too much!

Such linear allocators are not too uncommon in embedded / static allocation context, but one definitely needs to know how they work. So first thought was you didn't read the docs, but docs do not clearly state that behavoour that is ugly (:

There are basically no docs for this allocator.

Re: The curious case of a memory leak in a Zig program

#54
post #35

I know absolutely nothing about Zig (but I know C) and when I read "FixedBufferAllocator" I immediately guessed what the problem would be. I can see why it is claimed as a C replacement. I am actually kind of surprised the author spent so much time figuring it out. The name of the allocator is not that well-defined, but at least to me it hints of it being simpler rather than full-featured allocator. I would also imag…

After reading the description of the problem, I also had the thought, "Maybe FixedBufferAllocator is a bump allocator?". So before reading further, I checked the documentation to see if I was right. Turns out .. there is no documentation for FixedBufferAllocator? Here's the documentation page: https://ziglang.org/documentation/master/std/#A;std:heap.Fix... . It just lists a couple methods, most of which have the desc…

> I knew it was still early for Zig, but that's a bit disappointing.

They're pretty explicit about not taking too much effort to document stuff in the stdlib, because any given thing in there may or may not make the final cut when the stdlib is stabilized (this is deliberate because they don't want to make people pissed or burned out for putting effort into documentation that winds up getting nuked). While FBA (or something like it) will likely make the cut, I'd say, maybe give them a break?

Re: The curious case of a memory leak in a Zig program

#55
post #41

Earlier quoted context omitted.

Why? All words are coined at some point.

well for one, because the correct word "idiomatic" already exists: https://wikipedia.org/wiki/Programming_idiom

there's a lot of sunlight between the class of things that are idiomatic and the class of things that are anti-patterns; and there are likely things that are idiomatic but still anti-patterns (yes, you can do this, and if you did this it would look like this, and it causes no regression in this particular case, but don't get in the habit of doing it this way because it can cause a hard-to-spot regression in the general case)

Re: The curious case of a memory leak in a Zig program

#56

Earlier quoted context omitted.

Thanks for the pointers! > use a GeneralPurposeAllocator and setting "enable_memory_limit" and "requested_memory_limit" Interesting! I hadn't looked at GeneralPurposeAllocator too closely, but yes these seem like the right way to do things instead of abusing FixedBufferAllocator as I did. > If the purpose is to "only use the stack"... Not really, I just had to decide on some arbitrary upper bound on the mem usage, an…

>Is there a better way? Just let the kernel handle it. The virtual memory and mapped memory abstraction the kernel has makes your program's implementation simpler.

Zig is a low-level language. You might not have an MMU. You might not have a kernel. You might be the kernel.

Re: The curious case of a memory leak in a Zig program

#57
post #4
post #3

TL;DR: the author had to figure out the hard way that Zig's FixedBufferAllocator is a bump allocator, and that it doesn't reuse freed memory except when it's the last allocation.

Nit: /last/latest/

That depends a good deal on your connotations for those words. Either work, so long as you restrict to the "live" allocations. If not, neither work.

Re: The curious case of a memory leak in a Zig program

#58
Every recommendation I’ve seen surrounding learning/using zig’s standard library highlights that there is very limited documentation, so you must read the source. Good news, it’s quite readable and navigable — I’ve done it a lot.

I’m not defending nor criticizing that fact or the OP, but it is the state of things today. Even the existence of the library docs is marked “experimental” on https://ziglang.org/learn/

Maybe it’s not emphasized enough.

Re: The curious case of a memory leak in a Zig program

#59
post #41

Earlier quoted context omitted.

well for one, because the correct word "idiomatic" already exists: https://wikipedia.org/wiki/Programming_idiom

there's a lot of sunlight between the class of things that are idiomatic and the class of things that are anti-patterns; and there are likely things that are idiomatic but still anti-patterns (yes, you can do this, and if you did this it would look like this, and it causes no regression in this particular case, but don't get in the habit of doing it this way because it can cause a hard-to-spot regression in the gener…

> anti-patterns

I agree with all that, but patternic is not a word.

Post reply on HN