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

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

Pre 2010

Uploaded on 07/30/2009

koofers-user-5mn
koofers-user-5mn 🇺🇸

10 documents

1 / 78

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS354: Operating Systems
Spring 2009 Instructor: Pascal Meunier
Original slides created by
Gustavo Rodriguez-Rivera
Computer Science Department
Purdue University
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e

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