

Actually you can't force java garbage collector as done in C or C++ to free memory . We can only request for it by using the following command System.gc(); As we know that the System is a sub class of the static super class Object.Hence we have use the classname System with dot operator to the method garbage collector or gc();

Hi,
Here is the answer.
You can't force it but you call System.gc(), which is a "hint" to the runtime engine that now might be a good time to run the GC. But this does not guarantee when it will happen. Local variables in methods go out of scope when the method exits. At this point the methods are eligible for garbage collection. Each time the method comes into scope the local variables are re-created.
Thanks.

Hi, the priority of GC Thread is low to compare all other threads so it always run in the last.We can not force to GC Thread to collect the garbaze.there are two cases : first for other class for which we can never force GC THread AS: class Abc{ protected void finalize() { System.out.println("garbaze collected"); } public static void main(String []args) { //case1 String s1="ram"; s1=null; for(int i=0;i<100000;i++) { System.gc(); } //case2 Abc ob = new Abc(); ob = null; System.gc(); } } in above example we have 2 cases first for String class whose object is null & we make 100000time call but gc not run. 2nd is our class whose object is null in this case gc runs in 1st call. so for otherclass object we can never force to GC.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.