Can I force memory cleanup in C#?

后端 未结 5 632
执念已碎
执念已碎 2021-01-12 18:02

I heard that C# does not free memory right away even if you are done with it. Can I force C# to free memory?

I am using Visual Studio 2008 Express. Does that mat

5条回答
  •  既然无缘
    2021-01-12 18:26

    You can't force C# to free memory, but you can request that the CLR deallocates unreferenced objects by calling

    System.GC.Collect();
    

    There is a method WaitForPendingFinalizers that will "suspend the current thread until the thread that is processing the queue of finalizers has emptied that queue." Though you shouldn't need to call it.

    As the others have suggested head over to the MSDN for more information.

提交回复
热议问题