Dynamic Memory Management/ What is compaction fragmentation?/ What is the difference between compaction and fragmentation?

Dynamic memory management is the memory which is given instantly to a program variable or data structure when it is needed. Here we are explaining this dynamic memory management phenomena with reference to the data structure.

Inside this blog post, we will explore What do you mean by dynamic memory management? What is static and dynamic memory? What is the Dynamic Memory Allocation example? What are dynamic memory allocation and its types? What is compaction in memory management? What is the difference between compaction and fragmentation?

What do you mean by dynamic memory management?

There are many occasions while we don’t know about the memory needed by the program or program variable before compiling the program and running the program.

So this dynamic memory management helps us to handle the situation at compile time or static memory allocation. And it is the best help for us when we need a chunk of memory at run time of the program. In the most real-life problem, we need dynamic memory management.

In the multiprocessing computer environment, several programs reside at memory at the same time. a different program has different memory requirements.

For example, one program may require 50k of memory another require 100k of memory and yet another requires 200k of memory. Whenever there is a requirement by the operating system then it should be able to provide the contiguous block of memory of the right size.

When the execution process for a program is completed then it releases the memory and frees the block allocated to it and this freed block may be allocated to another program which needs it.

In the dynamic processing environment, the required memory is not known before the time of actual execution. Moreover, blocks of memory will, in general, be freed in some order different from that in which they are allocated.

At the start of the computer system, no jobs are in memory, so the whole memory says size M words are available for allocating to the programs. Now the jobs are submitted to the computer and the request is made for variable-size blocks of memory.

A large contiguous amount of storage may be required by a particular program such as a large array. So it would be impractical to entertain such a large block at a time, similarly, a program may require a storage block in a large variety of sizes. In such cases, a dynamic memory management system must be able to process requests for a variable length of a block.

For example, consider a small memory of 1024 words, suppose a request is made for three blocks of storage that are 348, 110, and 212 words. it is assumed that these blocks are allocated sequentially as shown in Fig1 below.

dynamicmemory-managementnew

Now suppose the second block of size 110 is freed, resulting in the situation shown in Fig2. Now the 464 words of free space are available in noncontiguous blocks. Hence the request for 400 words can not be satisfied.

Suppose that block 3 is now freed clearly, it is not desirable to retain three blocks of 110, 212, and 354 words. Rather the blocks should be combined into a single large block of 676 words so that the further large request can be satisfied as shown in Fig3.

What is static and dynamic memory?

Static memory is a form of pre-declared memory which is assigned to the variable at compile time of a program. And dynamic memory is allocated at the time of program execution and also known as run-time memory allocation.

What is the Dynamic Memory Allocation example?

Sometimes the declared size of array or memory is not sufficient at the run time then if a program or process requests for any additional memory. Like in C/C++ program we assign this memory at a run time manually with the use of some functions like MALLOC, CALLOC, RE-ALLOC, and FREE().

If we talk in the context of data structure then when a program requests a memory at run time then OS provides a block of main memory and then the program uses this memory for execution and once it is completed then return the memory back for other programs.

What is compaction in memory management?

Compaction is a process where every empty and unused memory blocks are collected together to form a large memory block which can be assigned to a process or program to execute successfully.

Compaction solves the problem of fragmentation where small unused memory blocks are left idle. A single block with some left memory may not sufficient to fulfill the memory requirement of a program or process.

What is the difference between compaction and fragmentation?

As we have known that there are two types of fragmentation, one is internal fragmentation and other one is external fragmentation.

Internal fragmentation occurs when the process requires either less or more memory than the assigned fixed size memory block.

External fragmentation occur when we can not assigned any memory block to the process as we have varying memory block which does not suits the process to fulfil the request.

To solve the problem of external fragmentation we use memory compaction and we collect the memory of all unused blocks and make a large memory block and then assigned it to a requested and best-suited process.

Conclusion

Thus this example of dynamic memory management shows us the necessity to keep the track of available space, allocate portions of that space when allocation requests are made, and to combine contiguous free spaces when a block is freed. If memory is free then it should be in a chunk so that it can be used by the program needed a large block of memory.

What do you mean by dynamic memory management? What is static and dynamic memory? What is Dynamic Memory Allocation example? What is dynamic memory allocation and its types? What is compaction in memory management?…….

….What is compaction technique? What is compaction in computer? What is dynamic memory fragmentation? Inside this blog post we have gone through.

In the case of any queries, you can write to us at [email protected] we will get back to you ASAP.

Hope! you would have enjoyed this post about dynamic memory management.

Please feel free to give your important feedbacks in the comment section below.

Have a great time! Sayonara!

Anurag

I am a blogger by passion, a software engineer by profession, a singer by consideration and rest of things that I do is for my destination.