Skip to content

Latest commit

 

History

History
36 lines (26 loc) · 4.12 KB

6_heap_requirement.md

File metadata and controls

36 lines (26 loc) · 4.12 KB

Heap Memory Management Requirement 📘

  • Heap Memory Segment

    • It's a contiguous block of memory that represents the process's virtual address space.
  • Memory Allocation (malloc)

    • malloc function allows a process to
      • request a specific number of bytes from the operating system whenever needed.
      • It should be able to allocate smaller chunks of memory as per the requested size from the heap memory segment.
  • Memory Deallocation (free)

    • Process should be able to return the allocated chunks of memory back to the heap memory segment whenever needed using the free function.
    • There’s no strict sequence for returning the memory chunks; the process can do it in any order.
  • No Supporting Data Structures

    • The heap memory management should not utilize any supporting data structures.
    • Utilizing data structures would imply needing additional memory to manage them, thereby falling into a "chicken and egg" problem: to manage memory, more memory (and management) is needed.

🚀 Curiosity

  1. Question: How does the malloc function determine the size and location of the memory chunk to allocate from the heap memory? Answer: The malloc function typically uses a strategy involving maintaining a list of free blocks and on a request, it finds a block of a suitable size, splits it (if the block is larger than requested), and returns a pointer to the block, updating its record of free and used blocks.

  2. Question: What could be the potential issues if the memory chunks are returned to the heap in a non-sequential manner? Answer: Returning memory chunks in a non-sequential manner could lead to fragmentation. Fragmentation occurs when memory is allocated and deallocated in such a way that the free memory is broken into small pieces scattered throughout the heap, making it harder to allocate larger contiguous blocks when needed.

  3. Question: Can memory leaks occur with improper use of malloc and free and how might they be identified and prevented? Answer: Yes, memory leaks can occur when memory is allocated using malloc but not adequately deallocated using free, resulting in a reduction of available memory over time. Memory leaks can be identified using tools like Valgrind and can be prevented by ensuring that every allocation is paired with a deallocation once the memory is no longer needed.

  4. Question: Explain how the operating system avoids using supporting data structures for heap memory management without running into the chicken-and-egg problem? Answer: Some heap management schemes integrate management information (like block size, usage status) into the heap memory itself rather than using separate data structures. Thus, metadata is stored in the heap memory blocks, managing allocations and deallocations while avoiding the necessity for additional memory for management.

  5. Question: In what ways can heap fragmentation be minimized or managed in memory management? Answer: Heap fragmentation can be minimized through techniques such as coalescing (combining adjacent free memory blocks into a single block), using memory pools (pre-allocating a “pool” of fixed-size objects), and garbage collection (automatically identifying and reclaiming unused memory).

🍎 Concepts in Simple Words

Heap memory is like a big shelf where your computer keeps data for running processes. Imagine you have a long shelf (heap memory) where you place boxes (data) of different sizes. The malloc function helps you find a free spot on the shelf and puts your box there. The free function takes the box away when you don’t need it anymore. The trick is that you have to manage this shelf very smartly without using another mini-shelf to keep track of where all the boxes are, because that would be like needing extra shelves to manage the extra shelves! So, you put little notes (metadata) inside the spaces on the main shelf itself to remember the size of each space and whether it's free or not. This way, the computer can manage the main shelf (heap memory) effectively without needing additional shelves (data structures) to keep track of everything.