Docsity

Dynamic Memory Allocation: Implicit vs. Explicit and Garbage Collection - Prof. Gustavo Ro, Study notes of Operating Systems

Partial preview of the text

Download Dynamic Memory Allocation: Implicit vs. Explicit and Garbage Collection - Prof. Gustavo Ro and more Study notes Operating Systems in PDF only on Docsity!

CS354: Operating Systems

Spring 2009 Instructor: Pascal Meunier Original slides created by Gustavo Rodriguez-Rivera Computer Science Department Purdue University

Part 9: Memory Allocation

Implicit Memory Management

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

Reference Counting

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

Mark-and-Sweep Garbage

Collection

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

Reachability

Roots  (^) All objects in the call stack  (^) Local variables  (^) Parameters  (^) Globals  (^) Registers

Transitivity  (^) Root -> Object A -> Object B -> Object C  (^) Root can reach “C”

Mark and Sweep Garbage

Collection

Global Vars Stack^ Registers

Mark and Sweep Garbage Collection

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”.

  • Program freezes, generally unpredictably

Explicit Memory Management

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

Free List

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

Explicit Memory Management

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.

Explicit Memory Management

Heap BSS Data Text

sbrk(s)

s

Explicit Memory Management

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.

Malloc Implementation

There are several data structures that can be used for memory allocation:  (^) Single Free list  (^) Segregated Free Lists  (^) Cartesian Tree  (^) Boundary tags