Array allocation and garbage collection

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?
 
J

Jon Skeet [C# MVP]

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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

Jon

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

Ignacio Machin \( .NET/ C# MVP \)

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?
 
A

Arne Vajhøj

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
 
A

Arne Vajhøj

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
 
J

Jon

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top