Monday 19 August 2013

Garbage Collection - 48

Learning Objectives
After completing this session, you will be able to:
‰  Define Garbage Collector
‰  Explain the GC Designs
‰  Identify the Memory Leaks
Garbage Collector
The Garbage Collector runs in a thread in the JVM. When the free memory drops under a
threshold the GC begins to run to eliminate unneeded objects. Exactly when it runs and for how
long, however, cannot be controlled by the user program.
Important Facts About GC
‰  GC is unpredictable in nature, and in fact, causes serious problems for Real Time (RT)
applications of Java.
‰  The JVM does not actually require a GC and in some special situations, the GC is
simply turned off or left out. In that case, the program must itself make sure that it
does not run out of memory.
What Does a GC Perform?
‰  If a section of memory earlier used by an object is no longer referenced by any
variable, then the Garbage Collector (GC) will release this memory for re-use by new
objects. This takes place automatically without any input from the user program.
‰  There are no heap commands such malloc() and free() functions in C needed to
allocate or de-allocate memory buffers.
‰  Java does use a similar "new" operator as in C++ for creating objects but has no
"delete" operator.
Overview of GC Designs
The JVM Specification does not in fact specify very much about the GC. It leaves considerable
room for variation in implementation. Research continues in the computer science community for
fast and efficient GC algorithms. A GC must meet a number of difficult challenges:
‰  Recognize and eliminate dead (unreferenced) objects from the heap
‰  Keep the heap from becoming so fragmented that new objects do not easily fit
‰  Minimize the processing time taken away from the application
‰  Recognize circular cases where dead objects (that is, objects are no longer accessible
from the live thread processes or from static references) referencing each other and
so might give them the appearance of life
Some of the Common Garbage Collector Designs
Some of the common GC designs are:
‰  Copying Collector:
‰  Scan through all references to determine which objects are live (referenced) and copy
them to one half of the heap
‰  When finished, free up the old half
‰  Restart the application
‰  Repeat when free memory drops under a threshold
‰  Mark and Sweep:Scan through all references and mark the objects that are live
‰  Free up the areas occupied by dead objects
‰  Mark and Compact: Similar to Mark and Sweep except after the marking phase, the
live objects are copied to a free area and all of the area earlier occupied by objects is
freed up
Generational:
‰  Most objects in most programs only last for a brief time.
‰  It is more efficient to concentrate on freeing up memory from the short-lived objects
and spend as little time as possible checking on old but stable objects.
‰  The generational GC creates at least two sub-heap areas with one for short-lived
objects and the other for old objects.
‰  A Mark and Sweep or other GC algorithm would work on the short-lived heap.
‰  Those objects that last beyond a few cycles are moved to the long-lived heap, which is
only checked occasionally.
‰  These types of algorithms (as discussed in earlier slides) must interrupt the application
threads and complete their job before returning control.
‰  There are some algorithms that work in an incremental approach, rather than
completing a full clean-up at one go, and thus interfere less with the program.
Memory Leaks
‰  Memory leak refers to the loss of free memory in the heap to unusable data as a
program runs.
‰  This can lead to inefficient memory utilization and eventually to a crash of the program
when no free memory remains.
‰  Even with the garbage collector, memory leaks can still occur in Java.
Examples for Memory Leaks
Examples of situations that create memory leaks include:
‰  Maintaining references to unneeded objects: For example, a method might create
temporary objects in the first section ofthe method and then go on to do other tasks
such as a loop in which the temporary objects are no longer needed.
‰  If references to the temporary objects remain, then the objects will not be collected
and take up memory. In such cases the references should explicitly be set to null.
Waiting for finalizer:
‰  A class can override the finalize() method and use it to do clean up tasks before the
object is reclaimed by the GC.
‰  However, there is no way to know when the finalizer will be called and if a lot of
objects with finalizers are waiting for their final invocation, a lot of memory might be
used up.
‰  Generally, it is recommended to avoid application of the finalize() method and instead
create a clean up method that you call explicitly when the object is no longer needed.
Try It Out
Problem Statement:
Write a program that illustrates the working of Garbage Collector.
Code:
public class GarbageDemo {
Garbage g;
int max;
public static void main(String[] args) {
int n = 15;
if (args.length > 0)
n = (new Integer(args[0])).intValue();
GarbageDemo app = new GarbageDemo(n);
app.run();
}
public GarbageDemo(int n) {
max = n;
}
Summary
‰  In Java, garbage collection (GC) provides automated memory management.
‰  The purpose of GC is to delete objects that can’t be reached.
‰  Only the JVM decides when to run the GC, you can only suggest it.
‰  You can’t know the GC algorithm for sure.
‰  Objects must be considered eligiblebefore they can be garbage collected.
‰  An object is eligible when no live thread can reach it.
‰  To reach an object, you must have a live, reachable reference to that object.
Test Your Understanding
1.State true or false for the following:
a)Garbage Collection is a mechanism for reclaiming memory from objects that are
no longer in application, making the memory available for new objects.
b)An object being no longer in application means that it still can be referenced by
any ‘active’ part of the program.
c)Garbage Collection runs in a high priority thread. It will never kick in when memory
is too low.

No comments:

Post a Comment