When I set a static variable to null, will be available for GC ?
static Integer jfv;
boolean someMethod(){
jfv =10;
//use for some purpose
jfv = null
return true;
}
In this case, will jfv be picked by GC ?
Edit:
There was typo, the question was is static object available for GC after assigning null values.
Thanks for all the responses
What jvf
used to contain would be available for reclamation, but not its current value.
That is, if an int
were an object, which it isn't.
You cannot assign null to an int
variable.
But what you are trying to do is wrong-headed anyway.
The int
type is a primitive type ... and that means that int
variables not ever have an independent existence in the Java heap. Hence there is nothing to be garbage collected.
Now if jfv
was a reference type (ie its type was a class or an array type), then assigning null
to it:
jfv
refers to being a candidate for garbage collection. Note that the last bullet is qualified:
If the object that jfv
refers to is reachable from other places, then it won't be a candidate for garbage collection.
The fact that something is a candidate for garbage collection doesn't mean that it will be garbage collected. It depends when the collector is run ... and some other things.
Finally, it is generally a bad idea to try to influence / control when Java objects are garbage collected. Just let the garbage collector deal with it. There are some exceptions to this; read about Java memory leaks.
Incorrect use of static
variables is a common cause of memory leaks ... but randomly assigning null
to statics is not the solution.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.