GC.MaxGeneration Propriedade
Definição
Obtém o número máximo de gerações que o sistema dá suporte atualmente.Gets the maximum number of generations that the system currently supports.
public:
static property int MaxGeneration { int get(); };
public static int MaxGeneration { get; }
member this.MaxGeneration : int
Public Shared ReadOnly Property MaxGeneration As Integer
Valor da propriedade
Um valor que varia de zero ao número máximo de gerações com suporte.A value that ranges from zero to the maximum number of supported generations.
Exemplos
O exemplo a seguir demonstra como usar a MaxGeneration propriedade para exibir o número de maior geração em uso no momento.The following example demonstrates how to use the MaxGeneration property to display the largest generation number currently in use.
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();
}
}
}
}
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
Comentários
O número de geração ou a idade de um objeto é uma medida relativa definida pela implementação do tempo de vida de um objeto.The generation number, or age, of an object is an implementation-defined relative measure of an object's lifespan. Os objetos criados mais recentemente estão na geração 0 e os objetos mais antigos estão em uma geração menor ou igual à geração retornada pela MaxGeneration propriedade.The most recently created objects are in generation 0 and the oldest objects are in a generation less than or equal to the generation returned by the MaxGeneration property.
O coletor de lixo assume que a memória mais nova provavelmente está qualificada para a coleta de lixo do que a memória mais antiga.The garbage collector assumes that newer memory is more likely to be eligible for garbage collection than older memory. Portanto, o coletor de lixo melhora seu desempenho ajustando os números de geração cada vez que recupera a memória e o MaxGeneration valor da propriedade pode crescer ao longo do tempo.Therefore, the garbage collector improves its performance by adjusting generation numbers each time it reclaims memory, and the MaxGeneration property value can grow over time.
Se a duração do objeto for implementada, a MaxGeneration propriedade retornará o número de geração máximo usado pelo sistema; caso contrário, essa propriedade retornará zero.If object aging is implemented, the MaxGeneration property returns the maximum generation number used by the system; otherwise, this property returns zero.
Para essa implementação, é garantido que o valor retornado pela MaxGeneration Propriedade permaneça constante durante o tempo de vida de um aplicativo em execução.For this implementation, the value returned by the MaxGeneration property is guaranteed to remain constant for the lifetime of an executing application.
Use a MaxGeneration propriedade para determinar o valor máximo que você pode especificar ao chamar o Collect(Int32) método que usa um parâmetro de geração.Use the MaxGeneration property to determine the maximum value you can specify when calling the Collect(Int32) method that takes a generation parameter.