Hi,
I have a doubt with respect to Garbage collection.
Let's suppose we have psuedo code that runs 24X7 as follows:
class A{
String[] a ; //Global Variable
String b ;
methodinA() {
a = parseString(b) ;
}
String[] parseString(String str){
String[] strArray = new String[count];
//Some piece of code
returnn strArray;
}
}
class B{
public static void main(String args[]){
A obj = new A() ;
do{
obj.methodinA() ;
}while(<some condition>
}
}
My question is:
1. Will the object-obj be garbage collected only after the program is terminated.
2. Since in the main method i have a do--while loop and will come out only if the condition(do-while condition) is met.Let's suppose that,this do while loop executes 100,000 times before the while condition fails.That means methodinA() is called 100,000 times.
methodinA() will inturn invoke parseString 100,000 times and a string array strarray is assigned 100,000 times to string array a[].Hence,100,000 times global string array is created and memory consumed by this global string array is never released until termination of program.
I am trying to free the memory of the string array a[] before the program terminates,so my my plan is to do the following:
methodinA() {
a= null ;
a = parseString(b) ;
}
Will the above change work.
If i set string array a[] as null,will it clear up the memory in the heap.
3. Is their any other way in which i can force the garbage collection for this example.Please explain.
Regards,
Pradeep