Clear method and Garbage Collector
Hi Gurus,
Does the Clear method (hashtable, vector etc..) is a good option for garbage collection ?
In other words, if I have a vector V with 100 elements and I perform a V.clear(), am I sure that the 100 elements are now candidates for the Garbage Collection ?
Or is it better to write a loop performing a NULL assignment for each element of the vector ?
[394 byte] By [
slash0] at [2007-9-26 12:22:32]

Yes friend,
clear method cleans up the hashtable and the vector. By clear it means that it will free up all the references it is holding. In case those objects are no longer referenced then they become eligible for garbage collection.
So, don't worry, and remember use LnkedList instead of Vector, they are effecient and memory optimized too.
Ashish
Yes friend,
clear method cleans up the hashtable and the vector. By clear it means that it will free up all the references it is holding. In case those objects are no longer referenced then they become eligible for garbage collection.
So, don't worry, and remember use LnkedList instead of Vector, they are effecient and memory optimized too.
Ashish
Thanks shrimalashish.
I've work on my side too checking the Clear method code.
Actually, it performs exactly the code I wanted to add.
No more, no less.
public void clear()
{
removeAllElements();
}
public synchronized void removeAllElements()
{
for(int i = 0; i < elementCount; i++)
elementData = null;
elementCount = 0;
}
My 2
/Slash
> Hi Gurus,
> Does the Clear method (hashtable, vector etc..) is a
> good option for garbage collection ?
> In other words, if I have a vector V with 100 elements
> and I perform a V.clear(), am I sure that the 100
> elements are now candidates for the Garbage Collection
> ?
> Or is it better to write a loop performing a NULL
> assignment for each element of the vector ?
If you want the objects in the Vector to become available for garbage collection, all you need to do is set the Vector itself to null. In fact, you almost never have to do that in actual Java code, because the Vector reference will be set to point somewhere else or will become unreachable.
It's almost always a mistake to write Java code specifically to make objects get collected. Just let the objects go away on their own.
Hi schapel,
I know it's not a good idea to force the garbage collector, but let me explain what I am confronted with.
(FYI, I didn't write the source code. Comes from a 3rd party)
The aim is to build a jsp page which is the list of the files contained in a web server directory.
I have a "Directory" class, and a "File" class. To build the jsp, each directory of the webserver is represented by a directory class, and each file, ...by a file class.
To simplify, when the tree structure of the web server is build in memory, I create a jsp page to list the files, and then, I do not need the vector anymore. But the vector can be quite huge and each client will build his own vector at the begening of his session (and will never use it again during the session).
Don't you think it's usefull in that situation to free some memory ?
> Don't you think it's usefull in that situation to free some memory ?
I never said that it's not a good idea to "force the garbage collector", or to free memory. I said that you nearly never have to do anything special to get that to happen. Nearly all the code I've seen to "help the garbage collector" is useless and leads to software that's slower, bigger, and harder to modify.
The most you have to do to get the garbage collector to free the Vector is to set the reference to it to null. However, unless the Vector is an element in an array and you need to keep the array but not the Vector, it probably pointless to do that. The reason is that the Vector itself will become unreachable anyway.