Force VS to garbage collect


Here’s another little gem I’ve got for you.

As you probably know, Visual Studio is a big product, with contributions coming from many many different teams, and languages. A big part of Visual Studio is written in managed languages these days, and there are a ton of 3rd party components that you can download and install from the VS Gallery that are also written in managed languages. As such, there are lots of developers out there writing managed components targeting the Visual Studio “runtime” if you will.

When testing code that is run in a managed runtime, sometimes it is quite helpful to force the runtime to garbage collect. The CLR has always had API that you could use to programmatically force a GC, but wouldn’t it be great if there was a way to force the runtime inside Visual Studio’s address space to collect via a simple keystroke?

Well there is. Ctrl-Alt-Shift-F12, twice, does exactly that.

So again, you have to hold down ctrl-Alt-Shift and then hit F12 twice in order to make Visual Studio garbage collect.

UPDATE ( 12/16/2010 ):

One thing I should have mentioned when I originally posted, was that if you don’t like that default mapping, or a 3rd party has overridden that mapping and you can’t find what key stroke the ForceGC command is mapped to, you can always check the Visual Studio key mappings by selecting Tools->Customize…, then hit the “Keyboard…” button:

SNAGHTMLb87c29f

…which takes you to the “Options” dialog where you can type “ForceGC” in the “Show Commands” text box and remap to whatever keystroke you’d like!

SNAGHTMLb89379f

Enjoy!

Cameron

Comments (9)

  1. Bob says:

    Could you give a (possibly contrived) scenario when this might be helpful to do?

  2. For example, you're building version 2 of a component that runs in VS and you are trying to get a quick / rough sense on how the memory utilization is going vis-a-vis version one, that sort of thing…

  3. mazhou says:

    Very helpful post, especially to those who usually open tens of Visual Studio instances simultaneously for heavy development work.

  4. Alfred says:

    Key combo doesn't seem to work with CodeRush Express installed.  Do you know what it's remapped to?

  5. Double check the Options window to see what it is currently mapped to. I just updated the post with the information. Thanks for the question! 🙂

  6. huseyint says:

    Let me give you another shortcut which will collect all the VS garbage: "Alt + F4" (Pun intended)

  7. Aaron Hightower says:

    For whatever reason this trick made my visual studio dev environment stop being super slow.  It had been bugging me for months.  I found another thread that also pointed me here .. Visual Studio 2010 would be a better product if it were not required to do this to perform optimally, but by many reports, this makes a huge difference in how usable the environment really is for people who use VS2010 a lot and or very frequently.

  8. Residential Trash Removal Service says:

    Very helpful post, especially to those who worked on heavy development work. Thanks for this nice blog.

  9. NB says:

    Too bad it's not implemented in VS2005.

    But maybe that is a bit slimmer than the following versions.