C#的內(nèi)存回收代碼
更新時間:2014年10月29日 09:36:28 投稿:shichen2014
這篇文章主要介紹了C#的內(nèi)存回收代碼,涉及到win32底層操作,具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
本文實(shí)例講述了C#的內(nèi)存回收方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
如下示例代碼是調(diào)用win32底層操作,可實(shí)現(xiàn)內(nèi)存的回收。
復(fù)制代碼 代碼如下:
[DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
/// <summary>
/// 釋放內(nèi)存
/// </summary>
public static void ClearMemory()
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}
public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
/// <summary>
/// 釋放內(nèi)存
/// </summary>
public static void ClearMemory()
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}
希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
C#雙向鏈表LinkedList排序?qū)崿F(xiàn)方法
這篇文章主要介紹了C#雙向鏈表LinkedList排序?qū)崿F(xiàn)方法,涉及C#雙向鏈表的定義與排序技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08c# 9.0新特性nint和Pattern matching的使用方法
這篇文章主要介紹了c# 9.0新特性nint和Pattern matching的使用方法,文中講解非常細(xì)致,幫助你更好的學(xué)習(xí)c# 9.0,有需求的朋友可以參考下2020-06-06C#?wpf實(shí)現(xiàn)截屏框熱鍵截屏的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#?wpf實(shí)現(xiàn)截屏框熱鍵截屏的相關(guān)知識,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,有需要的小伙伴可以學(xué)習(xí)一下2023-09-09