garbage collection

15
GARBAGE COLLECTION

Upload: somya-bagai

Post on 08-May-2015

1.090 views

Category:

Education


1 download

DESCRIPTION

JAVA GARBAGE COLLECTOR

TRANSCRIPT

Page 1: Garbage collection

GARBAGE COLLECTION

Page 2: Garbage collection

MEMORY MANAGEMENT

• Languages like C or C++ that do not offer automatic garbage collection.

• Creating code that performs manual memory management cleanly and thoroughly is a nontrivial and complex task, and while estimates vary, it is arguable that manual memory management can double the development effort for a complex program.

Java's garbage collector provides an automatic solution to memory management. It frees you from having to add any memory management logic to your application. It helps in ensuring program integrity.

Page 3: Garbage collection

OVERVIEW

Garbage collection revolves around making sure that the heap has as much free space as possible.

Heap is that part of memory where Java objects live, and it's the one and only part of memory that is in any way involved in the garbage collection process.

When the garbage collector runs, its purpose is to find and delete objects that cannot be reached.

If no more memory is available for the heap, then the new operator throws an OutOfMemoryException.

Page 4: Garbage collection

DISADVANTAGE

GC is a overhead, system has to stop current execution to execute it .

1. Causes short stop and may influence user’s experience.

2. We can do nothing to improve gc,but only improve out program

1. Less control over scheduling of CPU time.

Page 5: Garbage collection

WHEN DOES GARBAGE COLLECTOR RUN

The garbage collector is under the control of the JVM. The JVM decides when to run the garbage collector.

Objects that are referenced are said to be alive. Unreferenced objects are considered dead(garbage).

From within your Java program you can ask the JVM to run the garbage collector, but there are no guarantees, under any circumstances, that the JVM will comply.

The JVM will typically run the garbage collector when it senses that memory is running low.

Page 6: Garbage collection

DETECTING GARBAGE OBJECTS

REFERENCE-COUNTING COLLECTORS: When the object is created the reference count of object is set to one. When you reference the object r.c is incremented by 1. When reference to an object goes out of scope ,the r.c is decremented

Object that have r.c zero (not referenced) is a garbage object.

ADVANTAGES:1. Can run in small chunks of time.2. Suitable for real time environments

Page 7: Garbage collection

TRACING COLLECTOR(mark and sweep algo)

1. Traverse through a graph ,starting from the root

2. Marks the objects that are reachable.3. At the end of the trace all unmarked objects

are identified as garbage.

Page 8: Garbage collection

COMPACTING COLLECTORS :1. Reduces fragmentation of memory by moving

all free space to one side during garbage collection.

2. Free memory is then available to be used by other objects.

3. References to shifted objects are updated to refer to new m/m location.

Page 9: Garbage collection

EXPLICITLY MAKING OBJECTS AVAILABLE FOR GARBAGE COLLECTION

Nulling a Reference : Set the reference variable that refers to the object to null.

1. public class GarbageTruck {2. public static void main(String [] args) {3. StringBuffer sb = new StringBuffer("hello");4. System.out.println(sb);5. // The StringBuffer object is not eligible for collection6. sb = null;7. // Now the StringBuffer object is eligible for

collection8. }9. }

Page 10: Garbage collection

Reassigning a Reference Variable : We can also decouple a reference variable from an object by setting the reference variable to refer to another object.

class GarbageTruck {public static void main(String [] args) {StringBuffer s1 = new StringBuffer("hello");StringBuffer s2 = new StringBuffer("goodbye");System.out.println(s1);// At this point the StringBuffer "hello" is not eligibles1 = s2; // Redirects s1 to refer to the "goodbye"

object// Now the StringBuffer "hello" is eligible for

collection}}

Page 11: Garbage collection

FORCING GARBAGE COLLECTIONGarbage collection cannot be forced. However, Java

provides some methods that allow you to request that the JVM perform garbage collection.

USING RUNTIME CLASS 1. The garbage collection routines that Java provides are

members of the Runtime class.2. The Runtime class is a special class that has a single

object (a Singleton) for each main program.3. The Runtime object provides a mechanism for

communicating directly with the virtual machine. 4. To get the Runtime instance, you can use the method

Runtime.getRuntime(), which returns the Singleton. Once you have the Singleton you can invoke the garbage collector using the gc() method.

Page 12: Garbage collection

Using static methods1. The simplest way to ask for garbage collection

(remember—just a request) is

System.gc();

Garbage collection process is not under the user's control. So it makes no sense to call System.gc(); explicitly. It entirely depends on the JVM.

JVM also runs parallel gc threads to remove unused objects from memory . So there is no requirement to explicitly call System.gc() or Runtime.gc() method.

Page 13: Garbage collection

FINALIZATION

Java provides you a mechanism to run some code just before your object is deleted by the garbage collector. This code is located in a method named finalize() that all classes inherit from class Object.

Any code that you put into your class's overridden finalize() method is not guaranteed to run.

There are a couple of concepts concerning finalize() that you need to remember.

1. For any given object, finalize() will be called only once (at most) by the garbage collector.

2. Calling finalize() can actually result in saving an object from deletion.

Page 14: Garbage collection

Right before an asset is freed, the java run time calls the finalize() method on the object.

The finalize method has this general form :

protected void finalize(){//finalize code}

Keyword protected prevents access to finalize by code defined outside its class.

Page 15: Garbage collection

THANK YOU

Somya Bagai Sonia Kukreja Varun Luthra