Heap Flashcards
Heap Memory Management
- Explicit memory management is essentially required for languages that allow users to access memory directly.
(a) [3pts] What allocator do these algorithms typically use?
*-fit allocation
(b) [3pts] Why is garbage collection ineffective for these languages?
You can’t rearrange memory after allocating, as that would invalidate pointers given to the user
- A user allocates five variables: a, b, c, d, and e, so that they are allocated on the heap as such: a b c d e
(a) Later, she frees()’s b. After the free, what happens
i. [2pts] to a?
ii. [2pts] to c?
i. [2pts] to a?
Nothing, other pointers should not change in explicit memory management
ii. [2pts] to c?
Nothing, other pointers should not change in explicit memory management
Heap Memory Management
1. [3pts] When we studied automatic memory management, we discussed three algorithmic components that make up all implementations of automatic memory managers. What were they?
Allocation
Identification
Reclamation
- [3pts] We also discussed how explicit pointers reduce the ability of a system to perform automatic memory management. Why?
The addresses of the pointers cannot be altered because they are in use by the user.
- [4pts] Name and describe one copying garbage collector.
Mark-Compact: Trace the heap for unreachable objects when the entire heap fills up and then apply compaction to move all the objects together to the beginning of the heap.
Semi-space - Split the heap into two parts (to and from space) and when one part fills up, copy all reachable objects into the other half and reclaim all the memory in the from space.
- In explicit memory management, who is responsible for:
(a) [2pts] allocating memory?
(b) [2pts] deallocating memory?
(a) [2pts] allocating memory?
User
(b) [2pts] deallocating memory?
User
- In automatic memory management, who is responsible for:
(a) [2pts] allocating memory?
(b) [2pts] deallocating memory?
(a) [2pts] allocating memory?
User
(b) [2pts] deallocating memory?
Runtime system / garbage collector
Heap Memory Management
1. In class, we studied both explicit and automatic memory management.
(a) [4pts] In explicit memory management, we recommend that you assign NULL to freed pointers. Why?
This is so that we do not accidentally refer freed memory locations in the future. If we do so, we would get an exception stating dereferencing a null pointer which is easier to fix than find memory errors caused by accessing unmapped memory.
(b) [4pts] In automatic memory management systems, the runtime system manages the pointers and the users are not given access to them. Why?
So we can rearrange memory to reduce fragmentation.
- As part of automatic memory management, we studied the interactions between two allocators, free list and bump pointer, and two reclamation algorithms, mark-sweep and mark-compact.
(a) [4pts] Which allocator is used by the mark-sweep algorithm? Why is it an appropriate choice?
Free list - it is appropriate because it does not perform copying. In such a case if bump pointer is used, we may lose freed space.
(b) [4pts] Which allocator is used by the mark-compact algorithm? Why is it an appropriate choice?
Bump pointer - it is appropriate because we will be copying memory to the front of the heap on garbage collection, so bump pointer allows us to quickly allocate
Heap Memory Management
- [3pts] Where is heap management implemented?
Heap management is implemented by the runtime system of a process.
- [2pts] Which allocator should a copying garbage collector use? Defend your answer.
Bump pointer, because we can copy memory to the beginning of the heap, we can always allocate in order using a bump pointer to increase performance
- [3pts] What happens if multiple processes try to dynamically allocate memory in the heap at the same time?
Each process has its own heap.