SlideShare a Scribd company logo
GARBAGE COLLECTION  IN ANDROID  VIKAS M.Tech(Digital Comm) MSRIT, Bangalore
Agenda   Introduction  Classification of GC Tracing algorithms Mark & Sweep GC Generational GC Copying GC Incremental GC- CMS Allocation and collection in android Conclusion Observations
Data, notably byte-code, must be shared between multiple processes to minimize total system memory usage.  The overhead in launching a new app must be minimized to keep the device responsive.  Storing class data in individual files results in a lot of redundancy, especially with respect to strings. To conserve disk space we need to factor this out.  Byte-code optimization (quickened instructions, method pruning) is important for speed and battery life.
Languages like C, C++ that compile native code require linking stepafter source code is compiled. Linking step ->Merges code from separately compiled source files with shared libraries to form executable files. But in java instead of separate linking step we have directly a loader
 
 
The basic - and very efficient - separation method in Android is the multiprocess capability of the Dalvik VM. There is however another, often overlooked separation mechanism  inside  each VM process that provides further separation the ClassLoader separation
Memory management is the process of recognizing when allocated objects are no longer needed freeing the memory used by such objects, and making it available for subsequent allocations. Explicit vs. Automatic Memory Management Two common problems with explicit memory management are DANGLING REFERENCES and SPACE LEAKS. An alternate approach is GC. Terminologies : Root-  Any reference variable that our program can directly access Live objects- Objects that is referenced by root.
Mark & Sweep Generational Copying Incremental Classification of Garbage collector   Basic algorithms Reference counting   Tracing algorithms
Tracing algorithms The “Tri-color marking” mechanism is used to describe and illustrate the tracing algorithm. Tri - color   Marking White : not yet seen by the collector. Black : alive and scanned by the collector Gray : alive but not yet scanned by the collector.
(I) Mark-and-sweep GC GC pointer goes on checking for the live objects and if found it sweeps to the left side of the heap so that a contiguous space is formed.
TWO FINGER ALGORITHM FOR MARK AND SWEEP GARBAGE COLLECTION FLOWCHART There are two ptrs  Free_ptr  and  Live_ptr void mark (Object p)  if (!p.marked)  p.marked = true;  for each  Object q  referenced by  p mark (q); void sweep () for each  Object p  in the heap   if (p.marked)  p.marked = false  else heap.release (p);
PICTORIAL REPRESENTATON OF TWO FINGER ALGORITHM
Generational Garbage collection avoids repeated collection of objects by dividing the heap into old and young generations. •  Separate young objects •  Monitor references to young from old – “ Remembered Set” •  Use those as roots to collect just the young space (II)   Generational Garbage collection
Copying garbage collection is a method of performing GC using semi-spaces, that is, by splitting heap memory into two parts and only using one at a time. (III)  COPYING GARBAGE COLLECTOR
Cheney's Algorithm for copying Garbage collection
PICTORIAL REPRESENTATON OF  CHENEY’S ALGORITHM
Advantages and disadvantages  Advantages Eliminate fragmentation. Fast allocation, as out of memory checks is just a  pointer check. Actual allocation is just incrementing the free pointer. Disadvantages   Extra storage during copy i.e almost double the memory space is required Difficult to combine with incremental but if we combine it then we can get boost in performance. Ex Baker’s and Barlett’s incremental algorithms are some of them
(IV) Concurrent Mark-Sweep (CMS) Collector Collection of the old generation is done concurrently with the execution of the application. Initially marks all the live objects. Concurrent mark Mark from the set of objects found during Initial Mark (Because app is running and updating reference fields) Therefore app stops and starts remarking to ensure complete marking of live objects. Concurrent Sweep Reclaim dead space, adding it back onto free lists No compaction is involved, instead linked lists are used
For R in roots  Live_ptr = heap_bottom While(application runs) { Live_ptr scans the heap and marks the live object If (live_ptr == live object) Copy to free lists Live_ptr ++ If(application halts) Exit() Elseif( application is still running and there is change in reference ) rescan and mark the changed reference to live object goto (1) Concurrently sweep and use linked lists to connect the free spaces } CMS ALGORITHM
GC in Android Heap is divided into Current-space and next-space which is analogous to to_space and from _space in copying collector. The spaces here are not necessarily be contiguous. A space identifier associated with each heap page is used to indicate the space it is in. First, the collector ‘‘guesses’’ which heap pages contain objects that may be referenced from pointers in the stack, registers and the static area.
The objects that point to A, C and D are called  ambiguous roots,  because these are not actually real pointers, but they contain the root objects through which all accessible objects can be traced. Because of this uncertainty, ambiguous roots cannot be changed and these roots are locked, so the objects on these pages can be retained A,C,D are forwarded
 
 
Pinned objects are promoted at the start of a garbage collection so that they are not moved to next space
 
After the stack and registers are searched and promoting is done, the garbage collector scans the objects inside them. Scanning is done using a breadth-first discipline
 
Conclusion   When the VM cannot allocate an object from the heap because of lack of contiguous space, a memory allocation fault occurs, and the Garbage Collector is called. Garbage Collection is not predictable. Incremental Garbage Collection is used in present ANDROID.
This process starts when any thread calls the Garbage Collector either indirectly as a result of allocation failure, or directly by a specific call to System.gc() The first step is to acquire exclusive control on the virtual machine to prevent any further Java operations. Garbage collection can then begin. Concurrent mark and sweep technique is used for the objects residing in stack and copying collector technique is used for the objects residing in heap.
We can implement concurrency in mark and sweep but difficult in Copying GC. The next version of mark and sweep is mark, sweep and compact. The compaction can be done parallelly known as  Parallel Compact Collector. In android 2.3 concurrent mark and sweep algorithm is used but since no concurrency or parallelism is involved in copying collector there is a scope of improvement in copying collector if we go for concurrency. Observations
Preparation for GC IN ANDROID…
Continued..
References : An Efficient Parallel Heap Compaction Algorithm -  Diab Abuaiadh; Yoav Ossia; Erez Petrank; Uri Silbershtein-  IBM Haifa Research Laboratory Memory Management in the Java HotSpot™ Virtual Machine -  Sun Microsystems April 2006. Incremental, Generational Mostly-Copying Garbage Collection in Uncooperative Environments - G. May Yip. Managing Virtual Memory -Randy Kath, Microsoft Developer Network Technology group, January 20, 1993.
  THANK YOU

More Related Content

Gc in android

  • 1. GARBAGE COLLECTION IN ANDROID VIKAS M.Tech(Digital Comm) MSRIT, Bangalore
  • 2. Agenda Introduction Classification of GC Tracing algorithms Mark & Sweep GC Generational GC Copying GC Incremental GC- CMS Allocation and collection in android Conclusion Observations
  • 3. Data, notably byte-code, must be shared between multiple processes to minimize total system memory usage. The overhead in launching a new app must be minimized to keep the device responsive. Storing class data in individual files results in a lot of redundancy, especially with respect to strings. To conserve disk space we need to factor this out. Byte-code optimization (quickened instructions, method pruning) is important for speed and battery life.
  • 4. Languages like C, C++ that compile native code require linking stepafter source code is compiled. Linking step ->Merges code from separately compiled source files with shared libraries to form executable files. But in java instead of separate linking step we have directly a loader
  • 5.  
  • 6.  
  • 7. The basic - and very efficient - separation method in Android is the multiprocess capability of the Dalvik VM. There is however another, often overlooked separation mechanism  inside  each VM process that provides further separation the ClassLoader separation
  • 8. Memory management is the process of recognizing when allocated objects are no longer needed freeing the memory used by such objects, and making it available for subsequent allocations. Explicit vs. Automatic Memory Management Two common problems with explicit memory management are DANGLING REFERENCES and SPACE LEAKS. An alternate approach is GC. Terminologies : Root- Any reference variable that our program can directly access Live objects- Objects that is referenced by root.
  • 9. Mark & Sweep Generational Copying Incremental Classification of Garbage collector Basic algorithms Reference counting Tracing algorithms
  • 10. Tracing algorithms The “Tri-color marking” mechanism is used to describe and illustrate the tracing algorithm. Tri - color Marking White : not yet seen by the collector. Black : alive and scanned by the collector Gray : alive but not yet scanned by the collector.
  • 11. (I) Mark-and-sweep GC GC pointer goes on checking for the live objects and if found it sweeps to the left side of the heap so that a contiguous space is formed.
  • 12. TWO FINGER ALGORITHM FOR MARK AND SWEEP GARBAGE COLLECTION FLOWCHART There are two ptrs Free_ptr and Live_ptr void mark (Object p) if (!p.marked) p.marked = true; for each Object q referenced by p mark (q); void sweep () for each Object p in the heap if (p.marked) p.marked = false else heap.release (p);
  • 13. PICTORIAL REPRESENTATON OF TWO FINGER ALGORITHM
  • 14. Generational Garbage collection avoids repeated collection of objects by dividing the heap into old and young generations. • Separate young objects • Monitor references to young from old – “ Remembered Set” • Use those as roots to collect just the young space (II) Generational Garbage collection
  • 15. Copying garbage collection is a method of performing GC using semi-spaces, that is, by splitting heap memory into two parts and only using one at a time. (III) COPYING GARBAGE COLLECTOR
  • 16. Cheney's Algorithm for copying Garbage collection
  • 17. PICTORIAL REPRESENTATON OF CHENEY’S ALGORITHM
  • 18. Advantages and disadvantages Advantages Eliminate fragmentation. Fast allocation, as out of memory checks is just a pointer check. Actual allocation is just incrementing the free pointer. Disadvantages Extra storage during copy i.e almost double the memory space is required Difficult to combine with incremental but if we combine it then we can get boost in performance. Ex Baker’s and Barlett’s incremental algorithms are some of them
  • 19. (IV) Concurrent Mark-Sweep (CMS) Collector Collection of the old generation is done concurrently with the execution of the application. Initially marks all the live objects. Concurrent mark Mark from the set of objects found during Initial Mark (Because app is running and updating reference fields) Therefore app stops and starts remarking to ensure complete marking of live objects. Concurrent Sweep Reclaim dead space, adding it back onto free lists No compaction is involved, instead linked lists are used
  • 20. For R in roots Live_ptr = heap_bottom While(application runs) { Live_ptr scans the heap and marks the live object If (live_ptr == live object) Copy to free lists Live_ptr ++ If(application halts) Exit() Elseif( application is still running and there is change in reference ) rescan and mark the changed reference to live object goto (1) Concurrently sweep and use linked lists to connect the free spaces } CMS ALGORITHM
  • 21. GC in Android Heap is divided into Current-space and next-space which is analogous to to_space and from _space in copying collector. The spaces here are not necessarily be contiguous. A space identifier associated with each heap page is used to indicate the space it is in. First, the collector ‘‘guesses’’ which heap pages contain objects that may be referenced from pointers in the stack, registers and the static area.
  • 22. The objects that point to A, C and D are called ambiguous roots, because these are not actually real pointers, but they contain the root objects through which all accessible objects can be traced. Because of this uncertainty, ambiguous roots cannot be changed and these roots are locked, so the objects on these pages can be retained A,C,D are forwarded
  • 23.  
  • 24.  
  • 25. Pinned objects are promoted at the start of a garbage collection so that they are not moved to next space
  • 26.  
  • 27. After the stack and registers are searched and promoting is done, the garbage collector scans the objects inside them. Scanning is done using a breadth-first discipline
  • 28.  
  • 29. Conclusion When the VM cannot allocate an object from the heap because of lack of contiguous space, a memory allocation fault occurs, and the Garbage Collector is called. Garbage Collection is not predictable. Incremental Garbage Collection is used in present ANDROID.
  • 30. This process starts when any thread calls the Garbage Collector either indirectly as a result of allocation failure, or directly by a specific call to System.gc() The first step is to acquire exclusive control on the virtual machine to prevent any further Java operations. Garbage collection can then begin. Concurrent mark and sweep technique is used for the objects residing in stack and copying collector technique is used for the objects residing in heap.
  • 31. We can implement concurrency in mark and sweep but difficult in Copying GC. The next version of mark and sweep is mark, sweep and compact. The compaction can be done parallelly known as Parallel Compact Collector. In android 2.3 concurrent mark and sweep algorithm is used but since no concurrency or parallelism is involved in copying collector there is a scope of improvement in copying collector if we go for concurrency. Observations
  • 32. Preparation for GC IN ANDROID…
  • 34. References : An Efficient Parallel Heap Compaction Algorithm - Diab Abuaiadh; Yoav Ossia; Erez Petrank; Uri Silbershtein- IBM Haifa Research Laboratory Memory Management in the Java HotSpot™ Virtual Machine - Sun Microsystems April 2006. Incremental, Generational Mostly-Copying Garbage Collection in Uncooperative Environments - G. May Yip. Managing Virtual Memory -Randy Kath, Microsoft Developer Network Technology group, January 20, 1993.
  • 35. THANK YOU