tags:

views:

3403

answers:

8

Im sorry if this is an obvious question but neither google or a search here led me to an answer.

Is there a way to remove an array entirely?

I want the opposite of int[] array = new int[5]

+2  A: 

Remove? Set it to null:

array = null;
Otávio Décio
Well, that makes it *eligible* for collection (assuming there are no other references to the same array); it doesn't actually collect it...
Marc Gravell
Granted, but the question wasn't too precise.
Otávio Décio
Setting to null is pointless. "Even if you add “aC = null;” after your usage of it, the JIT is free to consider this assignment to be dead code and eliminate it." - http://blogs.msdn.com/cbrumme/archive/2003/04/19/51365.aspx
Greg Beech
+6  A: 

You just have to let it go out of scope, and wait for GC to find it; which might not be immediately (in fact, it almost certainly won't be). If you have a field on a long-lived object (that is going to stay in scope), then you can set to null, which can help.

You can influence the GC to collect sooner (not just your object: everything eligible), but you should rarely if ever do this. I use it only in test rigs; but:

GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); // DON'T DO THIS!!!

for more on GC.Collect:

Marc Gravell
+1 for telling GC.
codemeit
+2  A: 

There is no need to delete the array, the GC will deal with it.

(Very) Simplistically:

When you run out of memory the garbage collector will kick in, stop your code and traverse all the live objects in memory.
live means some reference on the stack, a register, a static reference and some other things known collectively as 'GC Roots'. As it traverses them it notes that they are alive.

If your array is no longer live there is no way anything could access it anymore (that's what determines liveness) so the memory it occupied will be available for reuse.

There may be a reason to assign the reference to null if it would hold references alive longer than is desired or it is holding a large chunk of memory that you need immediately but this can easily backfire and actually make the array live longer. Instance rather than stack variables are better candidates for this optimisation if the containing instance will have considerably longer life than the array it contains.

To be clear, as a beginner you should not be considering this sort of thing, let the GC do it's job and only try to help it when you:

  1. know you need to
  2. know how to
  3. know how to check you did it right
ShuggyCoUk
+1  A: 

array = null, but I think it'll be a good idea if you read a little about the .NET memory management model to fully understand why.

Trap
Do I really deserve a downvote for this?
Trap
I up voted as I don't think you deserved the down vote :)
Gautam
Well, thanks! :D
Trap
Arguably you do deserve a down-vote, because it's wrong. Read the part about assigning a reference to null being dead code which the JIT is free to eliminate here: http://blogs.msdn.com/cbrumme/archive/2003/04/19/51365.aspx
Greg Beech
(BTW - I didn't down-vote because the suggestion of reading more about the .net memory model is a good one)
Greg Beech
Well, you're right, I should have made it clear that I was referring more to the "I want the opposite of int[] array = new int[5]" statement instead of the actual question.
Trap
+10  A: 

Say you call:

 void Foo(){
     int[] a = new int[5]
 }

In C# there is no way to undefine the variable a. That means a will be defined in Foo even if you set a to null. However, at the end of Foo a will fall out of scope. That means no code can reference it, and the garbage collector will take care of freeing the memory for you the next time it runs, which might not be for a long time.

RossFabricant
Gotta love the definite answers :) Thanks a bunch for saving me from googling it much longer.
Patrik Björklund
A: 

The closest you get is just to wait for it getting out of scope or setting it to null.

It won't remove or delete the array immediately, but after you drop all references to the array, the garbage collector (GC) will delete it in time...

Arjan Einbu
+1  A: 

Just to clarify in case you don't understand some of the terminology expressed in the other answers, GC stands for garbage collector. Languages like C# that have automatic memory management allow you to focus on doing what needs to be done and not worry about what you're creating and when to delete it. The GC works by periodically going through all of the objects you create and seeing whether they have any references to them. If not, this means there's no way you can do anything with them, so they might as well be deleted, and the system does just that.

For more details, see http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)

mandaleeka
Exactly. C# is not C++.
Dana Robinson
+1  A: 

You should read Chris Brumme's article on the subject; the first few paragraphs should explain the situation.

And anybody suggesting "assign null to the variable" should take particular note of the part that says:

Even if you add “aC = null;” after your usage of it, the JIT is free to consider this assignment to be dead code and eliminate it.

Greg Beech