ArticleslgStudy

computer science

Buddy memory allocation

Buddy memory allocation is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Buddy memory allocation rather than just read about it. In short: The buddy memory allocation technique is a memory allocation algorithm that divides memory into partitions to try to satisfy a memory request as suitably as possible. This system makes use of splitting memory into halves to try to give a best fit.

Key takeaways

  • Buddy memory allocation belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Buddy memory allocation to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Buddy memory allocation from memory before moving on to harder problems.

Reference excerpt

The buddy memory allocation technique is a memory allocation algorithm that divides memory into partitions to try to satisfy a memory request as suitably as possible. This system makes use of splitting memory into halves to try to give a best fit. According to Donald Knuth, the buddy system was invented in 1963 by Harry Markowitz, and was first described by Kenneth C. Knowlton (published 1965). The Buddy memory allocation is relatively easy to implement. It supports limited but efficient splitting and coalescing of memory blocks.

Algorithm There are various forms of the buddy system; those in which each block is subdivided into two smaller blocks are the simplest and most common variety. Every memory block in this system has an order, where the order is an integer ranging from 0 to a specified upper limit. The size of a block of order n is proportional to 2n, so that the blocks are exactly twice the size of blocks that are one order lower. Power-of-two block sizes make address computation simple, because all buddies are aligned on memory address boundaries that are powers of two. When a larger block is split, it is divided into two smaller blocks, and each smaller block becomes a unique buddy to the other. A split block can only be merged with its unique buddy block, which then reforms the larger block they were split from. Starting off, the size of the smallest possible block is determined, i.e. the smallest memory block that can be allocated. If no lower limit existed at all (e.g., bit-sized allocations were possible), there would be a lot of memory and computational overhead for the system to keep track of which parts of the memory are allocated and unallocated. However, a rather low limit may be desirable, so that the average memory waste per allocation (concerning allocations that are, in size, not multiples of the smallest block) is minimized. Typically the lower limit would be small enough to minimize the average wasted space per allocation, but large enough to avoid excessive overhead. The smallest block size is then taken as the size of an order-0 block, so that all higher orders are expressed as power-of-two multiples of this size. The programmer then has to decide on, or to write code to obtain, the highest possible order that can fit in the remaining available memory space. Since the total available memory in a given computer system may not be a power-of-two multiple of the minimum block size, the largest block size may not span the entire memory of the system. For instance, if the system had 2000 K of physical memory and the order-0 block size was 4 K, the upper limit on the order would be 8, since an order-8 block (256 order-0 blocks, 1024 K) is the biggest block that will fit in memory. Consequently, it is impossible to allocate the entire physical memory in a single chunk; the remaining 976 K of memory would have to be allocated in smaller blocks.

Example The following is an example of what happens when a program makes requests for memory. Assume that in this system, the smallest possible block is 64 kilobytes in size, and the upper limit for the order is 4, which results in a largest possible allocatable block, 24 times 64 K = 1024 K in size. The following shows a possible state of the system after various memory requests.

This allocation could have occurred in the following manner

The initial situation. Program A requests memory 34 K, order 0. No order 0 blocks are available, so an order 4 block is split, creating two order 3 blocks. Still no order 0 blocks available, so the first order 3 block is split, creating two order 2 blocks. Still no order 0 blocks available, so the first order 2 block is split, creating two order 1 blocks. Still no order 0 blocks available, so the first order 1 block is split, creating two order 0 blocks. Now an order 0 block is available, so it is allocated to A. Program B requests memory 66 K, order 1. An order 1 block is available, so it is allocated to B. Program C requests memory 35 K, order 0. An order 0 block is available, so it is allocated to C. Program D requests memory 68 K, order 1. No order 1 blocks are available, so an order 2 block is split, creating two order 1 blocks. Now an order 1 block is available, so it is allocated to D. Program B releases its memory, freeing one order 1 block. Program D releases its memory. One order 1 block is freed. Since the buddy block of the newly freed block is also free, the two are merged into one order 2 block. Program A releases its memory, freeing one order 0 block. Program C releases its memory. One order 0 block is freed. Since the buddy block of the newly freed block is also free, the two are merged into one order 1 block. Since the buddy block of the newly formed order 1 block is also free, the two are merged into one order 2 block. Since the buddy block of the newly formed order 2 block is also free, the two are merged into one order 3 block. Since the buddy block of the newly formed order 3 block is also free, the two are merged into one order 4 block. As you can see, what happens when a memory request is made is as follows:

If memory is to be allocated Search for an exact match: Look for a free memory slot of exactly that target 2k size. If found: Allocate it to the program immediately. If not found: The system looks up the chain for the next available larger free power-of-two block (e.g.,2k+1,2k+2, etc.). Split the larger block: Once a larger free block is found, the system executes a loop Split the larger block in half into two equal "buddies." If the resulting halves are still larger than your target 2k size, keep one half and split the other half again. Repeat this splitting process until you have successfully created a block that matches your target 2k size. Allocate: Allocate one of the newly created target-sized blocks to the program and leave its "buddy" in the free list. If memory is to be freed A buddy should only be tied with the true buddy where it got divided with. It cannot be tied with a neighboring block (Next to a buddy) not associated with the buddy prior to splitting. Wait till the program is done with the block currently assigned with the program. If buddy A is now free check buddy B if free then combine the two into one unified single block. Check if the 64kb combined block from Buddy A & Buddy B is free, repeat the process upward.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Buddy memory allocation

Start with the simplest possible case. Write down what Buddy memory allocation claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Buddy memory allocation before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Buddy memory allocation ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Buddy memory allocation

In research
Buddy memory allocation appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Buddy memory allocation in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Buddy memory allocation is common in secondary-school and first-year university syllabi. It links to neighbouring topics Memory management algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Buddy memory allocation outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Buddy memory allocation” →

How to study Buddy memory allocation in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Buddy memory allocation means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Buddy memory allocation out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Buddy memory allocation in simple terms?

The buddy memory allocation technique is a memory allocation algorithm that divides memory into partitions to try to satisfy a memory request as suitably as possible. This system makes use of splitting memory into halves to try to give a best fit.

Why does Buddy memory allocation matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Buddy memory allocation?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Buddy memory allocation.

Tags

  • Memory management algorithms

Keep exploring