Junior — Middle
Can you explain the concept of memory fragmentation in dynamic memory?
sobes.tech AI
Answer from AI
Memory fragmentation is a situation where free dynamic memory is broken into many small blocks that cannot be efficiently used for new allocations.
There are two types of fragmentation:
- Internal fragmentation: when the allocated memory block is larger than needed by the program, and some part remains unused.
- External fragmentation: when free memory is broken into small non-contiguous segments, and even if the total free memory is sufficient, it is impossible to allocate a large contiguous block.
For example, frequent allocation and deallocation of blocks of different sizes can create "holes" in the heap, which prevent subsequent memory requests from being satisfied, even though there is enough free memory.