Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 4
System
GC Class
GC Methods
Collect Method
 Collect Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4

Other versions are also available for the following:
.NET Framework Class Library
GC..::.Collect Method

Forces an immediate garbage collection of all generations.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic
Public Shared Sub Collect
C#
public static void Collect()
Visual C++
public:
static void Collect()
F#
static member Collect : unit -> unit 

Use this method to try to reclaim all memory that is inaccessible.

All objects, regardless of how long they have been in memory, are considered for collection; however, objects that are referenced in managed code are not collected. Use this method to force the system to try to reclaim the maximum amount of available memory.

The following example demonstrates how to use the Collect method to perform a collection on all generations of memory. The code generates a number of unused objects, and then calls the Collect method to clean them from memory.

Visual Basic
Imports System

Namespace GCCollect_Example
    Class MyGCCollectClass
        Private Const maxGarbage As Integer = 1000

        Shared Sub Main()
            'Put some objects in memory.
            MyGCCollectClass.MakeSomeGarbage()
            Console.WriteLine("Memory used before collection: {0}", GC.GetTotalMemory(False))

            'Collect all generations of memory.
            GC.Collect()
            Console.WriteLine("Memory used after full collection: {0}", GC.GetTotalMemory(True))
        End Sub

        Shared Sub MakeSomeGarbage()
            Dim vt As Version

            Dim i As Integer
            For i = 0 To maxGarbage - 1
                'Create objects and release them to fill up memory
                'with unused objects.
                vt = New Version()
            Next i
        End Sub
    End Class
End Namespace
C#
using System;

namespace GCCollectExample
{
    class MyGCCollectClass
    {
        private const int maxGarbage = 1000;

        static void Main()
        {
            // Put some objects in memory.
            MyGCCollectClass.MakeSomeGarbage();
            Console.WriteLine("Memory used before collection: {0}", GC.GetTotalMemory(false));

            // Collect all generations of memory.
            GC.Collect();
            Console.WriteLine("Memory used after full collection: {0}", GC.GetTotalMemory(true));
        }

        static void MakeSomeGarbage()
        {
            Version vt;

            for(int i = 0; i < maxGarbage; i++)
            {
                // Create objects and release them to fill up memory
                // with unused objects.
                vt = new Version();
            }
        }
    }
}
Visual C++
using namespace System;
const int maxGarbage = 1000;
void MakeSomeGarbage()
{
   Version^ vt;
   for ( int i = 0; i < maxGarbage; i++ )
   {

      // Create objects and release them to fill up memory
      // with unused objects.
      vt = gcnew Version;

   }
}

int main()
{

   // Put some objects in memory.
   MakeSomeGarbage();
   Console::WriteLine( "Memory used before collection: {0}", GC::GetTotalMemory( false ) );

   // Collect all generations of memory.
   GC::Collect();
   Console::WriteLine( "Memory used after full collection: {0}", GC::GetTotalMemory( true ) );
}

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Does 'all generations' also mean the Large Object Heap?      Steve Dunn   |   Edit   |   Show History
Does 'all generations' also mean the Large Object Heap? Perhaps the sample could be updated to create something on the LOH too.$0
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker