Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Gc in android


Published on

this presentation helps you in briefing you about the garbage collection technique in android

Published in: Technology

Gc in android

  2. 2. Agenda <ul><li>Introduction </li></ul><ul><li>Classification of GC </li></ul><ul><li>Tracing algorithms </li></ul><ul><ul><li>Mark & Sweep GC </li></ul></ul><ul><ul><li>Generational GC </li></ul></ul><ul><ul><li>Copying GC </li></ul></ul><ul><ul><li>Incremental GC- CMS </li></ul></ul><ul><li>Allocation and collection in android </li></ul><ul><li>Conclusion </li></ul><ul><li>Observations </li></ul>
  3. 3. <ul><li>Data, notably byte-code, must be shared between multiple processes to minimize total system memory usage. </li></ul><ul><li>The overhead in launching a new app must be minimized to keep the device responsive. </li></ul><ul><li>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. </li></ul><ul><li>Byte-code optimization (quickened instructions, method pruning) is important for speed and battery life. </li></ul>
  4. 4. <ul><li>Languages like C, C++ that compile native code require linking stepafter source code is compiled. </li></ul><ul><li>Linking step ->Merges code from separately compiled source files with shared libraries to form executable files. </li></ul><ul><li>But in java instead of separate linking step we have directly a loader </li></ul>
  5. 7. <ul><li>The basic - and very efficient - separation method in Android is the multiprocess capability of the Dalvik VM. </li></ul><ul><li>There is however another, often overlooked separation mechanism  inside  each VM process that provides further separation the ClassLoader separation </li></ul>
  6. 8. <ul><li>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. </li></ul><ul><li>Explicit vs. Automatic Memory Management </li></ul><ul><li>Two common problems with explicit memory management are DANGLING REFERENCES and SPACE LEAKS. </li></ul><ul><li>An alternate approach is GC. </li></ul><ul><li>Terminologies : </li></ul><ul><ul><li>Root- Any reference variable that our program can directly access </li></ul></ul><ul><ul><li>Live objects- Objects that is referenced by root. </li></ul></ul>
  7. 9. <ul><li>Mark & Sweep </li></ul><ul><li>Generational </li></ul><ul><li>Copying </li></ul><ul><li>Incremental </li></ul>Classification of Garbage collector Basic algorithms Reference counting Tracing algorithms
  8. 10. Tracing algorithms <ul><li>The “Tri-color marking” mechanism is used to describe and illustrate the tracing algorithm. </li></ul><ul><li>Tri - color Marking </li></ul><ul><ul><li>White : not yet seen by the collector. </li></ul></ul><ul><ul><li>Black : alive and scanned by the collector </li></ul></ul><ul><ul><li>Gray : alive but not yet scanned by the collector. </li></ul></ul>
  9. 11. (I) Mark-and-sweep GC <ul><li>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. </li></ul>
  10. 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);
  12. 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
  13. 15. <ul><li>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. </li></ul>(III) COPYING GARBAGE COLLECTOR
  14. 16. Cheney's Algorithm for copying Garbage collection
  16. 18. Advantages and disadvantages <ul><li>Advantages </li></ul><ul><li>Eliminate fragmentation. </li></ul><ul><li>Fast allocation, as out of memory checks is just a pointer check. </li></ul><ul><li>Actual allocation is just incrementing the free pointer. </li></ul><ul><li>Disadvantages </li></ul><ul><li>Extra storage during copy i.e almost double the memory space is required </li></ul><ul><li>Difficult to combine with incremental but if we combine it then we can get boost in performance. </li></ul><ul><li>Ex Baker’s and Barlett’s incremental algorithms are some of them </li></ul>
  17. 19. (IV) Concurrent Mark-Sweep (CMS) Collector <ul><li>Collection of the old generation is done concurrently with the execution of the application. </li></ul><ul><li>Initially marks all the live objects. </li></ul><ul><li>Concurrent mark </li></ul><ul><ul><li>Mark from the set of objects found during Initial Mark </li></ul></ul><ul><ul><li>(Because app is running and updating reference fields) </li></ul></ul><ul><li>Therefore app stops and starts remarking to ensure complete marking of live objects. </li></ul><ul><li>Concurrent Sweep </li></ul><ul><ul><li>Reclaim dead space, adding it back onto free lists </li></ul></ul><ul><ul><li>No compaction is involved, instead linked lists are used </li></ul></ul>
  18. 20. <ul><li>For R in roots </li></ul><ul><li>Live_ptr = heap_bottom </li></ul><ul><li>While(application runs) </li></ul><ul><li>{ </li></ul><ul><li>Live_ptr scans the heap and marks the live object </li></ul><ul><li>If (live_ptr == live object) </li></ul><ul><li>Copy to free lists </li></ul><ul><li>Live_ptr ++ </li></ul><ul><li>If(application halts) </li></ul><ul><li>Exit() </li></ul><ul><li>Elseif( application is still running and there is change in reference ) </li></ul><ul><li>rescan and mark the changed reference to live object </li></ul><ul><li>goto (1) </li></ul><ul><li>Concurrently sweep and use linked lists to connect the free spaces </li></ul><ul><li>} </li></ul>CMS ALGORITHM
  19. 21. GC in Android <ul><li>Heap is divided into Current-space and next-space which is analogous to to_space and from _space in copying collector. </li></ul><ul><li>The spaces here are not necessarily be contiguous. </li></ul><ul><li>A space identifier associated with each heap page is used to indicate the space it is in. </li></ul><ul><li>First, the collector ‘‘guesses’’ which heap pages contain </li></ul><ul><li>objects that may be referenced from pointers in the stack, registers and the static area. </li></ul>
  20. 22. <ul><li>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. </li></ul><ul><li>Because of this uncertainty, ambiguous roots cannot be changed and these roots are locked, so the objects on these pages can be retained </li></ul>A,C,D are forwarded
  21. 25. Pinned objects are promoted at the start of a garbage collection so that they are not moved to next space
  22. 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
  23. 29. Conclusion <ul><li>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. </li></ul><ul><li>Garbage Collection is not predictable. </li></ul><ul><li>Incremental Garbage Collection is used in present ANDROID. </li></ul>
  24. 30. <ul><li>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() </li></ul><ul><li>The first step is to acquire exclusive control on the virtual machine to prevent any further Java operations. Garbage collection can then begin. </li></ul><ul><li>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. </li></ul>
  25. 31. <ul><li>We can implement concurrency in mark and sweep but difficult in Copying GC. </li></ul><ul><li>The next version of mark and sweep is mark, sweep and compact. </li></ul><ul><li>The compaction can be done parallelly known as Parallel Compact Collector. </li></ul><ul><li>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. </li></ul>Observations
  26. 32. Preparation for GC IN ANDROID…
  27. 33. Continued..
  28. 34. References : <ul><li>An Efficient Parallel Heap Compaction Algorithm - </li></ul><ul><li>Diab Abuaiadh; Yoav Ossia; Erez Petrank; Uri Silbershtein- IBM Haifa Research Laboratory </li></ul><ul><li>Memory Management in the Java HotSpot™ Virtual Machine - Sun Microsystems April 2006. </li></ul><ul><li>Incremental, Generational Mostly-Copying Garbage Collection in Uncooperative Environments - G. May Yip. </li></ul><ul><li>Managing Virtual Memory -Randy Kath, Microsoft Developer Network Technology group, January 20, 1993. </li></ul>
  29. 35. <ul><li> THANK YOU </li></ul>