CS 439 - Heap Memory Management

¡Supera tus tareas y exámenes ahora con Quizwiz!

What do you do when you run out of heap space?

-Ask the OS for additional memory (you must construct additional pylons) 1. Ask for a very large chunk of memory 2. ...and insert the new chunk into the free list 3. ...and then try again, this time successfully *Operating system dependent -i.e, sbrk command in UNIX

What is automatic memory management (or garbage collection?)

-The programmer explicitly allocates memory, but the runtime system manages it (Java Virtual Machine) -Allocation: new -Deallocation: User can't do this! runtime system does this! -Pointers: Program and runtime system know and keep track of all of the pointers, the user doesn't have to worry as much about this. -Runtime System must discriminate live (in use) objects and garbage. -Example languages: Java, MatLab, Python

What is explicit heap memory management?

-The programmer explicitly manages all of the memory -Allocation: done by calling malloc or new -Deallocation: free/delete -Pointers: anything in this type of environment may or may not be a pointer -Example languages: C, C++

Describe the basic sequence of events that occurs when the runtime system allocates memory in the heap.

1. Allocate the space i. Be fast! ii. Keep memory space organized iii. Low fragmentation (wasted space) 2. <User uses it> 3. Deallocate the space i. Be fast! ii. Reclaim the space for later use!

Explain the Bump-Pointer heap allocation technique

1. Contiguous allocation (for all requested blocks) 2. Pointer begins at start of heap 3. As requested, bytes allocated, and pointer is "bumped" past allocation Example: #include <stdlib.h> void *malloc(size_t size); void free(void *ptr); char *p1 = malloc(3); //p1 is allocated at the base of heap, takes up 3 bytes char *p2 = malloc(1); //p2 is allocated below p1, takes up 1 byte. char *p3 = malloc(4); //p3 is allocated below p2, takes up 4 bytes. free(p2); //the one-byte ptr below p1, which is p2, is deallocated char *p4 = malloc(6); //p4 is allocated below p3, takes up 6 bytes. free(p3); // the region of heap below where the end of p2 used to be, ptr 3, is deallocated. char *p5 = malloc(2); free(p1); free(p4); free(p5);

Describe what happens when we want to allocate a block to the user, but the block is too big.

1. Divide the free block into two smaller blocks 2. Keep first (now smaller) block in the free list 3. Allocate second block to the user

Describe the Free List heap allocation technique

1. Divides memory into some size blockss 2. Maintains a free list i. Remember, must be stored on the heap! ii. Uses a special structure for free blocks. 3. To allocate memory, find the block in the free list i. Using what algorithm?????(?????) ii. If the right size does not exist, carve up a bigger piece 4. To deallocate memory, put memory back on the free list!

Describe how program code is organized in memory

Stack: main; a = 2; X; b = 2; Stack grows down from this point Heap grows up from a point later down in memory Base of heap is here Static Data Segments are stored here, after heap. Code is stored here, i.e: void X (int b) { if (b == 1) { ... int main() { int a = 2; X(a); }

Describe what happens when we allocate memory to a block in a free list that is the perfect fit for how much memory we need.

Suppose block is perfect fit: -Remove element from the list -Link the prev element with the next element -Return the current element to the user (skipping the header. This is "p", which is located after the header information containing the reference to the next element of the free list and the size of the current free block).

What are some challenges for the user when using explicit memory management?

1. More code to maintain 2. Being CORRECT i. Free an object to soon -> core dump ii. Free an object too late -> waste space iii. Never free -> at best waste, at worst fail. Some advantages however are that efficiency can be VERY high, as garbage collection algorithms don't always yield the highest rate of efficiency. It also gives programmers control of their code and their memory. So as long as the programmers actually keep track of what they're doing and are correct, explicit memory management is god like.

What are the runtime system requirements for a program environment where heap memory is explicitly managed by the user?

1. Must be able to handle arbitrary request sequences i. Memory may be allocated and freed in any order! 2. Must be able to make immediate responses to requests i. Cannot reorder/buffer requests to improve performance 3. Must be able to use ONLY the heap i. Any data structures (such as a free list) used by malloc()/free() must be stored on the heap! 4. Must be able to align blocks (e.g., on 8-byte boundary) i. Blocks must be able to hold any type of data object! 5. Must be able to NOT modify allocated blocks i. Can only manipulate or change free blocks ii. Cannot modify (or move!) other blocks after they are allocated iii. Results in fairly simple allocation policies.

Where and how do we manage dynamically allocated (user) memory (aka the heap?)

1. Program/runtime ssystem requests memory from the OS for the heap 2. OS gives memory to a process 1 to k pages at a time (why?) 3. The runtime system manages the heap memory 4. Typically, the memory is not returned to the OS until the program ends.

How do you coalesce a block to be freed with its neighbors?`

1. Scanning the list finds the location for inserting the block to be freed i. Pointer to-be-freed is "bp" ii. Pointer to previous element in the free list is "p" 2. Coalescing into larger free blocks i. Check if contiguous to upper and lower neighbors. Ex: Free list sorted by ADDRESS 1. In use 2. Free 3. In use 4. lower neighbor "p" 5. FREE ME "bp" (Free this man, removing the limit bound, and setting the beginning bound to "p" to coalesce with these free neighbors). 6. upper neighbor

Explain the division of labor between the user and the runtime system within the context of explicit memory management

1. User -Explicitly allocates memory by requesting a number of bytes -May explicitly request deallocation of memory when it is no longer used! 2. Runtime system -Receives the requests for memory -Identifies appropriate location for allocation i. If allocation doesn't fit, requests more memory from the Operating System -Returns pointer -Later, frees allocation on request

Describe how deallocation in free lists by using the "free()" function works.

1. User passes a pointer to the memory block 2. Free function inserts block into the list i. Identify the start of the entry ii. Find the location in the free list iii. Add to the lists, coalescing entries, if needed

What are some advantages and disadvantages of the "Ranged" binning strategy?

Advantages: Fewer bins! Disadvantages: you still need to search the bins for a big enough chunk if needed.

What are some advantages and disadvantages to the "exact fit" binning strategy?

Advantages: no search for requests up to size(limit) Disadvantages: many bins, each storing a pointer.

Describe how a free-list would be linked together as a circularly linked list

All of the FREE blocks are linked together. Pointers to the blocks that are currently in use are removed, since they break the definition required to be in the FREE LIST. List may be ordered by address or by size, depending on the algorithm.

What are some problems with the "best-fit" heap memory allocation strategy?

This is slow because we need to scan the free list for the best fitted block. Our problem comes from the fact that the free chunks can be different sizes. The solution is binning! AKA, dividing the list by chunk size instead of organizing by say, address order, for example.

Describe the structure of the free block, the element of a free list.

Exists in memory First: Pointer to the next free block Second: The size of the current free block Third: Free space (that can be allocated to the user)

What advantage does bump-pointer allocation have over free-list allocation?

Fast allocation. Since, on allocation, the pointer is bumped to the next available block of free space, allocating memory is very simple. You don't have to search a free list for a block of an appropriate size.

Describe the "Ranged" binning strategy

Have a bin cover a RANGE of sizes, up to a size limit. bin 1 stores blocks over a range of 1-2 bytes in size bin 2 stores blocks over a range of 3-4 bytes in size ... bin >k stores blocks that are greater than the limit This last bin is for allocating larger amounts of memory, or when you need to split free blocks to create smaller chunks.

Describe the "exact fit" binning strategy

Have a bin for each chunk size, up to a limit. Bin 1 stores a pointer to a free list of blocks of size 1 Bin 2 stores a pointer to a free list of blocks size 2 ... Bin k stores a pointer to a free list of blocks size k Bin >k (limit) stores a pointer to a bunch of blocks that are greater in size than the limit! This last bin is for allocating larger amounts of memory, and for splitting to be able to create smaller chunks, when needed.

What is in the heap?

Dynamically allocated program objects and data is in this region of memory. When you use "new" or "malloc", for example. You need to use this when the required memory size is not known until the program runs.

What system is responsible for efficiently creating and recycling memory on behalf of a user program?

the runtime system is responsible for this.


Conjuntos de estudio relacionados

Health and PE: Body Systems Part I-III

View Set

PHYSICS 20E - LIFE IN THE UNIVERSE (midterm 2)

View Set

Identifying Independent and Dependent Variables

View Set

Adaptive Quizzing Basics of Nursing Practice - Intermediate

View Set

Chapter 14: Psychosocial Development In Middle Childhood

View Set

BIO 113 - Chapter 1 Study Questions

View Set

1.1-1.9 AP-Style MC Practice & Quizzes

View Set

Nursing Care During Stages of Labor ATI Chapter 14

View Set