1 / 12

Memory Allocation and Garbage Collection

Memory Allocation and Garbage Collection. Why Dynamic Memory?. 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 You can request memory dynamically using malloc/new.

tex
Download Presentation

Memory Allocation and Garbage Collection

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Memory Allocation andGarbage Collection

  2. Why Dynamic Memory? • 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 • 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. • 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. • 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: • 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. • 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. • 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: • Memory is recycled as soon as it is no longer in use. • 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 2 1 1 0 1 1

  9. Tracing Garbage Collection • 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 • A stack or queue is used to keep track of the objects that have been not been visited. • 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 Root Objects Marked – Live Dynamic Objects Unmarked - Garbage

More Related