软件编程
位置:首页>> 软件编程>> C#编程>> C#的内存回收代码

C#的内存回收代码

作者:shichen2014  发布时间:2023-06-20 06:38:47 

标签:C#,内存,回收

本文实例讲述了C#的内存回收方法。分享给大家供大家参考。具体实现方法如下:

如下示例代码是调用win32底层操作,可实现内存的回收。      
 

[DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
/// <summary>
/// 释放内存
/// </summary>
public static void ClearMemory()
{
    GC.Collect();
    GC.WaitForPendingFinalizers();
    if (Environment.OSVersion.Platform == PlatformID.Win32NT)
    {
 SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
    }
}

希望本文所述对大家的C#程序设计有所帮助。

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com