3 ms·
If you need to allocate lots of memory, you should be using mmap(). If you need it faulted in, you use MAP_POPULATE. Try to use MAP_HUGETLB to economize on TLB
by ncmncm 7y ago
If you need to allocate lots of memory, you should be using mmap(). If you need it faulted in, you use MAP_POPULATE. Try to use MAP_HUGETLB to economize on TLB cache entries; you may need to set "vm.nr_hugepages=16384" (or something) in /etc/sysctl.conf (or someplace) to reserve them.
If you are allocating lots of memory and don't know about hugepages, you badly need to learn.
If your program has threads, unmapping memory is generally to be avoided.
- PaulDavisThe1st 7y agommap is an operating system interface, or at the very least a part of POSIX. it isn't part of the C++ standard, and so you cannot rely on it being present on systems that don't claim extensive POSIX compatibility. As for MAP_POPULATE and MAP_HUGETLB, try using that on a POSIX-ish OS that isn't Linux and see how far you get.
- ncmncm 7y agoWhat, are there any left? Every OS nowadays has mmap, by some spelling, and they all support hugepages, likewise. A program that doesn't use its host OS doesn't, generally, do much.
- saagarjha 7y ago> Every OS nowadays has mmap, by some spelling I mean, I'm fairly sure that's the problem. (Plus a lot of systems support C++ but not virtual memory…)
- IcePic 7y agoJust to take one (small) example, the Arduino IDE is C++, and you will not mmap() a lot there.
- ncmncm 7y agoYou also won't new[] a lot there. How much can it even physically address?
- alehander42 7y agobut often you might write code that you dont know it will end up there, like lib
- IshKebab 7y agoActually Arduino code does tend to use dynamic allocation for things like strings - it's a lot easier than the alternative, they tend to have quite a lot of memory these days (e.g. 1 MB) and hobby projects don't need to be super-reliable usually.
- ncmncm 7y agoOne megabyte or ten isn't enough to bother with mapping, and on Arduino you don't typically have memory mapping hardware anyway. "Big" means gigabytes these days, sometimes hundreds of them.
- dekhn 7y agoI've actually used an STL port to Arduino and was able to create a standard vector of length three but not four. that crashes the system. I believe it uses a custom allocator.
- PaulDavisThe1st 7y agoPerhaps you've not come across the idea of cross-platform development? I have a large fairly successful cross-platform program that does a hell of a lot. All of our interactions with the OS are wrapped, either by libstdc++ or by the portability library that we use. We would never allow a direct call to mmap() from the main application code ... we don't even allow direct calls to most of the POSIX API since we have to run on Windows too (without WSL or whatever MS' current POSIX layer is). "By some spelling" is precisely why almost all programmers are better off using "new X[N]" than mmap, unless you actually think that code like this is good: #ifdef __APPLE__ #define LOCAL_HUGETLB 0 #else #define LOCAL_HUGETLB MMAP_HUGETLB #endif ... mmap (..., foo|bar|LOCAL_HUGETLB) This cannot be a serious suggestion regarding the recommended way to allocate memory.
- usefulcat 7y ago> If your program has threads, unmapping memory is generally to be avoided. Could you expand on this? I recently encountered a situation where allocating and freeing many large allocations using mmap() seemed to eventually cause problems with thread creation, but I had assumed that it was probably because the virtual address space had become too fragmented, which of course would not solely be a result of unmapping. Or maybe that fragmentation is what you're referring to, and I'm just reading too much into that sentence.
- ncmncm 7y agoIt just creates long stalls as the TLBs of all cores that threads in the process run on are flushed and must be reloaded from backing RAM.
- wbkang 7y agoMany c stdlib will fallback to mmap above a certain threshold :)