At some point in my code I want to nullify a Collection object. Is it possible for memory leak by doing so?
Set<String> set = new HashSet<String>("Test");
...
...
set = null;
if(set == null){
...
}
You don't nullify an object - you change a variable's value to null
. It's really, really important that you understand the difference between objects, variables and references.
That's rarely useful, but it would be extremely odd to lead to a variable leak in itself. I can't immediately think how that could happen - unless you had some nullity check which changed your behaviour regarding other data, of course. It may make the collection that the previous variable value referred to eligible for garbage collection, or it may not - it depends on whether anything else still has a reference to it.
See more on this question at Stackoverflow