Heap fragmentation happens when memory gets split into several small and displaced blocks. This could mean that a program has plenty of available memory overall, but there are no blocks of large enough contiguous memory to fit a data request.

Two Types

External Fragmentation:

Free spaces are scattered between allocated blocks. The total unallocated memory is sufficient for a new object, but it is divided, making continuous large allocations impossible.

Internal Fragmentation:

Memory assigned to an object is slightly larger than the requested size. The extra space within the allocated block goes unused and cannot be utilized by other objects.

Leads to OOM errors, performance degradation, and cache inefficiency.