0

I'm a little confused with the following scenario.

If there is an array created like so:

Declaration:

int[] array1;

Instantiation:

array1 = new int[500];

But then, later on, I no longer require 500 elements, so I simply create a new array with the new size, like so:

array1 = new int[50]

There are various references around the code that access this array, like so:

for (x=0;x<array1.length;x++){
array1[x]+someValue
}

What happens to the memmory where the other 450 elements were? The array itself is still 'alive' is it not because I still have lots of references to it's name, so it can't be GC'd, so I'm a little confused how the GC'd works in this case.

Note I know that I should have used an arrayList in order to re-size, unfortunately, this isn't an option for me now as it would be a ton of work to change my code (18 classes in size) so I want to stick to array's at least for this project and manage them as best I can.

Kara
  • 6,115
  • 16
  • 50
  • 57
Zippy
  • 3,826
  • 5
  • 43
  • 96

3 Answers3

2

In your example, you're simply reusing a variable, array1 to reference multiple objects throughout the execution of your code. This does not mean that every object you reference with array1 will be held in memory forever.

Try thinking of it this way. When you assign array1 = new int[500] you are allocating an array of size 500 somewhere in memory and referencing that chunk of memory with a variable named array1:

image of array1 pointing to the first array

Later, you assign array1 = new int[50], so array1 is now holding a reference to an entirely different object:

image of array1 pointing to the second array

The first array is eligible for garbage collection because nothing holds a reference to it anymore.

DannyMo
  • 11,344
  • 4
  • 31
  • 37
  • Instead of thinking of `array1` as a "reference" itself, it might help to think of it as a variable that _holds_ a reference. At first it holds a reference the size 500 array, then it holds an entirely different reference to the size 50 array (at which point the first reference is lost). – DannyMo Aug 05 '13 at 18:41
1

The old one will be waiting for GC to kick in and to be collected as long as there's no reference to older ones. With new you will create a complete new placeholder for the array.

Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
  • Ah, now this is what is confusing me - by 'reference' I assume you mean 'array1'? As the new array has the same name, there are references to it all over the code (as we still need to reference it even though we're now accessing the new array space). So is the old memory space eligible for GC? – Zippy Aug 05 '13 at 17:58
  • @Zippy GC will clean up any reference. Nothing to worry about. – Sajal Dutta Aug 05 '13 at 18:01
  • @Zippy I don't think you have something like array2 = array1; That's a reference. – Sajal Dutta Aug 05 '13 at 18:17
  • No I don't have anything like that - but what would happen if I did? If I wanted to do what you say (array2 = array1) but I wanted to now make array2 = (the new) array1 - does this mean the old memory would not be collected even though it was not longer being used - if you see what I'm saying? Thanks – Zippy Aug 05 '13 at 18:23
  • If you do, array2 = array1. array2's previous reference is ready to be GC'd. As I said you do not need to worry about it. – Sajal Dutta Aug 05 '13 at 18:25
0

Java has a magical mechanism called garbage collection that will reuse the memory of the old array and assign you a new one. All the original contents of the array with 500 elements will be lost when you call new again though.

David Elliman
  • 1,379
  • 8
  • 15
  • Hi @DavidElliman, yep I just wasn't sure how the Garbage Collector worked in this case as the 'reference' to the array is still there if you see what I mean! So you're saying that because the reference now points to a new memory location, the old memory is automatically disassociated with this reference and thus becomes eligible for collection? This is what I thought would happen but I wasn't really sure and I want to avoid leaks :-) – Zippy Aug 05 '13 at 17:55