How to allocate memory in C# for loop?

俊仁 包 1 Reputation point
2020-06-11T13:24:05.89+00:00

Hi ,

The question is that I try to create an instance to a variable in a loop and don't use this variable.
Like this :
9842-1.png

Then I use the 'Diagnostic Tool' in VS to track the memory in managed heap , however , I get only a count of Foo type in heap without GC behavior , it makes me very confuse. Hence , I infere that there is a optimization at run time for the variable which don't use in context and this optimization in order to save memory to only allocate one time in managed heap as no impact for application. I'm very very very thank you to give me some advice for that as this question let me can't keep good sleep every day.
9807-2.png

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,260 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Dave Patrick 426.1K Reputation points MVP
    2020-06-11T13:25:25.333+00:00

    They're actively answering C# question in dedicated forums here.

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=csharpgeneral

    --please don't forget to Accept as answer if the reply is helpful--


    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    0 comments No comments