3 ms·
No, it definately can't. In C one would usually store pointers into memory blocks, which it is impossible to "fix up". What you can do is search through all al
by CJefferson 7y ago
No, it definately can't. In C one would usually store pointers into memory blocks, which it is impossible to "fix up".
What you can do is search through all allocated memory for full 4K pages which are unused, which can be "given back" to the OS.
However, it's unclear how much benefit there is from doing this. If you give back memory too eagerly, and then need to request more later, you end up thrashing the kernel. Assuming you have some swap space, this unused memory gets swapped out.
It could be (hard to tell, but I do this in my own memory managers) that the memory is technically still assigned to the program, but the pages are marked as unused, in which point there is no "memory" there at all, just address space which you can start using again later. However, it can still look like my program is using something crazy like 1TB of memory.
- chrisseaton 7y ago> No, it definately can't. Glibc malloc can return memory to the OS. This is what the trim that people are talking about does.
- scott_s 7y agoI got tripped up on that statement, too. The problem is that tinus_hn's parent comment started with a false premise: that the only way to return memory to the OS is by moving memory around, which is not the case. I think CJefferson was saying that manual memory allocators can't move memory around (which in the general case is true, but you point out in a different thread there are cases when it can), because the rest of their comment is explaining how they can return memory.
- CJefferson 7y agoSorry, I should have been clear I was specifically replying to suggestion that glibc (or any malloc, by which I mean C standard library memory manager) could move memory allocations.
- chrisseaton 7y ago> specifically replying to suggestion that glibc (or any malloc, by which I mean C standard library memory manager) could move memory allocations But I think malloc can move memory allocations - it can move objects out of a page and return it to the kernel by copying objects into an existing page with a gap and then remapping the old addresses onto the copied page. Then the memory has moved but retains the same address and the old page is returned. See for example the Mesh allocator which does this and still manages to expose a conventional malloc interface. https://github.com/plasma-umass/Mesh https://github.com/plasma-umass/Mesh
- CJefferson 7y agoOK, mesh is doing something very cool and unusual. I've never seen that before (in my defense, it is a 2019 academic paper). It still has its limits, as it can only merge allocations which don't overlap modulo 4096.