views:

57

answers:

2

What is the impact of creating variables using:

Dim ds as New DataSet
ds = GetActualData()

where GetActualData() also creates a New DataSet and returns it?
Does the original empty DataSet that was 'New'ed just get left in the Heap?

What if this kind of code was in many places? Would that affect the ASP.NET process and cause it to recycle sooner?

+1  A: 

The memory allocated with Dim ds as New DataSet will be garbage collected when GC.Collect() is performed.

Simply - when ds allocates new memory in the method, the old memory won't be referenced from anything and will be garbage collected when it comes time to.

Svetlozar Angelov
I understand that, however, my question is whether having hundreds of these New DataSet calls where they aren't necessary a major issue or are they really no big deal and the GC will take care of them.
eych
Not leaving garbage would postpone the GC.Collect(), after all if the memory gets short it will GC.Collect() and the performance would be hit. Having unnecessary new DataSets are something you should take care of.
Svetlozar Angelov
+1  A: 

The impact is that you are creating more objects than you need to.

The unused object will be left on the heap, so it will need to be garbage collected eventually. If you do this a lot, it will result in the garbage collector having to run more frequently.

Compared to the real work done it should however be minimal, i.e. actually populating a data set is so much more work that the extra unused object is almost negligible.

It won't cause the ASP.NET process to recycle sooner, as object throughput / GC frequency is not a factor.

Eventhough the impact is minimal, the code should of course not create those unused objects. Perhaps more important than the performance is that the code doesn't really correspond with what you want to accomplish. Every little discrepancy like that makes the code harder to maintain.

Guffa