resourceconnector.cs

来自「Microsoft?Visual C#?.NET (Core Reference」· CS 代码 · 共 51 行

CS
51
字号
using System;

namespace MSPress.CSharpCoreRef.GcDemo
{
    /// <summary>
    /// A simulation of a class that manages an external resource.
    /// The class implements IDisposable, and will free its
    /// resources via either Dispose or the Finalizer.
    /// </summary>
	public class ResourceConnector: IDisposable
	{
		public ResourceConnector()
		{
			// Pretend to allocate our external resource here.
		}

		~ResourceConnector()
		{
			Dispose(false);
		}

		public void UseResource()
		{
		}

public void Dispose()
{
	Dispose(true);
}

		protected void Dispose(bool disposing)
		{
			if(disposing)
			{
				GC.SuppressFinalize(this);
			}
			// Release our external resources here.
		}
		public void CollectAndAudit(int generation, bool waitForGC)
		{
			int myGeneration = GC.GetGeneration(this);
			long totalMemory = GC.GetTotalMemory(waitForGC);
			Console.WriteLine("I am in generation {0}.", myGeneration);
			Console.WriteLine("Memory before collection {0}.", totalMemory);
			GC.Collect(generation);
			Console.WriteLine("Memory after collection {0}.", totalMemory);
		}

	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?