9

I suppose this is a very silly question but I've been looking around and couldn't find answers on the following questions. Really appreciate answers shedding light on this.

1) What happens to the previous object if instantiating a new one within the same method. Example:

DataTable dataTable = new DataTable();
dataTable = new DataTable(); // Will the previously created object be destroyed and memory freed?

2) Same question as (1) but on static variable. Example:

static private DataView dataView;

private void RefreshGridView()
{
    dataView = new DataView(GetDataTable()); // Will the previously created objects be destroyed and memory freed?
    BindGridView();
}

Thanks!

1
  • I realize that the code sample in (1) is a simplification of something more complicated, but I just thought I would comment that it resembles an irritating coding practice that I see all the time, usually in the form of assigning a value returned by a function to the variable immediately after the initial new object assignment in the variable declaration. There's no point in doing the first assignment (unless the constructor causes some kind of side effect, which would be a very poor/questionable implementation anyway). Commented Jul 10, 2013 at 17:25

3 Answers 3

11

// Will the previously created object be destroyed and memory freed?

Potentially. As soon as you do this, you'll no longer be holding a reference to the original DataTable. As long as nothing else references this object, it will become eligible for garbage collection. At some point after that, the GC will run, and collect the object, which will in turn free it's memory.

This is true for static, instance, and local variables. The mechanism is identical in all of these scnearios.

Note that if the object you're referencing implements IDisposable, it's a good practice to call Dispose() on it before losing the reference. Technically, a properly implemented IDisposable implementation will eventually handle things properly, but native resources may be tied up until the GC collection occurs, which may not happen quickly. Note that this has nothing to do with (managed) memory, but is still a good practice.

That being said, your first example is a bad practice. While the memory will (eventually) get cleaned up, you're performing extra allocations that serve no purpose whatsoever, so it'd be better to not "double allocate" the variable.

Sign up to request clarification or add additional context in comments.

1 Comment

Understand, thanks for the elaborate answer in such a short time!
2

The short answer is that all of this is handled by the garbage collector. The instances won't be immediately deleted, but they will be marked as "unreachable" and freed at a later time.

I suggest having a read through the Garbage Collection article at MSDN

1 Comment

Thanks for the quick answer and the knowledge source! =)
1

Objects are reference variable in C#. That means they store memory references in them.

So when you re-assign an object, you simply, overwrite the earlier value (memory reference) that it held. And the earlier value is now qualified for Garbage Collection. Now its Garbage Collector's job to free that memory. Same is for all kinds of variables including static

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.