aspose file tools*
The moose likes Beginning Java and the fly likes Making object null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Making object null" Watch "Making object null" New topic
Author

Making object null

pradeep chellappan
Greenhorn

Joined: Oct 20, 2005
Posts: 29
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
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16


Adding in the explicit call to "a = null" doesn't gain you anything. an object is eligible for gc when there are no references to it. So, it doesn't matter if you set the reference to null first - the reference still ends up not pointing to the object.

You CANNOT force garbage collection.

that's important, so let me say it again:

You CANNOT force garbage collection.

Objects will be gc'd when the JVM thinks it needs to do a gc, and then, only if there are no active references to it.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
pradeep chellappan
Greenhorn

Joined: Oct 20, 2005
Posts: 29
Hi Fred,

If i make a change as shown below:

a= null ;
a = parseString(b) ;

Question

1. What will happen to heap memory allocated previously for the string array object a.Let us suppose that 100,000 times heap memory object is allocated.So,what will happen to the memory allocated if i make string array as null.Will that free the memory utilized previously.If not can you please explain.

2. What i am trying to achieve here is to free the memory allocated for the string array[] object somehow.Can i free the memory allocated to the object-obj of type A by using Runtime.gc().Please advise.

3.The pseudo code which i have written is an example of not releasing the memory properly.Do you agree with me?If yes,why?If not Why?

Thanks ,
Pradeep.

Thanks
Pradeep.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

1) What I am saying is that there is no reason to explicitly set a to null, if you are going to then immediatly assign it to something else. the end result is the same, in that the orignal object has nothing referring to it.

Put it this way. let's say i give you a piece of paper with my phone number on it. then, a few weeks later, i get a new number. it there a difference between me saying

"change my number to 123"

and

"erase my old number off that piece of paper. Then, write my new number down on it"

In the end, the old number is lost, and the piece of paper now refers to the new number.

it certainly does't hurt to do it explicity, but it doesn't gain anything. when the original object has no more refferences, it becomes eligible for garbage collection.

Can i free the memory allocated to the object-obj of type A by using Runtime.gc().Please advise.

see my answer above - the part where i said "You CANNOT force garbage collection."

3) I don't have time to really analyze your code right now. Hopefully someone else can. otherwise, i can look at it when i get home from work.
 
 
subject: Making object null