what is the use of calling garbage collector

what is the usage of calling garbage collector explicitly in our application,I know that it will frees the memory,Even though we did'nt call the System.gc(); the Jvm will do this.So what is the purpose of calling System.gc() and when should we go for this.

Thanks in advance

Kanchana

[303 byte] By [kanchanaaa] at [2007-10-2 15:01:26]
# 1

> what is the use of calling garbage collector explicitly in our application?

If you run with -XX:+DisableExplicitGC

it will do nothing at all.

The only reason I know of for explicitly requesting a GC run is when minimizing a GUI app

(hoping to prevent a GC run when the app is activated again).

> I know that it will free the memory

Not necessarily, you're only requesting a GC run, there are no guarantees

tschodta at 2007-7-13 13:47:38 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

When running with the CMS collector this will force a stop-the-world compacting collection, which means you lose the benefits of mostly concurrent collector. If you're running with a mutli-gigabyte heap, this can be a potentially expensive operation (minutes of stop-the-world GC).

I would recommend tuning your garbage collection settings first, before resorting to explicitly calling Sytem.gc() in your code. If you search the Sun site for the keywords garbage collection tuning, you'll find some good starting material.

jscottarmstronga at 2007-7-13 13:47:38 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3
When you want any object to be garbage collected.Even explicitly calling System.gc() does not gaurantee that th eobject will be removed from the memory.
Pushpendramaharyaa at 2007-7-13 13:47:38 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4

> what is the usage of calling garbage collector

> explicitly in our application,I know that it will

> frees the memory,Even though we did'nt call the

> System.gc(); the Jvm will do this.So what is the

> purpose of calling System.gc() and when should we go

> for this.

It can be useful if your application knows it would be better to do a

(possibly expensive) GC now, rather than later. The most common use

is after initialization completes and just before an app goes 'live' (e.g.,

starts accepting user requests). A System.gc() will clear most or all

the temporary data allocated during initialization.

Even though there's no guarantee provided by the specification,

the Sun implementations will collect the entire heap in an attempt

to reclaim as much memory as possible. (That does not include clearing

SoftReferences, though.) Because this can be expensive with large heaps,

System.gc() is best used sparingly, or avoided.

jxca at 2007-7-13 13:47:38 > top of Java-index,Java HotSpot Virtual Machine,Specifications...