Download presentation
Presentation is loading. Please wait.
1
Memory Allocation and Garbage Collection
2
Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know memory requirements in advance when the program is written. In early Fortran all memory had to be allocated in advance. There was no dynamic memory In early Fortran all memory had to be allocated in advance. There was no dynamic memory You can request memory dynamically using malloc/new. You can request memory dynamically using malloc/new.
3
Types of Memory Management Dynamic memory needs to be recycled after it is no longer in use or the system may run out of memory. Dynamic memory needs to be recycled after it is no longer in use or the system may run out of memory. There are two ways memory can be recycled: There are two ways memory can be recycled: –Explicit Memory Management: »The program calls free/delete explicitly –Automatic Memory Management »The system determines what can be recycled using Automatic Garbage Collection
4
Explicit Memory Management The program calls free/delete when object is no longer in use. The program calls free/delete when object is no longer in use. Advantages: Advantages: –It uses less memory. The program do not need to wait to recycle memory. –Faster. No GC overhead and the memory allocated is likely to be in cache.
5
Explicit Memory Management Disadvantages: Disadvantages: –Error prone –Memory leaks - Memory is never freed. It causes system slow down or running out of memory swap space. Bad 24/7 apps. –Premature Frees – Memory is freed while still in use. It causes the program to crash. –Double frees – Free an object that is already freed. –Free of Non-Heap objects – Free an object that was not allocated with malloc/free. –Memory leaks is less severe than the other three but is still bad.
6
Implicit Memory Management Let the system determine what memory is no longer in use and recycle it. Let the system determine what memory is no longer in use and recycle it. There are two basic approaches: There are two basic approaches: –Reference Counting –Tracing
7
Reference Counting In each object keep a counter that represents the number of references pointing to the object. In each object keep a counter that represents the number of references pointing to the object. When the reference counter reaches 0, it means that there is no reference to this object so it can be safely recycled. When the reference counter reaches 0, it means that there is no reference to this object so it can be safely recycled.
8
Reference Counting Advantages: Advantages: –Memory is recycled as soon as it is no longer in use. Disadvantages: Disadvantages: – cycles will not be removed because the counter never reaches 0. –Each pointer assignment will need an increment/decrement operation in the counters. –It is used by Perl, Phyton and some times in Java with auxiliar GC to collect cycles. 1 1 2 1 0 1 1
9
Tracing Garbage Collection The objects in a program are divided into two groups: The objects in a program are divided into two groups: –Root objects – Objects that where not allocated with malloc/new and that may contain reference to dynamic objects. –Dynamic objects – Objects tahat are allocated with malloc/new and can become unused during the execution of the program
10
Tracing Garbage Collection Each object has a mark bit Each object has a mark bit A stack or queue is used to keep track of the objects that have been not been visited. A stack or queue is used to keep track of the objects that have been not been visited. Tracing Garbage Collection Algorithm Tracing Garbage Collection Algorithm –Before a GC the mark bits are cleared –Scan all the root objects for references to dynamic objects. If the object referenced has not been marked, mark it and push reference to the stack. –While the stack is not empty pop one reference from the stack, scan it for references and if the object referenced has not been marked, mark it and push reference to the stack.
11
Tracing Garbage Collection –Stop until the stack is empty –When the stack is empty, the unmarked objects are no longer reachable from the roots and can be safely recycled.
12
Tracing Garbage Collection Marked – Live Unmarked - Garbage Root Objects Dynamic Objects
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.