Garbage Collection cannot be Forced

I have read documents which say
1.garbage collection cannot be forced..
2.System.gc initiates garbage collection...

Are'nt these contradictory..which one of these is correct?

RimZim Sinha

Both of your arguments are right.
Garbage collection cannot be forced.
Because in java whenever any object has no reference in memory,then the space that object occupies is reclaimed automatically by System .gc i.e you do not have to call system.gc   explicitely, it is called implicitely by java

Saghir Qasim

Garbage collection cannot be forced. This indicates that Garbage Collector is an independent thread and is on a priority which is decided by the Java Virtual Machine, so you may not enforce Garbage Collection.
What Calling the System.gc method suggests is that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

Pallav

Does that mean one cannot explicitly call System.gc in his program?

Will it be invoked by JVm periodically?

RimZim Sinha

You can always call System.gc() as a normal method call. But, This does not guarantee that the Garbage Collection thread will clean up memory chunks which are no longer required. The execution of the Garbage Collector Thread for collecting the discarded objects is with the JVM, and JVM periodically infact on a as and when required basis cleans up the same.

Pallav

There are lots of points to be cleared on thisi topic so please read this carefully & try to understand teh exact use of system.gc::::

Some points to keep in mind are::

1.]calling System.gc() doesn't guarantee you that the GC will get performed. So if your program is dependent on a GC happening, then you're right -- and your program probably isn't coded so well.

But say your program is prety computationally intensive. But there's a few points when it slows down a bit (and kind of takes a breath if you know what I mean). It might be a really good idea to call System.gc() at those points where the system is less busy and kind of suggest to the garbage collector "Hey -- if you do your thing now, you'll be able to get it done!" Whereas if you don't put those System.gc() calls at the slower points in your code... the system may decide to do a GC at some really inopportune times and really slow down your program when its just not convenient.

VERY IMPORTANT NOTE::::

So why does Sun provide this method? Because it matters for a very few applications. There are two classes of programs where it makes sense to use the System.gc() method:

Time-Critical Applications: Calling System.gc() makes an application run slower, but it makes it less likely that garbage collection will be needed again very soon. By explicitly invoking the collector at well-known times, applications may be able to effectively avoid unpredictable garbage collection pauses that may happen at inopportune times. This helps in applications where timing matters more than total system throughput.

Test Applications: For testing and experimentation purposes, it is often convenient to run the garbage collector explicitly. This situation arises when the behavior being tested relates to collectability of an object, and it isn't good enough to know that the object will be collected as soon as its memory is needed.

This myth arises, partly, from artificial examples in Java books that use method under case two above. Code in books, trying with the best of intentions to demonstrate garbage collection at work, calls the method. It does this to coerce garbage collection to happen immediately so that the results of garbage collection are visible. Unfortunately, new programmers walk away with the idea that they need to call the garbage collector explicitly to get garbage collection to happen.

I hope now u understand the concept of System.gc()

Muneera Shiekh

How many objects are eligible for garbage collection at line1 in below code.  What is the internal process? How gc works?
 class q
 {
 private int id;
 protected void finalize()
    { s.o.p(id);}
 public q(int i)
  {
    id=i;
  }
 }
 public class r
 {
  public static void main(String s[])
   {
     q q1=null;
      for(int i=0;i<10;i++)
        q1=new q(i);
    System.gc()//line 1
 }
 }

If you have a closer look at the loop, you would find that every time you create a new instance of q you are assigning the reference to the reference variable q1.

So this goes like:

i = 0
q1 --> new q(0);

i = 1
new q(0);  // this is without any reference so eligible for Garbage Collection
q1 --> new q(1);

i = 2
new q(0);  // this is without any reference so eligible for Garbage Collection
new q(1);  // this is without any reference so eligible for Garbage Collection
q1 --> new q(2);

and so on
i = 9;
new q(0);  // this is without any reference so eligible for Garbage Collection
new q(1);  // this is without any reference so eligible for Garbage Collection
..
..
..
new q(8);  // this is without any reference so eligible for Garbage Collection
q1 --> new q(9);

So at Line 1 there are 9 q objects which are ready for garbage collection

In Java, garbage collection provides some automated memory management.All objects in Java live on the heap.The heap is also known as the garbage collectible heap.The purpose of garbage collecting is to find and delete objects that can’t be reached. Only the JVM decides exactly when to run the garbage collector.You (the programmer) can only recommend when to run the garbage collector (System.gc()).You can’t know the G.C. algorithm; maybe it uses mark and sweep, maybe it’s
generational and/or iterative.

Objects must be considered eligible before they can be garbage collected.An object is eligible when no live thread can reach it.To reach an object, a live thread must have a live, reachable reference variable to that object.

To reiterate: garbage collection can’t be forced.  Request garbage collection with System.gc();.

Pallav

Related:

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.