Click here to Skip to main content
15,908,634 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I feel so guilty Pin
Mike Hankey22-Dec-21 8:33
mveMike Hankey22-Dec-21 8:33 
GeneralRe: I feel so guilty Pin
Rick York22-Dec-21 8:35
mveRick York22-Dec-21 8:35 
GeneralRe: I feel so guilty Pin
Mike Hankey22-Dec-21 8:49
mveMike Hankey22-Dec-21 8:49 
GeneralRe: I feel so guilty Pin
honey the codewitch22-Dec-21 9:04
mvahoney the codewitch22-Dec-21 9:04 
GeneralRe: I feel so guilty Pin
Mike Hankey22-Dec-21 9:12
mveMike Hankey22-Dec-21 9:12 
GeneralRe: I feel so guilty Pin
honey the codewitch22-Dec-21 9:33
mvahoney the codewitch22-Dec-21 9:33 
GeneralRe: I feel so guilty Pin
Mike Hankey22-Dec-21 9:51
mveMike Hankey22-Dec-21 9:51 
GeneralRe: I feel so guilty Pin
trønderen22-Dec-21 11:44
trønderen22-Dec-21 11:44 
honey the codewitch wrote:
Bottom line is, it's not total RAM usage you need to care about as much as you need to care about allocations per second, vs deallocations per second - your "memory pressure"
Heap allocation/freeing has gained a reputation for being costly. It certainly doesn't have to be, especially if you have a large heap.

If you can afford 25% internal fragmentation, you can do buddy allocation in a dozen instructions or less (including call overhead) - there have been machines providing it as machine instruction. Freeing is even simpler than allocation. To reduce internal fragmentation, you can use fibonacci numbers as block sizes, rather than binary sizes, but buddy combining becomes slightly more complex.

Of course: Every now and then you run out of larger blocks and must combine buddies. But first: The limited number of block sizes means that the free lists each cover a range of sizes - e.g. any request between 257 and 512 units allocate from and free to the same list. If this free list is empty, but bigger blocks are available, cutting one in two is trivial, even if you have to go more than one level up.

Only if all higher lists are empty do you need to combine. If you want to distribute that cost over time, you combine only up to the requested size. That is likely to give you a lot of free blocks of this and smaller sizes for later requests. With binary buddies, combination isn't that costly. If you can require the heap to start at an address that is a multiple of the largest block size, you can really fine tune it, if you work at assembler or high level assembler (i.e. C or C++) level so you can really fine tune it by bit masking addresses directly to find buddies.

If you expect buddy pairing to be a fairly uncommon event (which it usually is), you will free to the head of the free list, which is super cheap. If you expect pairing to be common, you can make a trade: When freeing, scan the free list from the head until you find a larger block address. If you have to combine smaller buddies up to that size "all the time", it suggests that the free list typically is rather short, and so is the search for higher block address. Freeing becomes slightly more costly, but
it keeps the free list sorted, so you can combine all buddies of that size in a single sweep through the list.

It is possible to set up artificial test scenarios where buddy allocation causes a lot of splitting and combining (e.g. oscillating between filling the heap with minimum size blocks, freeing them and filling it with max size blocks). In a real-world load, and you are not starved on total heap size, you won't see much need for combining, and allocate/free is very fast.

The major disadvantage of buddy allocation is the internal fragmentation - for binary buddies: 25% if all block sizes are equally probable - that the probability of requesting e.g. 514 words is as high as that of requesting 512 words. In the stuff I code, 512 is a much more likely size than 514, but YMMV. If your typical allocation size is a binary size plus a small header (which really is a worst case for binary buddy!), consider fibonacci buddy.

If you have super-tight requirements for allocation latency, on the uppermost layer to combine (i.e. half the requested size, with binary buddies), you need not combine them all: Finding the first buddy pair is enough to satisfy the request. You can leave combining the remaining ones at that level for some later time.

If your application causes such a memory pressure that binary buddy allocation takes a noticeable fraction of the CPU power, then I'd like to learn about it! In an embedded environment I'd expect (internal and external) fragmentation in a tiny heap to be a far more significant issue. However: Make sure not to underestimate the internal and external fragmentation of a best or perfect fit allocation scheme! Buddy will probably be better externally and worse internally - but maybe less worse than you'd expect, compared to best/perfect fit algorithms.
GeneralRe: I feel so guilty Pin
honey the codewitch22-Dec-21 11:50
mvahoney the codewitch22-Dec-21 11:50 
GeneralRe: I feel so guilty Pin
trønderen22-Dec-21 14:04
trønderen22-Dec-21 14:04 
GeneralRe: I feel so guilty Pin
honey the codewitch22-Dec-21 14:10
mvahoney the codewitch22-Dec-21 14:10 
GeneralRe: I feel so guilty Pin
trønderen22-Dec-21 15:25
trønderen22-Dec-21 15:25 
GeneralRe: I feel so guilty Pin
OriginalGriff22-Dec-21 8:42
mveOriginalGriff22-Dec-21 8:42 
GeneralRe: I feel so guilty Pin
Mike Hankey22-Dec-21 8:51
mveMike Hankey22-Dec-21 8:51 
NewsRe: I feel so guilty Pin
oofalladeez34322-Dec-21 11:55
professionaloofalladeez34322-Dec-21 11:55 
GeneralRe: I feel so guilty Pin
Marc Clifton22-Dec-21 15:14
mvaMarc Clifton22-Dec-21 15:14 
GeneralRe: I feel so guilty Pin
Mike Hankey22-Dec-21 16:19
mveMike Hankey22-Dec-21 16:19 
GeneralRe: I feel so guilty Pin
Jacquers22-Dec-21 19:07
Jacquers22-Dec-21 19:07 
GeneralRe: I feel so guilty Pin
Mike Hankey23-Dec-21 2:23
mveMike Hankey23-Dec-21 2:23 
GeneralRe: I feel so guilty Pin
Chris C-B22-Dec-21 20:30
Chris C-B22-Dec-21 20:30 
GeneralDas 5QS mechanical keyboard - can recommend Pin
honey the codewitch22-Dec-21 7:40
mvahoney the codewitch22-Dec-21 7:40 
GeneralRe: Das 5QS mechanical keyboard - can recommend Pin
Mike Hankey22-Dec-21 8:00
mveMike Hankey22-Dec-21 8:00 
GeneralRe: Das 5QS mechanical keyboard - can recommend Pin
Kris Lantz22-Dec-21 9:04
professionalKris Lantz22-Dec-21 9:04 
GeneralRe: Das 5QS mechanical keyboard - can recommend Pin
honey the codewitch22-Dec-21 9:08
mvahoney the codewitch22-Dec-21 9:08 
GeneralRe: Das 5QS mechanical keyboard - can recommend Pin
Roger Wright22-Dec-21 10:25
professionalRoger Wright22-Dec-21 10:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.