Export (0) Print
Expand All
2 out of 4 rated this helpful - Rate this topic

GC.GetTotalMemory Method

Retrieves the number of bytes currently thought to be allocated. A parameter indicates whether this method can wait a short interval before returning, to allow the system to collect garbage and finalize objects.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public static long GetTotalMemory (
	bool forceFullCollection
)
public static long GetTotalMemory (
	boolean forceFullCollection
)
public static function GetTotalMemory (
	forceFullCollection : boolean
) : long
Not applicable.

Parameters

forceFullCollection

true to indicate that this method can wait for garbage collection to occur before returning; otherwise, false.

Return Value

A number that is the best available approximation of the number of bytes currently allocated in managed memory.

If the forceFullCollection parameter is true, this method waits a short interval before returning while the system collects garbage and finalizes objects. The duration of the interval is an internally specified limit determined by the number of garbage collection cycles completed and the change in the amount of memory recovered between cycles. The garbage collector does not guarantee that all inaccessible memory is collected.

The following example demonstrates how to use the GetTotalMemory method to get and display the number of bytes currently allocated in managed memory.

using System;

namespace GCCollectIntExample
{
    class MyGCCollectClass
    {
        private const long maxGarbage = 1000;
      
        static void Main()
        {
            MyGCCollectClass myGCCol = new MyGCCollectClass();

            // Determine the maximum number of generations the system
	    // garbage collector currently supports.
            Console.WriteLine("The highest generation is {0}", GC.MaxGeneration);
            
            myGCCol.MakeSomeGarbage();

            // Determine which generation myGCCol object is stored in.
            Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));
            
            // Determine the best available approximation of the number 
	    // of bytes currently allocated in managed memory.
            Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));
            
            // Perform a collection of generation 0 only.
            GC.Collect(0);
            
            // Determine which generation myGCCol object is stored in.
            Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));
            
            Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));
            
            // Perform a collection of all generations up to and including 2.
            GC.Collect(2);
            
            // Determine which generation myGCCol object is stored in.
            Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));
            Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));
            Console.Read();
        }

        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();
            }
        }
    }
}

package GCCollectIntExample; 

import System.* ;

class MyGCCollectClass
{
    private static final long maxGarbage = 1000;

    public static void main(String[] args)
    {
        MyGCCollectClass myGCCol = new MyGCCollectClass();

        // Determine the maximum number of generations the system
        // garbage collector currently supports.
        Console.WriteLine("The highest generation is {0}", 
            System.Convert.ToString(GC.get_MaxGeneration()));
        myGCCol.MakeSomeGarbage();

        // Determine which generation myGCCol object is stored in.
        Console.WriteLine("Generation: {0}", 
            System.Convert.ToString(GC.GetGeneration(myGCCol)));

        // Determine the best available approximation of the number 
        // of bytes currently allocated in managed memory.
        Console.WriteLine("Total Memory: {0}", 
            System.Convert.ToString(GC.GetTotalMemory(false)));

        // Perform a collection of generation 0 only.
        GC.Collect(0);

        // Determine which generation myGCCol object is stored in.
        Console.WriteLine("Generation: {0}", 
            System.Convert.ToString(GC.GetGeneration(myGCCol)));
        Console.WriteLine("Total Memory: {0}", 
            System.Convert.ToString(GC.GetTotalMemory(false)));

        // Perform a collection of all generations up to and including 2.
        GC.Collect(2);

        // Determine which generation myGCCol object is stored in.
        Console.WriteLine("Generation: {0}", 
            System.Convert.ToString(GC.GetGeneration(myGCCol)));
        Console.WriteLine("Total Memory: {0}", 
            System.Convert.ToString(GC.GetTotalMemory(false)));
        Console.Read();
    } //main

    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();
        }
    } //MakeSomeGarbage
} //MyGCCollectClass

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.