我在VB.NET中开发了桌面应用程序。框架是3.5和后端作为SQL Server 2005.内存不足异常
下面是代码[释放内存时关闭窗体]我已经写在主窗体,从中继承所有窗体。
Private Declare Auto Function SetProcessWorkingSetSize Lib "kernel32.dll" (ByVal procHandle As IntPtr, ByVal min As Int32, ByVal max As Int32) As Boolean
Private Declare Function EmptyWorkingSet Lib "psapi.dll" (ByVal hProcess As IntPtr) As Long
Private Sub frmEMR_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
GC.Collect()
GC.WaitForPendingFinalizers()
GC.RemoveMemoryPressure(GC.GetTotalMemory(True))
EmptyWorkingSet(Process.GetCurrentProcess.Handle)
SetProcessWorkingSetSize(Process.GetCurrentProcess.Handle, -1, -1)
End Sub
虽然我写了这个堆内存有争议的增加。
此外,未管理的内存增加。
任何人都可以帮我解决这个问题。
感谢&问候
血管沙阿
是的,你是对的,但我想写一些概括,所以我可以写在主表格和工作的所有继承形式。 –
通常情况下,最好的做法是尽快处置完成。这样你就不会发生内存不足错误的风险。基本上,您的应用程序正在创建所有这些实例,并使用该内存,并且在关闭表单之前内存不会被释放。在您完成后立即处置的地方,内存可用于下一个命令。 – JRSofty