简体   繁体   中英

How good a memory allocator is this?

As you know mmap and malloc are non-deterministic on a system with Address Space Layout Randomization. To make my memory allocation deterministic, I use mmap to reserve a very large address space (on a 64 bit system) with no swap space, that is, using MAP_NORESERVE. Then as I require memory, I assign 10 MB of space by doing mmap with MAX_FIXED within that address space range. Therefore, the memory allocated grows linearly.

When I need to free memory, I just unmap it using use munmap . Moreover, I don't reutilize the address space which has been unmapped, but keep on allocating ahead. I guess this doesn't really affect anything as my address space (allocated with mmap with MAP_NORESERVE) is very large anyway.

Now, the question is, how good a memory allocator is this. It ofcourse isn't a very smart one, as it cannot allocate small chunks of memories, as through mmap you allocate at least 4096 bytes of memory, but I guess its still quite a workable solution. What do you think?

Also, what for the case where a process allocates memory of factor 4096 only. In that scenario, I think this approach wouldn't be inferior to malloc .

EDIT

Note that I'm talking about determinism with respect of two identical redundant processes. One is forked from another, so it gets the initial address of the mmaped region with MAP_NORESERVE, as I do fork afterwards.

To make my memory allocation deterministic

An easier solution might be simply disable ASLR .

how good a memory allocator is this.

That very much depends on your quality criteria. As the other answer points out, it's not a very good general purpose allocator. But then a general purpose allocator wouldn't normally have a requirement to be deterministic.

Presumably you have such a requirement, and possibly some other (yet unstated) requirements as well.

Since you've kept us in the dark on what you are actually trying to do , we can't tell you whether what you've done is good or not.

Not good. Sooner or later, you'll run out of virtual memory. Whether it's sooner or later depends on how much your process allocates and frees, but anyway, it surely isn't suitable for a long running daemon.

But this determinism requirement is strange. Even if your memory allocation is deterministic, it depends on the input. If the processes do something in a slightly different order, then results will differ.
And if they do everything exactly the same, how are they redundant? If one would crash, then the other will do exactly the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM