6

It would be efficient for some purposes to allocate a huge amount of virtual space, and page in only pages that are accessed. Allocating a large amount of memory is instantaneous and does not actually grab pages:

char* p = new char[1024*1024*1024*256];

Ok, the above was wrong as pointed out because it's a 32 bit number.

I expect that new is calling malloc which calls sbrk, and that when I access a location 4Gb beyond the start, it tries to extend the task memory by that much?

Here is the full program:

#include <cstdint>
int main() {
  constexpr uint64_t GB = 1ULL << 30;
  char* p = new char[256*GB]; // allocate large block of virtual space
  p[0] = 1;
  p[1000000000] = 1;
  p[2000000000] = 1;
}

Now, I get bad_alloc when attempting to allocate the huge amount, so obviously malloc won't work.

I was under the impression that mmap would map to files, but since this is suggested I am looking into it.

Ok, so mmap seems to support allocation of big areas of virtual memory, but it requires a file descriptor. Creating huge in-memory data structures could be a win but not if they have to be backed by a file:

The following code uses mmap even though I don't like the idea of attaching to a file. I did not know what number to put in to request in virtual memory, and picked 0x800000000. mmap returns -1, so obviously I'm doing something wrong:

#include <cstdint>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>

int main() {
  constexpr uint64_t GB = 1ULL << 30;
  void *addr = (void*)0x8000000000ULL;
  int fd = creat("garbagefile.dat", 0660);
  char* p = (char*)mmap(addr, 256*GB, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  p[0] = 1;
  p[1000000000] = 1;
  p[2000000000] = 1;
  close(fd);
}

Is there any way to allocate a big chunk of virtual memory and access pages sparsely, or is this not doable?

Dov
  • 8,000
  • 8
  • 46
  • 75
  • I'm not very familiar or certain about this stuff, but I think the `mmap` system call could be used for this. – aschepler Oct 23 '19 at 13:31
  • As said by @aschepler, `mmap` is probably used. Check with `strace`. – gstukelj Oct 23 '19 at 13:32
  • 3
    Please stick to the internationally agreed SI units system - https://en.wikipedia.org/wiki/International_System_of_Units Please use **B** for **bytes** and **b** for **bits**, so one megabyte is 1MB, one gigabyte is 1GB (https://en.wikipedia.org/wiki/Gigabyte), one gigabit is 1Gb (https://en.wikipedia.org/wiki/Gigabit). Thank you. – Mark Setchell Oct 23 '19 at 13:38
  • @DanielLangr: that was the first problem. looking into mmap – Dov Oct 23 '19 at 13:38
  • 4
    @MarkSetchell According to SI unit system, G stands for 10^9. If you need to be precise, use GiB here. – Daniel Langr Oct 23 '19 at 13:41
  • @DanielLangr Yes, agreed, thank you. – Mark Setchell Oct 23 '19 at 13:50
  • @DanielLangr however, I would like to emphasise that while it probably won't hurt to be specific and use GiB, it would be foolish to assume GB to mean SI Giga in computer context. Due to deep rooted tradition, most people assume that GB == GiB, and only (yeah, this is an exaggeration :) people who use the SI definition are marketing storage or bandwidth, and do so because they get to use bigger numbers. – eerorika Oct 23 '19 at 14:52
  • @eerorika Totally agree. I just responded to Mark's comment that mentioned SI unit system. A simple comment like "use b for bits and B for bytes" would be enough. – Daniel Langr Oct 23 '19 at 14:58

2 Answers2

4

Is it possible to allocate large amount of virtual memory in linux?

Possibly. But you may need to configure it to be allowed:

The Linux kernel supports the following overcommit handling modes

0 - Heuristic overcommit handling. Obvious overcommits of address space are refused. Used for a typical system. It ensures a seriously wild allocation fails while allowing overcommit to reduce swap usage. root is allowed to allocate slightly more memory in this mode. This is the default.

1 - Always overcommit. Appropriate for some scientific applications. Classic example is code using sparse arrays and just relying on the virtual memory consisting almost entirely of zero pages.

2 - Don't overcommit. The total address space commit for the system is not permitted to exceed swap + a configurable amount (default is 50%) of physical RAM. Depending on the amount you use, in most situations this means a process will not be killed while accessing pages but will receive errors on memory allocation as appropriate.

Useful for applications that want to guarantee their memory allocations will be available in the future without having to initialize every page.

The overcommit policy is set via the sysctl `vm.overcommit_memory'.

So, if you want to allocate more virtual memory than you have physical memory, then you'd want:

# in shell
sysctl -w vm.overcommit_memory=1

RLIMIT_AS The maximum size of the process's virtual memory (address space) in bytes. This limit affects calls to brk(2), mmap(2) and mremap(2), which fail with the error ENOMEM upon exceeding this limit. Also automatic stack expansion will fail (and generate a SIGSEGV that kills the process if no alternate stack has been made available via sigaltstack(2)). Since the value is a long, on machines with a 32-bit long either this limit is at most 2 GiB, or this resource is unlimited.

So, you'd want:

setrlimit(RLIMIT_AS, {
    .rlim_cur = RLIM_INFINITY,
    .rlim_max = RLIM_INFINITY,
});

Or, if you cannot give the process permission to do this, then you can configure this persistently in /etc/security/limits.conf which will affect all processes (of a user/group).


Ok, so mmap seems to support ... but it requires a file descriptor. ... could be a win but not if they have to be backed by a file ... I don't like the idea of attaching to a file

You don't need to use a file backed mmap. There's MAP_ANONYMOUS for that.

I did not know what number to put in to request

Then use null. Example:

mmap(nullptr, 256*GB, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0)

That said, if you've configured the system as described, then new should work just as well as mmap. It'll probably use malloc which will probably use mmap for large allocations like this.


Bonus hint: You may benefit from taking advantage of using HugeTLB Pages.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Great answer! new/malloc does in fact work, but I would much rather not have to set a system-wide policy of overcommitting memory. Is there any way to override it just for my process? – Dov Oct 23 '19 at 15:26
  • @Dov as far as I know, the setting can only be changed kernel-wide. – eerorika Oct 23 '19 at 15:35
  • Perhaps the task set the parameter, allocate, then set it back? Kind of kludgy – Dov Oct 23 '19 at 15:38
3

The value of 256*GB does not fit into a range of 32-bit integer type. Try uint64_t as a type of GB:

constexpr uint64_t GB = 1024*1024*1024;

or, alternatively, force 64-bit multiplication:

char* p = new char[256ULL * GB];

OT: I would prefer this definition of GB:

constexpr uint64_t GB = 1ULL << 30;

As for the virtual memory limit, see this answer.

Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
  • @Dov I added a link to an answer that explains how virtual address space is limited. Basically, RAM size, swap size, and `ulimit` all play role here. I tried a system with 1.5 TB RAM and I was able to allocate 1 TB, but not 2 TB. `ulimit` set to unlimited. – Daniel Langr Oct 23 '19 at 13:51
  • 2
    @DanielLangr Point is that it can be, where as `ULL` will always be at least 64 bit. Your answer is all about fitting numbers into the correct amount of bits after all. – Paul Evans Oct 23 '19 at 14:01
  • 1
    Gotta agree with Paul that `ULL` is the correct choice: http://eel.is/c++draft/basic.fundamental#tab:basic.fundamental.width (although "`UL` is just `unsigned int` usually" is a dubious statement). – Max Langhof Oct 23 '19 at 14:06
  • @DanielLangr, Given that I want to access much less physical memory, this seems like a ridiculous limit. Does mmap not work this way? I am looking for a call like mmap but not with files. Surely I can memory map a file bigger than my swap space using mmap? – Dov Oct 23 '19 at 14:14