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 gives the guaranteed that allocated memory would be aligned with the object. 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 are 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.

Consider a scenario where the 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 is larger than a single block of memory. 

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

Whenever we called 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.

Let see an example program to understand the internal fragmentation,

In the below code, a programmer may think that the system will allocate 8 *100 (800) bytes of memory but due to bookkeeping, (if bookkeeping bytes is 8) system will allocate 8*100 extra bytes. You can see how internal fragmentation is waisting the heap memory. This is only for understanding actual behavior is the implementation-dependent.

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)

How is free work?... >>
<< What is the memory leak in C?...