Modul 5 - Minneshantering Flashcards
How to use malloc()?
- malloc() is used to allocate memory dynamically (at runtime) on the heap.
- Needed when you don’t know how much memory will be used until the program runs.
- You pass it a size asking for some room on the heap:
void *malloc(size_t size);
Return value:
- a pointer (variable that holds an address) to the beginning of the allocated memory
- or NULL if the memory can’t be allocated.
How to use free()?
- free() deallocates the memory for a given pointer.
- Takes a pointer that was returnes from malloc() to find the memory.
- To know how much memory that is supposed to be freed, the memory block keeps the size in a header block that lies just before the actual memory.
Example:
int *x = malloc(10 * sizeof(int));
…
free(x);
How does memory allocation work on Java (and other languages)?
They support automatic memory management. In such languages, while you call something like malloc()
to allocate memory (usually new or something similar to allocate a new object), you never have to call something to free space; rather, a garbage collector runs and figures out what memory you no longer have references to and frees it for you.
What types of calls is malloc() and free()?
They are library calls. Thus the malloc library manages space within your virtual address space, but itself is built on top of some system calls which call into the OS to ask for more memory or release some back to the system.
library calls vs system calls?
System calls and library calls are similar in that they are provided to application by the environment. The difference between the two is that system calls are implemented in kernel, whereas library calls are implemented in user space.
- System calls (functions provided by the kernel)
- Library calls (functions within program libraries)
- Library is often just a wrapper for the system call - sometimes more complex.
What does realloc() do?
realloc() makes a new larger region of memory, copies the old region into it, and returns the pointer to the new region.
What does calloc() do?
calloc() allocates memory and also zeroes it before
returning; this prevents some errors where you assume that memory is zeroed and forget to initialize it yourself
What does the system calls brk() and sbrk() do? (Kernel operations)
- brk() and sbrk() change the location of the program break, which defines the end of the process’s data segment.
- brk() sets the end of the data segment to the
value specified by address - sbrk() increments the program’s data space by
increment bytes. - Calling sbrk() with an increment of 0 can be
used to find the current location of the program
break. - Calling sbrk() is costly i.e. better to do a few large allocations and then do several smaller malloc() operations.
Free list strategies?
- Best fit: the block that minimize the left over.
- Worst fit: the block that maximize the left over.
- First fit: pick the first one.
Buddy allocation pros and cons?
Pros:
- Easy to determine if two blocks can be merged together.
- Efficient allocation and deallocations of frames.
- Coalescing efficient, O(lg(n))
- Handles external fragmentation well.
Cons:
- Internal fragmentation - if we need a frame of 9 blocks we get 16! As only blocks of size 2^n can be given.
Explain splitting a free list?
Split a memory block in the free list:
- Return the needed amount to the user
- The rest is kept on the free list
If a smaller chunk of memory is requested than what is available in the free list then the allocator performs splitting.
What is a free list?
- Data structure used for dynamic memory allocation management.
- Contains blocks (references) of the available free space on the heap.
- This data structure must not be a list per se, but just some kind of data structure to track free space.
Explain the mechanism known as coalescing of free space?
- Two blocks of free memory that lies next to each other are merged.
- The allocator coalesce free space when a chunk of memory is freed. That is merges newly freed space to the free spaces that are next to it, into a single larger free chunk.
The idea is simple: when returning a free chunk in memory, look carefully at the addresses of the chunk you are returning as well as the nearby chunks of free space; if the newly freed space sits right next to one (or two, as in this example) existing free chunks, merge them into a single larger free chunk.
What is allocated in the header block?
- The header minimally contains the size of the allocated region, it may also contain additional pointers to speed up deallocation, a magic number to provide additional integrity checking, and other information.
- Allocators store this little bit of extra information
in a header block which is kept in memory, usually just before the handed-out chunk of memory, so that we can free memory by giving a pointer to free(void *ptr) instead of giving a size parameter.
How does best fit strategy work?
- The best fit strategy is quite simple: first, search through the free list and find chunks of free memory that are as big or bigger than the requested size. Then, return the one that is the smallest in that group of candidates; this is the so called best-fit chunk.
- Tries to reduce wasted space by returning a block that is close to what the user asked.
- But naive implementations pay a heavy performance penalty when performing an exhaustive search for the correct free block.