Excellent, excellent article! I have a question though. > Couldn't we rearrange the memory to get a block of 6 contiguous bytes? Some sort of defragmentation process? > Sadly not. Remember earlier we talked about how the return value of malloc is the address of a byte in memory? Moving allocations won't change the pointers we have already returned from malloc. We would change the value those pointers are pointed at,…
Memory Allocation
131–140 of 182 posts
Re: Memory Allocation
#132Earlier quoted context omitted.
You mean a bug this deep took one day to debug?
Yes and no, it took me from 8am to 3am once we decided it needed to get fixed but really it sat on the app for years, it only happened on a background process that sent print jobs on a timer, since it used Windows GDI to compose the image we sent to the printer it was affected (our "frontend" should've been affected too but never was, I guess because it had a different memory usage pattern). We just had it restart it…
Re: Memory Allocation
#133Re: Memory Allocation
#134I was having some fun recently just seeing how fast we can allocate large chunks of memory. There's something refreshing about firing up a C (or maybe now Zig, in my case) compiler and allocating a gigabyte of memory, and seeing that your process is using exactly 1GB.
How did you measure that? What was fastest? I'd imagine sbrk is faster than mmap, but I haven't checked. If you're on Linux, then doing a virtual memory allocation will not cause the memory to have physical representation. Did you populate the pages through looping? Or did you use MAP_POPULATE?
I saw that madvise was going to get MADV_POPULATE_(READ|WRITE) in the Linux kernel mailing lists back in 2021, but I haven't seen it on the kernels that I use (5.4, 5.15)
Re: Memory Allocation
#135Re: Memory Allocation
#136this is awesome
Re: Memory Allocation
#137Thank you for this, this is helpful. I wrote a JIT compiler and I didn't bother calling free much, I just let the operating system free up all allocated memory. I got into this situation often: return_struct = do_something(mystruct); return_struct->inner_struct = malloc(sizeof(struct my_inner_struct)); Now, who owns inner_struct? Who is responsible for freeing it? Do I free it when I assign to it? I feel this ownersh…
> Now, who owns inner_struct? return_struct does since it is the only thing that knows the address. > Who is responsible for freeing it? return_struct is, unless you hand that responsibility over to something else. > Do I free it when I assign to it? Yes, unless you want leaks. > I think if we could visualise memory as cars on a road, we would see obvious traffic jams. That visualisation is helpful for threads, where…
Re: Memory Allocation
#138Re: Memory Allocation
#139Earlier quoted context omitted.
Well shit. I think you're right.
Oh another thing, I'm not a fan of the premise: "As a general-purpose memory allocator, though, we can't get away with having no free implementation." I have a belief that the future of software are short-lived programs that never free memory. Programs allocate and terminate. Short-lived program communicate with each other via blocking CSP-style channels (see Reppy's Concurrent Programming in ML). If you could also e…