6 ms·
How to Allocate Memory (2016)
- asveikau 7y agoUsing sbrk(2) as an application stack seems like a bad idea. A lot of times that is where malloc gets its heap addresses from. The article even says this but not strongly enough: > It is worth mentioning that the C library malloc might be using and caching the results of sbrk it may be unwise to mix this trick with your C library malloc, but it is very useful in new programs that you have no intention of using the C library malloc with. No it is not a good idea to write "new programs" to apply hacky uses of sbrk(2) and conflict with malloc. Any use of sbrk(2) outside of a malloc implementation is pretty suspect as code smell. And you will be hard pressed to find any program or library that doesn't use malloc, short of embedded use cases.
- ShroudedNight 7y agoThe strange thing [to me] is that, in a discussion of stack allocation patterns, sbrk is a deeply confusing red-herring. 'Little-s' stacks require bulk-allocated pages that are then carved up into object allocations as required, and de-allocated in last-in-first-out ordering. No alloca or sbrk required.
- wruza 7y agoNot only malloc, but the entire libc, as no functions in it explicitly guarantee that they do not call malloc and/or mess “your” brk range. You can guess it by what it does and ENOMEM in possible return status, but that is a stupid idea. sbrk is basically a remnant api and is now either an implementation detail of libc memory management or a fool’s crutch [1]. [1] https://opensource.apple.com/source/Libc/Libc-1272.250.1/emulated/brk.c.auto.html https://opensource.apple.com/source/Libc/Libc-1272.250.1/emu...
- asveikau 7y agoYes. Notably stdio typically uses the heap. So I hope this author isn't planning to use printf for New Programs.
- geocar 7y agoI don't, at least not when I care about performance. stdio is a big unknown with highly variable performance across platforms and libc-versions.
- smabie 7y agoSure, yeah but it’s not like using printf is a problem for error messages or logging. Using write() for that kind of stuff is more hassle than it’s worth.
- geocar 7y agoOh it so is! I can’t count the number of times I’ve gotten 20-50x speed ups just turning off logging. Assuming printf is free (or even cheap) is the sort of thing that’ll get you into trouble. It might not always matter, but when it does if your application is parsing strings at runtime at high volume, it’s going to hurt! I rarely log messages anyway, preferring to set state that I can then printout on command (e.g. siginfo)
- jstimpfle 7y agoSo did you just turn logging off, or did you in fact write a better custom implementation? And how much faster did it go? Did you actually measure that? What kind of system was that on? My first thought is, probably there was an fsync() or similar after logging each message. Also, printf() and friends have to lock the streams, so there might be some contention somewhere in case you're multi-threaded. That's not a problem that you can solve without some custom routing of logging messages. Using printf, you can easily write dozens of megabytes of the most complicated formatting code on contemporary systems (I'm just pulling a number out of thin air here). More than any system should want to log.
- geocar 7y agoPeople who deal in megabytes have different problems than people who deal in petabytes. Usually just turn it off. At least at first. Note I’m talking diagnostic here; If I need the log messages obviously I have to write a custom streaming system even if the system printf is fast enough because the API complicates recovery. Printf can also vary in performance by 10msec or more based on the internal state. That’s not good enough since my entire application has a hard limit of around 30msec. I can’t even do one printf — even every N messages (for any N) because I’ll never catch up.
- Reelin 7y agoIt seems like sbrk is a bad idea because it's a legacy API that manipulates global program state in a very error prone manner. Is there ever any reason to use it instead of mmap? More generally, I was very confused by the entire stack section. Why sbrk instead of mmap? Why should C provide special functions for creating or using a stack? Isn't a stack just a region of memory and a pointer into it? Isn't their high performance entirely due to data access patterns that facilitate cache locality while minimizing instruction count and pointer dereferences? I may well have some misconceptions, but if so this article did nothing to address them. Edit: Aha! From the mmap man page: > MAP_STACK (since Linux 2.6.27) > Allocate the mapping at an address suitable for a process or thread stack. > This flag is currently a no-op on Linux. However, by employing this flag, applications can ensure that they transparently obtain support if the flag is implemented in the future. ... some architectures may (later) require special treatment for stack allocations. ...
- Lt_Riza_Hawkeye 7y agoDon't forget MAP_GROWSDOWN > MAP_GROWSDOWN > This flag is used for stacks. It indicates to the kernel virtual memory system that the mapping should extend downward in memory. The return address is one page lower than the memory area that is actually created in the process's virtual address space. Touching an address in the "guard" page below the mapping will cause the mapping to grow by a page. This growth can be repeated until the mapping grows to within a page of the high end of the next lower mapping, at which point touching the "guard" page will result in a SIGSEGV signal.
- ShroudedNight 7y agoI don't like this article. It comes across as being written with the intent of highlighting the elevation the author's status as a 'low-level wizard', while conveying as little as possible to the ignorant reader. From the scattered aspects of memory management it covers to it uses of unexplained magic numbers in its code examples, it comes across more as an unsolicited secret handshake than cogent, empathetic attempt to elevate others in the practice. Disclaimer: I had a hard time following this article. I'm also (ostensibly anyway) not the target audience. I was at one point the primary person responsible for the memory management in the JIT compiler[1] used by OpenJ9, though I haven't done that for a couple of years now. [1] That is, the memory used by the JIT compiler itself, not its interactions with the garbage collector.
- SlowRobotAhead 7y agoI am the target audience. I get exactly what he’s going for but found a different gripe. I really dislike his coding style! It’s written to be “short” in favor of clear. I see what the code does, but not necessarily his intent. void*page_alloc(unsigned long long bytes) { void *x; for(;;) { x = mmap(0, bytes, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); if(x != MAP_FAILED) return x; out_of_memory(); } } For embedded I stoped using “unsigned long long” in favor of uint64_t. He spends all those extra characters but can’t put a space in the void pointer function definition?
- EdSchouten 7y agoIn this case it also makes most sense to use size_t, right?
- SlowRobotAhead 7y agoDepends. If you use size_t all the time, you may forget or have someone else forget that another system isn’t the same. If you always use uint8_t or int64_t etc there is no ambiguity. If I intended this to be an 32bit var, it will be regardless if who is compiling it.
- saagarjha 7y ago> One major limitation of malloc (and even the best implementations like jemalloc and dlmalloc) is that they try to use a single allocator for each data structure. I’m not sure what this is supposed to mean, but it’s good to note that malloc will switch between different paths based on the allocation size and what memory it has available to service the request.
- geocar 7y agomalloc() has incomplete information. It doesn't know that this allocation of 0 bytes is soon going to balloon to a few GB in a future realloc(), so if it starts in brk, it'll mmap later and memcpy the data over. The good news is that the caller knows this. By saying "I want a sizeof(Cons) object" versus "I want a buffer that's going to get big and used for a long time" versus "I want a buffer that'll get big but go away right after" means you get to avoid these unnecessary operations. That's important because they really add up when dealing with lots of data. That's how replacing a complicated and intelligent malloc() which has to rely on heuristics to decide which of the "different paths" to follow can lose big to a simpler strategy.
- Koshkin 7y agoAbstractions are leaky - much more often than one would like to think. On modern 64-bit systems, for example, with the apparently "limitless" virtual memory one has to pay close attention not only to the allocation patterns but also to the access patterns: random reads and writes from/to a huge array may lead to a significant drop in performance.
- zerotonine 7y agoCan anyone think of a use case for creating a second stack, without a thread, to allocate memory? The approach I have in mind: 1) Create a new stack POSIX makecontext() w/ initializer thread. 2) Initializer thread makes function calls to allocate objects. Functions are called in sequence and never return. The thread eventually reaches the end of its initialization sequence and stalls. 3) Use swapcontext() to return to the original thread from #1. 4) Original thread may reference all objects allocated on the stack of the other context. This seems like a widget that could be used to accomplish something interesting, but I'm not sure what that might be.
- geocar 7y agoA forth interpreter is probably the simplest and most-common example of a two-stack system someone might be familiar with. Sometimes I'd like to push an event onto a queue from an interrupt (e.g. a signal handler) and the buffer size can balloon some; sbrk() might be used because it's faster than malloc() and I know the consumer is fast enough (even though it's currently busy). Sometimes I know the memory usage is very temporary, but I don't want to contort my program to use alloca() and so again: using brk can be convenient. There are other things that are stack-like that we don't often think of as stacks. One interesting place might be an undo-stack: An editor typically has an undo and redo-button and will never be freeing this state anyway, so rather than malloc/realloc my state (and have extra copies going on), and without trashing my heap with a long linked list, I might prefer the performance of sbrk(). You could possibly use alloca() as well, but this would make redo very difficult. Another might be in a consensus algorithm: If implementing raft you wanted to queue messages until you had a chance for your election, your "stack" might take several seconds before you can start processing it. Keeping this on the "other" stack would make it difficult to do any other processing (where's the event loop?), and using malloc/realloc() will introduce copies. Reserving sbrk() for this stage might make everything much more straightforward.
- jstimpfle 7y ago> One interesting place might be an undo-stack: [..] so rather than malloc/realloc my state (and have extra copies going on), and without trashing my heap with a long linked list, I might prefer the performance of sbrk(). You can implement a stack-like thing that avoids copying, and even gives stable addresses, by linking fixed-size chunks (say 128 elements) in a list. A stack "pointer" is then a pointer to a chunk + offset. That said, editor/undo redo items are not performance critical. And you will be hard pressed to find a reason why you need to optimize memory consumption. Assuming a memory overhead of 16 bytes per allocation and assuming that a human would make < 1M edits. Preferring an obscure API like sbrk() instead is not good general advice. It's not portable and it precludes the use of basically any library including libc.