You are on page 1of 3

4/13/2016 What 

are the first fit, next fit and best fit algorithms for memory management? ­ Quora

What are the first fit, next fit and best fit algorithms for memory
management?
It would be really helpful if you could explain these with examples. I cannot get a satisfactory explanation
for these algorithms online. Thank you.

2 Answers

Varun Agrawal, Progamming Centurion
33.5k Views

Imagine you in charge of a movie theater with seats of varying sizes (Main memory blocks). Some
seats are extra small so that only really skinny people can sit in them, while some are really big for
people of larger sizes. There are a bunch of people (Processes) waiting outside the theater hall
waiting to get seated for the one of the many screenings (Job) of a very famous documentary film.
The people may watch the documentary for any arbitrary amount of time (Turnaround time)
before getting up and leaving. It is up to you to get the people to seats they will be comfortable in.

Here is the catch: There is no seat number on the movie tickets since you don't want to be unfair by
giving a small seat to a large person and no reservations  are allowed, (again being fair to everyone).
How will you do this?

1. First Fit: You measure/look at each person's size and then go sequentially through all the seats
looking for the first seat that the person can sit in. You always start from the first seat as you don't
want to take any chances by missing a valid seat, You then allocate them that seat. You don't care if
the seat is a bit larger than them since you just want to allocate a seat that they will be comfortable in,
even though you are wasting some space. In case there is no seat of the right size available (there may
be a few small seats available but the person is extra large), you simply wait till someone seated in an
appropriate seat gets up and leaves and then you give that seat to the person waiting, or you look at
the next person in line and see if they have a seat available for them.

2. Next Fit: You do the same thing as above, but after allotting a seat to someone, you start your
search for the next seat from the last seat you allotted rather than the start. This saves you from
looking at seats you already allotted at the beginning and forces you to look at the back seats sooner
than before, thus you are able to do a faster job at allotting seats to various people. You just have to
keep in mind the last seat you allotted to a person.

3. Best Fit: Again, you are allotting seats to people based on their size in a similar way as above, but
this time you take the extra effort to make sure the seat you are allotting someone is the best possible
seat for them. You ensure that there is minimum wastage of space when the person sits in his seat.
This may require you to skip a large enough seat for one later that is a better fit to the person in
question, but you ensure that the person fits snugly into his seat. Also, this may take you more time
since you are looking at more seats trying to find the best fitting seat for the current person, even

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22header%22%20style%3D%22margin­bottom%3A%2016px%3B%20color%3A%20rgb(51%2C%2051%… 1/3
4/13/2016 What are the first fit, next fit and best fit algorithms for memory management? ­ Quora

though you may have found a large enough seat already.

Now depending on the situation and the kind of service and efficiency (time vs. space) you want to
provide, you can go for any of the above algorithms.

Hope I have cleared your doubts satisfactorily. :)
Written 16 Oct 2013 • View Upvotes

Related Questions
More Answers Below

How does first fit policy works?

Is there any book which has a complete description of the first fit algorithm implemented in Ubuntu?

In memory management, while using the best­fit, first­fit and worst­fit memory allocation methods for
fixed partitioning, is only one process ...

Given memory partitions of 100k, 500k, 200k, 300k, and 600k (in order), apply first fit and best fit
algorithms to place processes with the sp...

In the bin packaging problem (BPP), why is first fit decreasing (FFD) more efficient than first fit
increasing (FFI)?

Yu Feng, Who codes.
33.1k Views • Upvoted by Paul Olaru, Learning programming in high school

These are different kinds of memory management policies.

Let's first assume the memory is partitioned into parts of fixed size.

The first fit policy says that when a memory allocation of P parts is launched, the algorithm scans the
memory and finds the first portion of free parts that is larger or equal to P in size. It is a straight and
fast algorithm, but tends to cut large portion of free parts into small pieces, causing allocations that
need a large portion of parts to fail even the total number of free parts exceeds the number requested,
which is so called internal fragmentation problem.

The best fit policy says that to fullfill a memory allocation request of P parts, it scans the memory and
find a best fit of free parts for the requested size P instead of just returning the first part that it finds
being larger or equal to P in size. It by doing so tries to avoid the internal fragementation problem.

Another problem of first fit is that it tends to allocate memory parts at the begining of the memory,
which may leads to more internal fragements at the begining. Next fit tries to address this problem by
starting search for the free portion of parts not from the start of the memory, but from where it ends
last time. Also, this algorithm returns the first portion in its search path that satisfies the request and
data:text/html;charset=utf­8,%3Cdiv%20class%3D%22header%22%20style%3D%22margin­bottom%3A%2016px%3B%20color%3A%20rgb(51%2C%2051%… 2/3
4/13/2016 What are the first fit, next fit and best fit algorithms for memory management? ­ Quora

is as fast as first fit.

Now comes the example, say after some time of execution, the memory has a layout as this:

And now, an allocation request R1 of 2 parts comes, followed by another request R2 which asks for 3
parts.

Fig1: The initial memory layout

+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
|  USED      |          3 parts free              |    USED    |     2 parts free       |
+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+

To serve R1, the first fit algorithm starts its scan from the begining, and finds a 3­part free portion
which satisfies R1, and this portion is returned to R1, giving a layout of:

Fig2: The memory layout after first­fit serves R1

+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+       
|  USED      |  USED BY R1           |1 parts free|    USED    |     2 parts free       |       
+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+   

                                                           

When R2 comes, first fit would fail to fullfill it even the total number of free parts is 3. But with best
fit, it would serve R1 with the 2­part portion, giving a layout of:

Fig3: The memory layout after best­fit serves R1

+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+
|  USED      |          3 parts free              |    USED    |     USED BY R1         |
+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐+

When R2 comes, best fit can allocate the 3­part portion to it.

Modern OS does not simple adopt any of these algorithm. The memory management system of Linux,
for example, is a mix of virtual memory, paging and buddy sys

data:text/html;charset=utf­8,%3Cdiv%20class%3D%22header%22%20style%3D%22margin­bottom%3A%2016px%3B%20color%3A%20rgb(51%2C%2051%… 3/3

You might also like