Q:

What is dynamic memory fragmentation?

0

What is dynamic memory fragmentation?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

The memory management function is guaranteed that if memory is allocated, then it would be suitably aligned to any object which has the fundamental alignment. The fundamental alignment is less than or equal to the largest alignment that’s supported by the implementation without an alignment specification.

One of the major problems with dynamic memory allocation is fragmentation, basically, fragmentation occurred when the user does not use the memory efficiently. There are two types of fragmentation, external fragmentation, and internal fragmentation.

The external fragmentation is due to the small free blocks of memory (small memory hole) that is available on the free list but the program not able to use it. There are different types of free list allocation algorithms that used the free memory block efficiently.

To understand the external fragmentation, consider a scenario where a program has 3 contiguous blocks of memory and the user frees the middle block of memory. In that scenario, you will not get a memory, if the required block of memory is larger than a single block of memory (but smaller or equal to the aggregate of the block of memory).

The internal fragmentation is wasted of memory that is allocated for rounding up the allocated memory and in bookkeeping (infrastructure), the bookkeeping is used to keep the information of the allocated memory.

Whenever we called the malloc function then it reserves some extra bytes (depend on implementation and system) for bookkeeping. This extra byte is reserved for each call of malloc and becomes a cause of the internal fragmentation.

For example,
See the below code, the programmer may think that the system will be allocated 8 *100 (800) bytes of memory but due to bookkeeping (if 8 bytes) system will be allocated 8*100 extra bytes. This is an internal fragmentation, where 50% of the heap waste.

char *acBuffer[100];
int main()
{
    int iLoop = 0;
    while(iLoop < 100)
    {
        acBuffer[iLoop ] =  malloc(8);
        ++iLoop;
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Embedded C interview questions and answers (2022)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How is the free work in C?... >>
<< What is the return value of malloc (0)?...