Max
Max

Reputation: 2859

Mmap vs Static allocation for large allocations

I'm allocating a rather large, roughly 100GB, chunk of memory. The exact size is always known at compile time.

Should I be allocating statically?

static char data[DATA_SIZE];

Or using mmap?

data = mmap(NULL, DATA_SIZE, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE|MAP_LOCKED|MAP_UNINITIALIZED, -1, 0)

With the former, the application (ignoring start-up time) seems to be running marginally faster.

Ignoring failed allocations, what are the pros and cons of each approach?

Upvotes: 2

Views: 454

Answers (2)

I would use mmap or malloc, simply because the failure case is easier to handle (and you could at least give a meaningful error message). With a static data, the execve(2) of your program would fail (and the shell trying it would give a not very useful message).

However, I would also perhaps test (maybe by parsing /proc/meminfo) that the underlying system has enough memory resources.

At last, without knowing why you need so much data, it smells quite bad. Are you sure you cannot do otherwise? If you really need 100Gbytes, you can only run on very large (and costly) machines.

Don't expect the virtual memory subsystem to handle that nicely by paging. Thrashing would be so important to make the computer unusable. Or consider using madvise(2).

Unless you have access to a specialized supercomputer, it looks like a design mistake (current desktops have at most 32Gbytes).

Upvotes: 4

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215557

There's no reason whatsoever to be using mmap for this; malloc is quite capable of doing it, and will normally use mmap under the hood anyway, but using malloc makes your code simpler, easier to understand, and more portable.

As for whether a static array is preferable, maybe. The main con is that it forces you to have an actual singleton (which is bad) rather than having a single instance of a data structure that could, even if you don't need it to, exist in more than one instance. Another aspect of using a static array which may be a pro or a con, depending on your perspective, is that it moves the failure case from something you have to handle at runtime to something the invoker of your program has to handle (due to failed execve or early process termination before control reaches your process).

Upvotes: 5

Related Questions