Gc in android

35
GARBAGE COLLECTION IN ANDROID VIKAS M.Tech(Digital Comm) MSRIT, Bangalore

description

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

Transcript of Gc in android

Page 1: Gc in android

GARBAGE COLLECTION

INANDROID

VIKASM.Tech(Digital Comm)MSRIT, Bangalore

Page 2: Gc in android

Agenda •Introduction •Classification of GC•Tracing algorithms

▫Mark & Sweep GC▫Generational GC▫Copying GC▫Incremental GC- CMS

•Allocation and collection in android•Conclusion•Observations

Page 3: Gc in android

•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.

Page 4: Gc in android

•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

Page 5: Gc in android
Page 6: Gc in android
Page 7: Gc in android

•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

Page 8: Gc in android

•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.

Page 9: Gc in android

Classification of Garbage collector

Basic algorithms

Reference counting Tracing algorithms

•Mark & Sweep

•Generational•Copying•Incremental

Page 10: Gc in android

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.

Page 11: Gc in android

(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.

Page 12: Gc in android

TWO FINGER ALGORITHM FOR MARK AND SWEEP GARBAGE COLLECTION

FLOWCHART

There are two ptrs Free_ptr and Live_ptrvoid 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 elseheap.release (p);

Page 13: Gc in android

PICTORIAL REPRESENTATON OF TWO FINGER ALGORITHM

Page 14: Gc in android

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

Page 15: Gc in android

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

Page 16: Gc in android

Cheney's Algorithm for copying Garbage collection

Page 17: Gc in android

PICTORIAL REPRESENTATON OF CHENEY’S ALGORITHM

Page 18: Gc in android

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

Page 19: Gc in android

(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

Page 20: Gc in android

For R in roots Live_ptr = heap_bottomWhile(application runs){

Live_ptr scans the heap and marks the live objectIf (live_ptr == live object)

Copy to free listsLive_ptr ++If(application halts)Exit()Elseif(application is still running and there is change in reference)rescan and mark the changed reference to live objectgoto (1)

Concurrently sweep and use linked lists to connect the free spaces}

CMS ALGORITHM

Page 21: Gc in android

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.

Page 22: Gc in android

•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

Page 23: Gc in android
Page 24: Gc in android
Page 25: Gc in android

Pinned objects are promoted at the start of a garbage collection so that they are not moved to next space

Page 26: Gc in android
Page 27: Gc in android

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

Page 28: Gc in android
Page 29: Gc in android

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.

Page 30: Gc in 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.

Page 31: Gc in android

•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

Page 32: Gc in android

Preparation for GC IN ANDROID…

Page 33: Gc in android

Continued..

Page 34: Gc in android

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.

Page 35: Gc in android

THANK YOU