开发者

Declaring variables with New DataSet vs DataSet

开发者 https://www.devze.com 2022-12-26 21:46 出处:网络
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?

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开发者_Python百科 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号