memory allocation and garbage collection l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Memory Allocation and Garbage Collection PowerPoint Presentation
Download Presentation
Memory Allocation and Garbage Collection

Loading in 2 Seconds...

play fullscreen
1 / 12

Memory Allocation and Garbage Collection - PowerPoint PPT Presentation


  • 211 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Memory Allocation and Garbage Collection' - tex


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
why dynamic memory
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.
types of memory management
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
explicit memory management
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.
explicit memory management5
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.
implicit memory management
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
reference counting
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.
reference counting8
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

tracing garbage collection
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
tracing garbage collection10
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.
tracing garbage collection11
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.
tracing garbage collection12
Tracing Garbage Collection

Root Objects

Marked – Live

Dynamic Objects

Unmarked - Garbage