






































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of dynamic memory allocation and compares two approaches: implicit memory management using garbage collection (gc) and explicit memory management. It discusses reference counting and mark and sweep algorithms, as well as the advantages and disadvantages of each method. The document also covers the concept of external and internal fragmentation and various malloc implementation policies.
Typology: Study notes
1 / 78
This page cannot be seen from the preview
Don't miss anything!







































































Spring 2009 Instructor: Pascal Meunier Original slides created by Gustavo Rodriguez-Rivera Computer Science Department Purdue University
The language runtime library provides a Garbage Collector (GC) that determines what objects are no longer in use and frees them. (^) The language runtime library provides common language functionality to a program. There are two approaches: (^) Reference Counting (^) Mark and Sweep
In reference counting every object has a counter with the number of reference pointing to the object, When the reference count reaches 0 the object is removed. This is the approach used by languages such as Perl and Python Advantage: (^) Easy to implement. Incremental (objects are freed while program is running) Disadvantage: (^) Slow. Every pointer assignment needs to increase/decreased a reference count. (^) Unable to free cycles (loops) of references
Is this object in use? (^) If it's not referenced by anything then it isn't, and can never be again used (^) Delete it, reclaim memory Naive approach (^) Mark all objects as not in use (mark bit) (^) Find all pointers everywhere (^) Mark objects that are referenced as being in use (^) Delete objects not in use
Roots (^) All objects in the call stack (^) Local variables (^) Parameters (^) Globals (^) Registers
Transitivity (^) Root -> Object A -> Object B -> Object C (^) Root can reach “C”
Global Vars Stack^ Registers
The program needs to stop all the threads while the GC is taking place. This is done because if the pointers change while the GC is taking place, some reachable objects may be unmarked and can be erroneously collected. Stopping all the threads except the GC thread is called “Stop-the-world”.
Explicitly (i.e., not automatically) request and free memory Use a “malloc library” usually provided by the C standard library, libc Memory is initially requested from the OS Difficult to return memory to the OS (^) OS handles memory in pages (^) Memory allocator handles memory in bytes
Memory obtained from OS is put in free list Requests from program satisfied using memory from the list Freed memory goes back to the free list In effect, recycling memory and managing it without intervention from the OS (^) This decreases the number of times that memory is requested from the OS
When the memory allocator runs out of memory in the free list it calls sbrk(s). Sbrk(s) increases the size of the heap by s bytes. Sbrk retuns a pointer to the old heap limit. You can decrease the size of the heap by passing a negative size to sbrk. This will shrink the heap.
Heap BSS Data Text
sbrk(s)
Returning memory back to the OS is difficult because: (^) Sbrk only can return memory to the OS if it happens to be at the end of the heap. (^) Munmap can return memory to the OS if whole pages are unused. Most memory allocators do not return memory to the OS. However this has not been a problem.
There are several data structures that can be used for memory allocation: (^) Single Free list (^) Segregated Free Lists (^) Cartesian Tree (^) Boundary tags