hi java.lang.System.gc() vs java.lang.Runtime.gc()

hi all what is the exact diff., java.lang.System.gc() vs java.lang.Runtime.gc() why dont I call Runtime.gc() like System.gc(), is ther any possibility ?Thanks in advance
[198 byte] By [JeeDevelopera] at [2007-11-27 6:49:41]
# 1
> hi all what is the exact diff., > java.lang.System.gc() vs java.lang.Runtime.gc() > > why dont I call Runtime.gc() like System.gc(), is> ther any possibility ?> > Thanks in advanceThey do the same thing. Read the
kajbja at 2007-7-12 18:23:28 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks, but I just want to call Runtime.gc() like System.gc() forcefully to invoke finalize() methodThanks in advance
JeeDevelopera at 2007-7-12 18:23:28 > top of Java-index,Java Essentials,Java Programming...
# 3
> Thanks, but I just want to call Runtime.gc() like> System.gc() forcefully to invoke finalize() method> Thanks in advanceWhat? Why do you want to do that? You should not call gc() at all.
kajbja at 2007-7-12 18:23:28 > top of Java-index,Java Essentials,Java Programming...
# 4

The JRE isn't like that - you cant "forcefully invoke" the finalize() method, in fact you can never guarantee the finalize() method will ever be called.

Calling System.gc() is no more than a hint. You're saying "now might be a good time to garbage collect, if you feel the need".

And, generally, it's better to use a PhantomReference to handle finalization, rather than the finalize method.

malcolmmca at 2007-7-12 18:23:28 > top of Java-index,Java Essentials,Java Programming...
# 5

> Thanks, but I just want to call Runtime.gc() like

> System.gc() forcefully to invoke finalize() method

> Thanks in advance

The calls are the same. Read the javadoc:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#gc()

public static void gc()

Runs the garbage collector.

Calling the gc method suggests 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.

The call System.gc() is effectively equivalent to the call:

Runtime.getRuntime().gc()

kevjavaa at 2007-7-12 18:23:28 > top of Java-index,Java Essentials,Java Programming...
# 6
> ... I just want to call Runtime.gc() like> System.gc() forcefully to invoke finalize() methodYou mean you want http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#runFinalization%28%29
tschodta at 2007-7-12 18:23:28 > top of Java-index,Java Essentials,Java Programming...
# 7
Thanks, yes,but if i write finalize(){ System.out.println("Waiting for Runtime.getRuntime.gc()");}I can call System.gc() to invoke finalize(), but I can't with Runtime.gc(), can tell why ?Thanks in Regards for All.
JeeDevelopera at 2007-7-12 18:23:28 > top of Java-index,Java Essentials,Java Programming...
# 8
> I can call System.gc() to invoke finalize(), but I can't with > Runtime.gc(), can tell why ?Because Runtime.gc() is not a static method. Try Runtime.getRuntime().gc(). http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html#gc()
kevjavaa at 2007-7-12 18:23:28 > top of Java-index,Java Essentials,Java Programming...
# 9
> Because Runtime.gc() is not a static method. > > Try Runtime.getRuntime().gc().It even hints at that in the javadoc; http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#gc%28%29
tschodta at 2007-7-12 18:23:28 > top of Java-index,Java Essentials,Java Programming...
# 10
Thanks
JeeDevelopera at 2007-7-12 18:23:28 > top of Java-index,Java Essentials,Java Programming...
# 11

> Because Runtime.gc() is not a static method.

Given that the system out has "Waiting for Runtime.getRuntime.gc()" I'm guessing he knows.

> > I can call System.gc() to invoke finalize(), but I

> can't with

> > Runtime.gc(), can tell why ?

You will get better responses if you full describe your problem. If it not compiling, or not printing out "Waiting for Runtime.getRuntime.gc()" when you think it should?

If the latter, why do you think you need to call System.gc?

mlka at 2007-7-12 18:23:28 > top of Java-index,Java Essentials,Java Programming...