2

If I use "Array_Name[] = NULL", does the garbage collector of java collect the rest of the array?

2
  • 2
    Minor point: by Array_Name[] = NULL you mean Array_Name = null, right? Commented Dec 28, 2010 at 2:30
  • 2
    Really should be arrayName = null ... style is important too. Commented Dec 28, 2010 at 2:32

2 Answers 2

13

Only if Array_Name had actually been referencing an array to begin with. And only if there are no other references to the array. And it will reclaim only those elements of the array which aren't referenced by anything outside of the array. And it will only do so when it feels like getting around to it :-)

(The syntax Array_Name[] = NULL isn't really meaningful. But I'm assuming you'd done something like:

 Foo[] Array_Name = new Foo[n];
 //...
 Array_Name = null; // Note the lowercase "null"

This might make the array a proper target for garbage collection, given the conditions I described above.)

Sign up to request clarification or add additional context in comments.

1 Comment

+1 ...it will only do so when it feels like getting around to it... (That is, the GC is largely non-deterministic -- from the point of Java code -- when it runs.)
2

I agree with everything Dan mentioned above but you can actually trigger garbage collection manually by calling System.gc(). Of course as mentioned this will only collect objects that are no longer referenced by anything else.

2 Comments

You might want to mention that calling System.gc() is highly discouraged.
I agree, I'm just presenting the option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.