Freeing Memory

After your program is done with a certain object, is it good practice to set that object to null in order to free memory?  Or what is preferred method if there is one?
sre22Asked:
Who is Participating?
 
lwinkenbCommented:
The java garbage collector will automatically free the memory when there are no more references to it.
0
 
sre22Author Commented:
>>when there are no more references to it.

what do you mean by this?  Does my program know when will be the last time I use an object and automatically delete it after the last time?
0
 
lwinkenbCommented:
>>what do you mean by this?  Does my program know when will be the last time I use an object and automatically delete it after the last time?

Pretty much, yes.  The JVM keeps track of how many references an object has (among other things), and when it reaches 0, that object's memory is freed up for use.  The actual method the JVM uses is a bit more complex than a simple reference counter, but I hope you get the picture.
0
 
sre22Author Commented:
sounds good to me.  Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.