Array allocation and garbage collection

  • Thread starter Thread starter Jon
  • Start date Start date
J

Jon

If I allocate an array, eg:

double[] x = new double[10000];

then use the array, then at some later point, allocate it again, eg:

double[] x = new double[15000];

does the garbage collector know that the memory allocated the first time round can be garbage
collected, or do I have to set x to null first before allocating the second time around?
 
If I allocate an array, eg:

double[] x = new double[10000];

then use the array, then at some later point, allocate it again, eg:

double[] x = new double[15000];

does the garbage collector know that the memory allocated the first time round can be garbage
collected, or do I have to set x to null first before allocating the second time around?

I assume the latter line was meant to just be:

x = new double[15000];

otherwise you're declaring a different variable.

However, the garbage collector is smart enough to know that if nothing
currently refers to the array, it can be garbage collected. Setting
the variable to null before setting it to another value wouldn't make
any difference (beyond being more fluff to understand, basically).

Jon
 
Hi,

We need more details, is that particular line of code inside a method? or
inside an instance?
The way you declare it seems like this is inside a method.

The GC collects any memory that is not being referenced. as soon as an
entity (like double[1000]) is not longer accesible it will be ready to be
collected.
 
Thanks Jon and Ignacio,

Jon:
"I assume the latter line was meant to just be: x = new double[15000];"
Yes, sorry about this mistake.

Ignacio:
It's defined within a class, as part of the member variables section, then it is used and
re-allocated in various different methods within that class.

class Class1{
double[] x = new double[10000];
...
method1(){
// use x
}
method1(){
// use x
}
method1(){
x = new double[15000];
}



"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.com> wrote in message
Hi,

We need more details, is that particular line of code inside a method? or
inside an instance?
The way you declare it seems like this is inside a method.

The GC collects any memory that is not being referenced. as soon as an
entity (like double[1000]) is not longer accesible it will be ready to be
collected.
 
Hi,




Jon said:
Thanks Jon and Ignacio,

Jon:
"I assume the latter line was meant to just be: x = new double[15000];"
Yes, sorry about this mistake.

Ignacio:
It's defined within a class, as part of the member variables section, then
it is used and
re-allocated in various different methods within that class.

class Class1{
double[] x = new double[10000];
...
method1(){
// use x
}
method1(){
// use x
}
method1(){
x = new double[15000];
}

The question is, do more than one method use the same values of X or you
create it each time you start a new method?
 
Jon said:
It's defined within a class, as part of the member variables section, then it is used and
re-allocated in various different methods within that class.

class Class1{
double[] x = new double[10000];
...
method1(){
// use x
}
method1(){
// use x
}
method1(){
x = new double[15000];
}

Yes.

When you reallocate the original 10000 element array will
be ready for GC unless you somehow managed to get another
variable (that are still reachable) to point to it.

Arne
 
Jon said:
However, the garbage collector is smart enough to know that if nothing
currently refers to the array, it can be garbage collected.

Refer as in reachable not as in points to.

Arne
 
Thanks everyone for your replies, it's now very clear to me.

Jon


"Jon" <.> wrote in message If I allocate an array, eg:

double[] x = new double[10000];

then use the array, then at some later point, allocate it again, eg:

double[] x = new double[15000];

does the garbage collector know that the memory allocated the first time round can be garbage
collected, or do I have to set x to null first before allocating the second time around?
 
Back
Top