C#如何立即回收内存
2017-09-17 11:16
344 查看
1.把对象赋值为null
2.立即调用GC.Collect();
注意:这个也只是强制垃圾回收器去回收,但具体什么时候执行不确定。
代码:
2.立即调用GC.Collect();
注意:这个也只是强制垃圾回收器去回收,但具体什么时候执行不确定。
代码:
public partial class Form1 : Form { [System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet =System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)] private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int maximumWorkingSetSize); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ClearMemory(); } #region 内存回收 public void ClearMemory() { GC.Collect(); GC.SuppressFinalize(this); if (Environment.OSVersion.Platform == PlatformID.Win32NT) { SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1); } } #endregion private void timer1_Tick(object sender, EventArgs e) { string s="clearMemory";
Form dt = new Form(); dt.Text = s;
//如果垃圾产生于timer中可能无法立刻回收资源,需加载timer tick事件中,立刻回收资源,或使用另外的timer控制回收时间。 GC.Collect(); } }
相关文章推荐
- C#如何立即回收内存
- C#如何立即回收内存
- C#如何立即回收内存
- C#共享内存操作是如何实现
- jvm 内存回收finalize如何在垃圾清除前工作原理
- C# 两个程序如何获得同用内存,一程序调用另外一程序内的方法或函数
- 深入了解C#系列:谈谈C#中垃圾回收与内存管理机制
- 内存回收,Dispose,Close,Finalie(C#中的析构函数)
- C# list存储的数据格式以及默认初始化空间,内存回收分析
- C# Winform应用程序内存回收
- 浅谈C#内存回收与Dispose﹐Close﹐Finalize方法
- GC 回收WPF 内存 C#通用
- C#如何限制程序内存使用的大小
- 在VS中调试C#程序,如何查看内存、寄存器、反汇编
- 深入了解C#系列:谈谈C#中垃圾回收与内存管理机制
- 浅谈如何灵活运用c#函数操作内存
- 浅谈C#内存回收与Dispose﹐Close﹐Finalize方法[转]
- 在VS中调试C#程序,如何查看内存、寄存器、反汇编
- Flex 如何调用System.gc 回收内存
- c# 如何查看某个程序所占内存