Share via


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

  • Anonymous
    December 15, 2010
    Could you give a (possibly contrived) scenario when this might be helpful to do?
  • Anonymous
    December 15, 2010
    The comment has been removed
  • Anonymous
    December 15, 2010
    Very helpful post, especially to those who usually open tens of Visual Studio instances simultaneously for heavy development work.
  • Anonymous
    December 16, 2010
    Key combo doesn't seem to work with CodeRush Express installed.  Do you know what it's remapped to?
  • Anonymous
    December 16, 2010
    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! :)
  • Anonymous
    December 16, 2010
    Let me give you another shortcut which will collect all the VS garbage: "Alt + F4" (Pun intended)
  • Anonymous
    January 27, 2011
    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.
  • Anonymous
    May 13, 2011
    Very helpful post, especially to those who worked on heavy development work. Thanks for this nice blog.
  • Anonymous
    November 06, 2011
    Too bad it's not implemented in VS2005.But maybe that is a bit slimmer than the following versions.