0

Is there any method to garbage collect an object? As I know finalize() is called just before if any object is going to garbage collected.

Wooble
  • 87,717
  • 12
  • 108
  • 131
saloni
  • 173
  • 2
  • 3
  • 16
  • 3
    Generally speaking, this is not something you should be trying to do except in very rare cases when there is usually a better approach to the problem. – Peter Lawrey Dec 13 '11 at 13:36

5 Answers5

6

The only real way to politely as the garbage collector to remove the object is to remove all references that can be used to access it.

Object o = new Object();
o = null; // at this point GC may remove the object.

To force the process you can try to call System.gc(). But remember that it does not guarantee that your object will be really deleted at this iteration of GC.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 3
    +1 it does not guarantees that your object will be really deleted at this iteration of GC. – Kent Dec 13 '11 at 13:36
  • 1
    It also does not guarantee that the garbage collection is actually run when System.gc() is called. – Bombe Dec 13 '11 at 14:34
4

Is there any method to garbage collect an object?

No there isn't.

An object will (potentially) be garbage collected after becomes "unreachable". It may be possible to cause the garbage collector to be run at a specific time by calling System.gc(). However. the JVM is allowed to ignore your System.gc() call, and running the GC at the wrong time is a waste of resources.

Indeed, calling System.gc() to reclaim a single object is horribly inefficient, and won't necessarily reclaim it anyway ... even if it is garbage at that point.


It is a mistake for a Java application to depend on objects being garbage collected at a particular time, or on finalizers being run at a particular time. If your application is designed to work that way, you should redesign it.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Hi stephen, Actually this question asked me in an interview , and at that time i forgot about System.gc() and i told him that there is no method to garbage a object. Anyways Thanks – saloni Dec 13 '11 at 14:13
3

There is a method System.gc(); which just gives a hint to the garbage collector to garbage objects. But no, there is no method which you call and immediately an object is garbage collected.

Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
2

You shouldn't rely on System.gc(); with images.

The following example will get the object garbage collected.

bitmap.recycle(); 
bitmap = null;` 

Note: System.gc(); slows down the application noticeably on some devices.

Anne
  • 26,765
  • 9
  • 65
  • 71
1

You can check this reply which shows how to ensure GC has run at least on some fragment of the heap which uses RuntimeUtil.gc() from the jlibs:

   /**
    * This method guarantees that garbage collection is
    * done unlike <code>{@link System#gc()}</code>
    */
   public static void gc() {
     Object obj = new Object();
     WeakReference ref = new WeakReference<Object>(obj);
     obj = null;
     while(ref.get() != null) {
       System.gc();
     }
   }

You can use a similar technique to replace obj with a reference to the object you want to gc.

Note: The method will be stuck in an infinite loop if the GC never runs and collects the newly allocated object.

Community
  • 1
  • 1
shams
  • 3,460
  • 24
  • 24