How to destroy arrays

S

Scott M.

Everything in .NET is a type (either reference or value types). All types
are managed by the Garbage Collector. There is no need to "free up memory"
as this is the purpose of the GC. There are certain circumstances though,
when you may want the object in question to fall out of scope immediately,
rather than waiting for the end of a procedure. In this case you could make
the object = Nothing.
 
J

Jon Skeet [C# MVP]

Scott M. said:
Everything in .NET is a type (either reference or value types). All types
are managed by the Garbage Collector. There is no need to "free up memory"
as this is the purpose of the GC. There are certain circumstances though,
when you may want the object in question to fall out of scope immediately,
rather than waiting for the end of a procedure. In this case you could make
the object = Nothing.

Usually that's unnecessary, however, as the garbage collector can tell
when a variable is last used in the IL. Unless you're in a particularly
special case (eg where a variable is used in the first iteration of a
long loop, and not thereafter) it's best not to clutter up your code
setting variables to null/Nothing.
 
C

Cor Ligthert

Hi Jon,

Maybe this is higher English; however can you tell me where your message is
different from that from Scott?

I found Scotts message very clear and I agree completely with him in the way
he wrote it.

Cor
 
N

Niki Estner

I think it's about code like:

{
SomeBigObject x = new SomeBigObject();
DoSomethingWithX(x);
// x = null;
DoSomethingElseThatTakesReallyLongButDoesntUseX();
}

Now, (as far as I got him) Jon tried to explain that there's NO use in
setting "x" to null/nothing, as the GC can tell it won't get used after line
2 anyway. I didn't read that in the previous post.

Of course, this only applies to local variables.

Niki
 
C

Cor Ligthert

Hi Niki,

I think not and although Scott did not mention it, what he told can count
even more for globally used objects.

I agree with Scott and Jon that it should not be done normally, however as
Scott already stated I believe that there are circumstances that it can be
done.

Cor
 
J

Jon Skeet [C# MVP]

Cor Ligthert said:
Maybe this is higher English; however can you tell me where your message is
different from that from Scott?

I found Scotts message very clear and I agree completely with him in the way
he wrote it.

Niki's message was spot on. Setting a variable to null/Nothing is less
useful than most people think - even for long-running methods, there
are only a few situations where it's of use.
 
N

Niki Estner

Quoted from Scott M.:
There are certain circumstances though,
when you may want the object in question to fall out of scope immediately,
rather than waiting for the end of a procedure. In this case you could make
the object = Nothing.

This implies that usually objects will be garbage collected when they fall
out of scope, that is, at the end of the procedure.
Strictly speaking, that's wrong for both local AND member variables: local
variables might get collected BEFORE the end of the procedure (as Jon
explained), and member variables survive the end of a procedure if they're
not set to null/nothing.

I guess Scott was aware of these facts, but nonetheless, the correction was
ok.

Niki
 
C

Cor Ligthert

Hi Jon,

Constructive quoting again from you, I once gave you a sample that more
people can do that however decent people do not do that.

Leaving the message from Scott out from this makes it the same as if Scott
was telling that the Tower Bridge was in Paris.

The message you give the same answer again as Scott was saying, while you
give the idea that he did not.
 
S

Sarfraz Hooda

Hi,

I have created an array of Objects in a collection. I was wondering is there
a way to destroy the array to free up the space in the memory ? or they are
automatically destroyed and garbagge collected by .Net framework?

Sarfraz
 
J

Jon Skeet [C# MVP]

Cor Ligthert said:
Constructive quoting again from you, I once gave you a sample that more
people can do that however decent people do not do that.

Leaving the message from Scott out from this makes it the same as if Scott
was telling that the Tower Bridge was in Paris.

Sorry if you thought I was implying that - I certainly wasn't, and I
don't actually think anyone thought I was.
The message you give the same answer again as Scott was saying, while you
give the idea that he did not.

No, it didn't. It gave more information, notably that the GC is capable
of detecting variables after their last use within a method in most
situations - it *automatically* makes objects (where appropriate)
eligible for garbage collection before the end of the method. This is
not what Scott implied (in the way that Niki and I read it at least)
when Scott wrote:

"There are certain circumstances though, when you may want the object
in question to fall out of scope immediately, rather than waiting for
the end of a procedure."

To me, that suggests that unless you take the given action, the method
(procedure in Scott's terminology) has to end before the variable's
contents will be collected.

However, I suspect that this whole subthread isn't really about garbage
collection at all, and that you'd have praised Scott for adding extra
information if the posts had been the other way round... (And that if
it had been someone else replying to Scott, you wouldn't have posted at
all.)
 
J

Jon Skeet [C# MVP]

Brian Davis said:
In addition, VB has an Erase statement, which should deallocate memory used
for the array's elements.

I don't *believe* it does, actually. It just sets each array element to
Nothing, which is the equivalent of calling Array.Clear. That's
different from actually deallocating the memory, which is the garbage
collector's job.
 
B

Brian Davis

I would tend to agree with you. What made me think otherwise is that the
MSDN documentation states that the Erase Statement is "Used to release array
variables and deallocate the memory used for their elements," so I am not
really sure. If I had to guess, I'd say that you are correct, and that MSDN
just means that it is marked as OK for deallocation by the GC rather than
actually deallocated.

As a side note, Erase isn't the same as calling Clear. Clear will set
elements of the array to Nothing, while Erase sets the array variable itself
to Nothing.

Either way, setting the array to Nothing should be functionally the same as
calling Erase, but, as you have pointed out in previous posts, that
shouldn't be necessary in most applications.

Brian Davis
http://www.knowdotnet.com
 
J

Jon Skeet [C# MVP]

Brian Davis said:
I would tend to agree with you. What made me think otherwise is that the
MSDN documentation states that the Erase Statement is "Used to release array
variables and deallocate the memory used for their elements," so I am not
really sure. If I had to guess, I'd say that you are correct, and that MSDN
just means that it is marked as OK for deallocation by the GC rather than
actually deallocated.

It wasn't even marking things as OK for deallocation in my (wrong)
understanding. It was just setting things to Nothing inside the array,
which may well not make them eligible for GC, as there could be other
references.

The MSDN is unfortunately not very clear (or, I believe, accurate) on
this. Looking at the IL generated, I believe it does just set the
variable to Nothing, which certainly doesn't fit in with the docs you
quoted. Odd.

So

Erase Foo, Bar Baz

is just the same as

Foo = Nothing
Bar = Nothing
Baz = Nothing

It seems relatively useless to me, to be honest. Maybe it was more
useful in VB6.
As a side note, Erase isn't the same as calling Clear. Clear will set
elements of the array to Nothing, while Erase sets the array variable itself
to Nothing.

Yup. You're absolutely right - I had completely misread the spec.
Sincere apologies for confusing things further :(
Either way, setting the array to Nothing should be functionally the same as
calling Erase, but, as you have pointed out in previous posts, that
shouldn't be necessary in most applications.

Yup.
 
C

Cor Ligthert

Hi Jon,

I would have answered as well as someone else had written the message you
did, however they seldom do. (Probably the tone had been something different
because arguing between us is not something new, therefore I did leave out
the introduction.)

:)

In my opinion Scott told it very clear. I did not read anything in your
message that would have given the OP more information when this arguing had
not been.

In my opinion there is a scene where setting an array to nothing can be good
and I do not believe that that is contradicting what Scott wrote.

In pseudo to make it language independent

Arraylist MyArray

Main
MakeNewArray

MakeNewArray
MyArray = New ArrayList
for i=0;i<5000000;i++ Arraylist.add(myobject)

Event Button1Click
MyArray = nothing

Event Button2Click
MakeNewArray

In my opinion I give the GC the change to clean up the old MyArrayObjects
before the program is ended.

Cor
 
J

Jon Skeet [C# MVP]

Sarfraz Hooda said:
Is there anything equivalent to Erase in C#?

There's an equivalent to what Erase *actually* does, which is just
setting the variable to null:

object[] foo = new object[100];
....

foo = null;

This is very rarely useful, as discussed elsewhere in the thread. (See
also http://blogs.msdn.com/csharpfaq/archive/2004/03/26/97229.aspx )

Of course, setting an instance or static variable to null is slightly
different - but for instance variables, I usually find that the array
is useful for exactly the same length of time as the object itself, so
again there's rarely a point in setting the variable to null.

Note that Erase doesn't automatically make an array eligible for
garbage collection - if there's another reference to the same array, it
can't be collected until that reference has changed (to null or to a
different array) or become eligible for garbage collection itself.
 
J

Jon Skeet [C# MVP]

Cor Ligthert said:
In my opinion Scott told it very clear. I did not read anything in your
message that would have given the OP more information when this arguing had
not been.

Really. Having read only Scott's post, when would you believe Foo was
eligible for garbage collection in the Sub below?

Public Shared Sub Test()
Dim Foo(9) As Integer

Console.WriteLine(Foo(0))
Console.WriteLine("Hello")

MessageBox.Show ("Can it be collected now?")

End Sub

If I had just read Scott's post (and didn't know anything else) I would
expect the array not to be eligible for garbage collection until the
end of the Sub. In fact (in release mode), it's eligible for garbage
collection immediately after Foo(0) has been evaluated.
In my opinion there is a scene where setting an array to nothing can be good
and I do not believe that that is contradicting what Scott wrote.

No, and that's also not contradicting anything I wrote. I didn't
contradict what Scott wrote - I added to it.
 
C

Cor Ligthert

Hi Jon,
Really. Having read only Scott's post, when would you believe Foo was
eligible for garbage collection.

This is the message from Scott

Where Scott did write about the place it was eligible for the GC?

When the method falls out of scope, it does not always mean that an (in that
method created) object is collectable for the GC, however Scott did not
write that either.

This was your answer
Usually that's unnecessary, however, as the garbage collector can tell
when a variable is last used in the IL. Unless you're in a particularly
special case (eg where a variable is used in the first iteration of a
long loop, and not thereafter) it's best not to clutter up your code
setting variables to null/Nothing.

It did, at that moment you wrote it, in my opinion Add nothing to the
question of the OP:

"How to destroy arrays", your message tells in a way where an array is
destroyed by the program (not for the system).

And that was for me already clear in the explanation from Scott. Why would
it be set to nothing at the end of the procedure (method)

Cor
 

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