1 / 18

Garbage Collection

Garbage Collection. CS 537 - Introduction to Operating Systems. Freeing Memory. Some systems require user to call free when finished with memory C / C++ reason for destructors in C++ Other systems detect unused memory and reclaim it Garbage Collection this is what Java does.

zeno
Download Presentation

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 CS 537 - Introduction to Operating Systems

  2. Freeing Memory • Some systems require user to call free when finished with memory • C / C++ • reason for destructors in C++ • Other systems detect unused memory and reclaim it • Garbage Collection • this is what Java does

  3. Garbage Collection • Basic idea • keep track of what memory is referenced and when it is no longer accessible, reclaim the memory • Example • linked list

  4. Example Obj1 Obj1 Obj1 head • Assume programmer does the following • obj1.next = obj2.next; next next next tail Obj1 Obj1 Obj1 head next next next tail

  5. Example • Now there is no way for programmer to reference obj2 • it’s garbage • In system without garbage collection this is called a memory leak • location can’t be used but can’t be reallocated • waste of memory and can eventually crash a program • In system with garbage collection this chunk will be found and reclaimed

  6. Mark-and-Sweep • Basic idea • go through all memory and mark every chunk that is referenced • make a second pass through memory and remove all chunks not marked OS 0 1 2 3 p2 = 650 p2 = 360 0 100 350 450 600 • Mark chunks 0, 1, and 3 as marked • Place chunk 2 on the free list (turn it into a hole)

  7. Mark-and-Sweep Issues • Have to be able to identify all references • this is difficult in some languages • similar to compaction • Requires jumping all over memory • terrible for performance • cache hits • virtual memory • Have to stop everything else to do • Search time proportional to non-garbage • may require lots of work for little reward

  8. Reference Counting • Basic idea • give each chunk a special field that is the number of references to chunk • whenever a new reference is made, increment field by 1 • whenever a reference is removed, decrement field by 1 • when reference count goes to zero, collect chunk • Requires compiler support

  9. Reference Counting • Example • everything in italics is added by compiler 1 p 0 Object p = new Object; p.count++; Object q = new Object; q.count++; p.count--; if(p.count == 0) collect p p = q; p.count++; q 1 2

  10. Reference Counting • Above example does not check for NULL reference Object p = new Object p.count++; p.count--; p = NULL; if(p != NULL) p.count++;

  11. Reference Counting Issues • What about pointers inside 0 referenced page? 0 1 • both of these are garbage • before reclaiming a chunk, must go through all references in the chunk • decrement the chunk they reference by 1

  12. Reference Counting Issues • What about cycles? p = new Object(); q = new Object(); p[3] = q; q[4] = p; p = q = NULL; p 1 2 1 q 1 2 1

  13. Reference Counting Issues • Both of the chunks above are garbage • Both of the chunks above have a reference count of 1 • Neither chunk will be reclaimed • Reference counting fails conservatively • it may not collect all the garbage • but it will never throw away non-garbage

  14. Reference Counting Issues • Final conclusions • relatively fast • it does collection immediately on garbage • safe • never throws away non-garbage • mostly works • has problems with cycles

  15. Generational Garbage Collection • Basic idea • occasionally create a new area in memory • every time an object is referenced, move it to that new area • when the original area becomes sparse, run mark-and-sweep on it • move surviving chunks to the new area • This method also compacts data in new region

  16. Generational Garbage Collection • Example • have initial memory – meminit • after time t create a new area – memnew • chunks r and v are unreferenced at time t p q r s t v meminit references after time t p.invokeMethod(); s.someField = someValue; q.doSomething(); r v Run Test-and-Sweep to move chunk v into the new area Then release all of meminit meminit p s q t memnew

  17. Generational Garbage Collection • After running for awhile, there should be multiple memory areas • some of these areas will consist of long lived chunks • do collection on these areas infrequently • some of these areas will consist of short lived chunks • do collection on these areas frequently • This is where the name of the algorithm comes from

  18. Generational Collection Issues • Same issues as those for compaction • identifying pointers • overhead of copying data • must interrupt the system to run the algorithm • On the good side • much faster than mark-and-sweep • collects all the garbage • unlike reference counting • This is the method of choice in today’s world

More Related