Search This Blog

Saturday, 3 November 2012

Garbage Collection, Heap in android and Java

Garbage collection (GC) is the process that aims to free up occupied memory that is no longer referenced by any reachable Java object, and is an essential part of the Java virtual machine's (JVM's) dynamic memory management system. In a typical garbage collection cycle all objects that are still referenced, and thus reachable, are kept.
 Object no longer have any reference are elligble for garbage collection.

Before going further we need to discuss some important term in Garbage  Collection in android as well java. it works similar

    
  Heap Size or Java Heap or Heap : Every device allocate a specific amount of memory to run a process. It does not allocate all memory to one  process to ensure multitasking. e.g Let an android example, a device with 2 GB memory have heap size of 48 MB(this is my assumption, Heap size depends on device density and other factor). So this device will allow 26  process to run at a time and rest memory used in running OS files. when a process load in memory. It start allocating memory to object. So heap size start increasing. Eventually, the Java heap will be full, which means that an allocating thread is unable to find a large-enough consecutive section of free memory for the object it wants to allocate.At that point, the JVM determines that a garbage collection needs to  happen and it notifies the garbage collector. A garbage collection can also be triggered when a Java program calls System.gc(). Using  System.gc() does not guarantee a garbage collection. Before any garbage collection can start, a GC mechanism will first determine whether it is  safe to start it
  
A garbage collector should never reclaim an actively referenced object; to do so would break the Java virtual machine specification. A garbage collector is also not required to immediately collect dead objects. Dead objects are eventually collected during subsequent garbage collection cycles. While there are many ways to implement garbage collection, these two assumptions are true for all varieties. The real challenge of garbage collection is to identify everything that is live (still referenced) and reclaim any unreferenced memory, but do so without impacting running applications any more than necessary

Two kinds of garbage collection are present to determine which object will garbage collect--

  • Reference counting collectors
  • Tracing collector algorithms

No comments:

Post a Comment

Feedback always help in improvement. If you have any query suggestion feel free to comment and Keep visiting my blog to encourage me to blogging

Android News and source code