GC.GetTotalMemory(Boolean) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Recupera le dimensioni dell'heap escluse la frammentazione. Ad esempio, se la dimensione totale dell'heap GC è pari a 100 mb e frammentazione, ad esempio, lo spazio occupato da oggetti gratuiti, richiede 40 mb, questa API segnala 60 mb. Un parametro indica se questo metodo può attendere un breve intervallo prima di restituire un risultato per consentire l'esecuzione della procedura di Garbage Collection e la finalizzazione degli oggetti.
public:
static long GetTotalMemory(bool forceFullCollection);
public static long GetTotalMemory (bool forceFullCollection);
static member GetTotalMemory : bool -> int64
Public Shared Function GetTotalMemory (forceFullCollection As Boolean) As Long
Parametri
- forceFullCollection
- Boolean
true
per indicare che questo metodo può attendere l'esecuzione dell'operazione di Garbage Collection prima di restituire un risultato; in caso contrario, false
.
Restituisce
Dimensioni dell'heap, in byte, escluse la frammentazione.
Esempio
Nell'esempio seguente viene illustrato come usare il metodo per ottenere e visualizzare il GetTotalMemory numero di byte attualmente allocati in memoria gestita.
using namespace System;
const long maxGarbage = 1000;
ref class MyGCCollectClass
{
public:
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()
{
MyGCCollectClass^ myGCCol = gcnew 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 ) );
}
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();
}
}
}
}
open System
let maxGarbage = 1000
type MyGCCollectClass() =
member _.MakeSomeGarbage() =
for _ = 1 to maxGarbage do
// Create objects and release them to fill up memory with unused objects.
Version() |> ignore
[<EntryPoint>]
let main _ =
let myGCCol = MyGCCollectClass()
// Determine the maximum number of generations the system
// garbage collector currently supports.
printfn $"The highest generation is {GC.MaxGeneration}"
myGCCol.MakeSomeGarbage()
// Determine which generation myGCCol object is stored in.
printfn $"Generation: {GC.GetGeneration myGCCol}"
// Determine the best available approximation of the number
// of bytes currently allocated in managed memory.
printfn $"Total Memory: {GC.GetTotalMemory false}"
// Perform a collection of generation 0 only.
GC.Collect 0
// Determine which generation myGCCol object is stored in.
printfn $"Generation: {GC.GetGeneration myGCCol}"
printfn $"Total Memory: {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.
printfn $"Generation: {GC.GetGeneration myGCCol}"
printfn $"Total Memory: {GC.GetTotalMemory false}"
0
Namespace GCCollectInt_Example
Class MyGCCollectClass
Private maxGarbage As Long = 10000
Public Shared Sub Main()
Dim myGCCol As 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()
End Sub
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
Commenti
Se il parametro è true
, questo metodo attende un breve intervallo prima di restituire mentre il forceFullCollection
sistema raccoglie i garbage e finalizza gli oggetti. La durata dell'intervallo è un limite specificato internamente determinato dal numero di cicli di Garbage Collection completati e la modifica nella quantità di memoria recuperata tra i cicli. Garbage Collector non garantisce che venga raccolta tutta la memoria inaccessibile.