1 / 16

Garbage Collection

Garbage Collection. Student: Jack Chang. Introduction. Manual memory management Memory bugs Automatic memory management We know... A program can only use objects that can be found . Reference Counting. Reference Count Store reference count in each object

matsu
Télécharger la présentation

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. Garbage Collection Student: Jack Chang

  2. Introduction • Manual memory management • Memory bugs • Automatic memory management • We know... • A program can only use objects that can be found.

  3. Reference Counting • Reference Count • Store reference count in each object • For each assignment operation, update reference count • If reference count equals 0, we de-allocate the object. Figure 1: Example objects Y Object i Object j 1 2 X Z

  4. Reference Counting Step 0: Start Object i 1 X Object j 1 Y Step 1: Y = Object i; Object i 1+1 X Object j 1-1 Y

  5. Reference Counting Step 3: Check right hand side Object i 2 X Object j 0 Y Step 4: Finish assignment Object i 2 X Y

  6. Reference Counting • Advantages • Easy to implement • Does not wait till memory is exhausted • Disadvantages • ...

  7. Reference Counting • Assignment operation gets slower 1 1 1 1 X 1 1 1

  8. Reference Counting • Assignment operation gets slower 1 1 1 0 X 1 1 1

  9. Reference Counting • Does not work with circular structure Object i 2 X Object j 1

  10. Reference Counting • Does not work with circular structure Object i 1 X Object j 1

  11. Stop and Copy Allocation Pointer Old Space New Space Allocation Pointer A B New Space C D E

  12. Stop and Copy Allocation Pointer Root set A B New Space C D E Scan Pointer

  13. Stop and Copy Allocation Pointer Scan Pointer Root set A B C D E A Allocation Pointer Scan Pointer Root set B A B C D E A

  14. Stop and Copy Root set A B E Old Space New Space

  15. Stop and Copy • Advantages • Defragment memory during garbage collection • The more garbage the faster it will be • Disadvantages • Need to copy all the reachable objects to new space • Need twice as much memory as the program actually uses

  16. At the end • The garbage collection is convenient, but... • Pauses • There are more advanced garbage collection algorithms • Concurrent • Parallel • Thank you.

More Related