Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I cannot see my strings on the heap while debugging

I am trying to debug a memory issue in a .NET application working with very large strings. To do that, I'd like to set some break points and analyse the heap at various stages of the execution.

The problem is: When using the diagnostic tools of Visual Studio 2015, it seems that large strings on the heap are only shown when starting the application without debugging. That's inconvenient, because it prevents me from setting break points.

Question: Is that a known bug? Or am I using the diagnostic tools incorrectly?


How to reproduce

  1. Create a new C# Console application (.NET 4.6.1) with the following code:

    using System;
    using System.Threading;
    
    class Program
    {
        static void Main(string[] args)
        {
            string test1 = new string('a', 100000000);
            Thread.Sleep(2000);
            string test2 = new string('a', 100000000);
            Thread.Sleep(2000);
            string test3 = new string('a', 100000000);
    
            Console.WriteLine("Done");
            Console.ReadLine();
        }
    }
    
  2. Activate the diagnostic tools (Ctrl+Alt+F2) and start the application with debugging (F5). Observe the memory usage and notice that each string allocation increases the memory usage by 200MB. After the memory usage has reached it's maximum, take a memory snapshot and view the heap.

  3. Observe that the object type taking the most memory on the heap is some icon with 10 KB. Obviously, that's incorrect.

with debugging

  1. Stop the application. Start it again with diagnostic tools but without debugging (Alt+F2). Select "Memory Usage" and start.

  2. Again, note the increase in memory, take a snapshot and have a look at it.

  3. Observe that the three large strings are shown (as it should be).

enter image description here

like image 845
Heinzi Avatar asked Jun 21 '16 15:06

Heinzi


People also ask

How to see variables when debugging Visual Studio?

The most commonly used way to look at variables is the DataTip. When stopped in the debugger hover the mouse cursor over the variable you want to look at. The DataTip will appear showing you the value of that variable.

How do I change the value while debugging in Visual Studio?

Use the Edit and Continue command while debugging in Visual Studio so that you can make changes to your source code while your program is in break mode.


1 Answers

I found a way to see the strings even when debugging. For some strange reason, the default option "Just my code" hides those strings when debugging:

just my code

Removing the check mark makes the strings visible.

Apparently, this is only a problem when debugging. As the second screenshot in the question shows, in the "not debugging" case, the strings are shown even with "Just my code" turned on.

like image 153
Heinzi Avatar answered Oct 04 '22 20:10

Heinzi