GC.GetTotalMemory(Boolean) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
检索不包括碎片的堆大小。 例如,如果总 GC 堆大小为 100mb,碎片(即可用对象占用的空间)占用 40mb,则此 API 将报告 60mb。 一个参数,指示此方法是否可以等待较短间隔再返回,以便系统回收垃圾和终结对象。
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
参数
- forceFullCollection
- Boolean
如果此方法可以在返回之前等待垃圾回收发生,则为 true
;否则为 false
。
返回
堆大小(以字节为单位),不包括碎片。
示例
以下示例演示如何使用 GetTotalMemory 方法获取和显示当前在托管内存中分配的字节数。
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
注解
forceFullCollection
如果 参数为 true
,则此方法在系统回收垃圾并完成对象时等待一小段时间,然后返回 。 间隔的持续时间是内部指定的限制,由已完成的垃圾回收周期数和各周期之间恢复的内存量的变化决定。 垃圾回收器不保证收集所有不可访问的内存。