SlideShare a Scribd company logo
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.
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.
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.
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.
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
 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.
 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.
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 collection
6. sb = null;
7. // Now the StringBuffer object is eligible for collection
8. }
9. }
 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 eligible
s1 = s2; // Redirects s1 to refer to the "goodbye" object
// Now the StringBuffer "hello" is eligible for collection
}
}
FORCING GARBAGE COLLECTION
Garbage 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.
 Using static methods
1. 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.
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.
 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.
THANK YOU
 Somya Bagai
 Sonia Kukreja
 Varun Luthra

More Related Content

Garbage collection

  • 2. 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.
  • 3. 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.
  • 4. 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.
  • 5. 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.
  • 6. 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
  • 7.  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.
  • 8.  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.
  • 9. 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 collection 6. sb = null; 7. // Now the StringBuffer object is eligible for collection 8. } 9. }
  • 10.  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 eligible s1 = s2; // Redirects s1 to refer to the "goodbye" object // Now the StringBuffer "hello" is eligible for collection } }
  • 11. FORCING GARBAGE COLLECTION Garbage 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.
  • 12.  Using static methods 1. 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.
  • 13. 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.
  • 14.  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.
  • 15. THANK YOU  Somya Bagai  Sonia Kukreja  Varun Luthra