3

I'm working on application scalability, and I'm wondering how to monitor memory management (in particular "leaks"), and ensuring that threads are created and destroyed properly. Are there tools within visual studio for doing that?

sircodesalot
  • 11,231
  • 8
  • 50
  • 83
  • There's the visual studio profiler and static analysis. There are also third party products from Telerik and Jetbrains. – Dustin Kingen May 06 '13 at 14:32
  • Well, a thread is destroyed when it finishes executing the delegate you gave it, as soon as it's done it is destroyed, there's nothing you need to do to ensure that happens. Just don't have a method doing nothing useful if you have nothing useful to do. – Servy May 06 '13 at 14:32
  • Take a look here: http://stackoverflow.com/questions/3927/what-are-some-good-net-profilers – Ahmed KRAIEM May 06 '13 at 14:34
  • `Well, a thread is destroyed when it finishes executing the delegate you gave it`. That's really the source of this question. I thought that should be the case, and that the system should relinquish the memory associated with said threads when the threads exit, but for some reason the memory footprint doesn't downscale like I assumed it would. – sircodesalot May 06 '13 at 14:36
  • 1
    @sircodesalot Are you getting out of memory errors? Is your program crashing? If not, it probably just hasn't bothered to clean things up yet. – Servy May 06 '13 at 14:38
  • I was, but after some refactoring it doesn't get out of memory exceptions any more, it just doesn't seem to be releasing memory after the stress test. Unless I misunderstand something about how garbage collection should work. – sircodesalot May 06 '13 at 14:40
  • @sircodesalot Call `GC.Collect`. (But only for debugging purposes; don't leave any `Collect` calls in production code.) If your memory is cleaned up it means the GC just hasn't bothered to clean it up yet. If it doesn't, it means you're doing something to hold onto large chucks of memory and aren't allowing the GC to free it. If that's the case be sure you're disposing of `IDisposable` resources, and then use a service such as below to see what's consuming most of your memory to see what isn't being freed. Note that that's unlikely to be threads not being feed, it's just be objects. – Servy May 06 '13 at 14:42
  • `If it doesn't, it means you're doing something to hold onto large chucks of memory and aren't allowing the GC to free it.` I'm pretty sure this. So I'm trying to figure out what I'm holding on to. Thanks for the suggestion though. – sircodesalot May 06 '13 at 14:43

2 Answers2

2

You can use Windows' performance monitor (perfmon) and add counters for .NET CLR Memory category, and restrict it to your program's instance.

David S.
  • 5,965
  • 2
  • 40
  • 77
2

Visual Studio -> Analyze -> Start Performance Analysis.

enter image description here

This will give you all the critical points within your application. This will also give you a log that tells you how long every method takes.

There is no need for Disposing the thread, because the Thread class doesn't contain a Dispose function.

You can catch the moment when your Thread ends by using the threadname.Join(); function.

Max
  • 12,622
  • 16
  • 73
  • 101