3 ms·
It's a greater degree of control than you get with most languages. For instance, in most high-level, garbage collected languages with RAII, allocations and fre
by skohan 7y ago
It's a greater degree of control than you get with most languages. For instance, in most high-level, garbage collected languages with RAII, allocations and frees happen at a fairly granular level when objects are created and destroyed, and the programmer has zero ability to ensure things like cache-friendly, contiguous memory layouts.
For things real-time applications, or even video games, this can make the difference between success or failure.
- eru 7y agoYes. Though funny enough, most implementations of malloc/free are not suitable for hard real time code. One of malloc or free usually has an unbounded worst-case runtime, because of the data structures for keeping track of free memory they keep.
- skohan 7y agoBut you could, for instance, allocate arenas at the beginning of execution and then do virtual allocations inside this for the rest of execution, no?
- stjohnswarts 7y agoI'm not sure what arenas are but you can certainly use pre allocated memory pools to bound a custom malloc/free in both time and space.
- eru 7y agoYes, you can do that. It's just not native malloc / free anymore. Of course, you need to be careful about your manual approach. Both to avoid mistakes, but also to actually achieve the performance goals you have.
- skohan 7y agoTo the original point, at least C/C++ allow you to do this type of memory management, even if it's not the 'default' strategy. It's not so clear how you would achieve something like this in Go or Java.
- eru 7y agoAgreed. I wasn't contesting that point at all, just elaborating. Most of the time for most people, the risk of arbitrarily long pauses from malloc/free is only a theoretical concern. Pauses from your typical GC are a practical concern more often. Reference counting lies in the middle. If you drop the last reference to a big part of your object graph, it can take an arbitrarily long time to clean up as well. You can bound it by either being careful how you construct (and deconstruct) your object graph at application level; or you can go for a more sophisticated real-time variant of reference counting. Google has lots of hits for 'real time reference counting'.