Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Array Destroy

Tags:

java

arrays

null

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

like image 357
Alaattin KAYRAK Avatar asked Jan 27 '26 02:01

Alaattin KAYRAK


2 Answers

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.)

like image 125
Dan Breslau Avatar answered Jan 28 '26 16:01

Dan Breslau


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.

like image 38
Tyler Avatar answered Jan 28 '26 16:01

Tyler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!