如何释放占用的内存

时间:2011-03-04 09:27:10

标签: c# .net windows vb.net winforms

我的项目中有一个主窗口,主要内部还有许多其他儿童寡妇 我注意到。当我打开主窗口占用1500K的内存时,打开一个子窗口然后加入占用的内存6000K 当我打开第二个窗口做同样的事情。当我关闭两个子窗口时,未释放占用的内存 所以我想要的是在关闭子窗口时释放占用的内存 我怎么能这样做?
如果可能的话,请在vb.net中给我一些代码示例 这个问题经常在本地NET上的计算机中找不到,而不是在我的计算机上(开发人员计算机上有SQL服务器)。

7 个答案:

答案 0 :(得分:13)

答案 1 :(得分:1)

使用block,自动释放内存

Using { resourcelist | resourceexpression }
    [ statements ]
End Using

答案 2 :(得分:1)

这是你不应该关心的事情。

.NET Framework Garbage Collector将为您完成此工作。

  

.NET Framework的垃圾收集器   管理分配和发布   内存为您的应用程序。每一次   你创建一个新的对象,常见的   语言运行库为内存分配内存   托管堆中的对象。如   只要地址空间可用   托管堆,运行时   继续为新的空间分配空间   对象。

修改

你必须确保你不再使用你所获得的资源。 垃圾收集器的功能位于命名空间GC下的类System中。

为了调用它,您可以执行GC.Collect(),但我建议您阅读有关此主题的更多信息,并查看一些示例,例如this one

答案 3 :(得分:0)

垃圾收集器将为您完成工作,因此您真的不应该介意它。当且仅当您使用非托管资源(com interop / PInvoke)时,您应该更深入地了解内部。

答案 4 :(得分:0)

我不是VB.net的专家,但据我所知,它有垃圾收集器。这通常意味着关闭子窗口不会释放内存,但如果删除对子窗口的所有引用,垃圾收集器可能会在下次运行时释放它。

此外,您通常可以“请求”垃圾收集器运行,但它通常会自行“决定”何时运行。

答案 5 :(得分:0)

按照Pranay的建议使用将默认调用Dispose方法。否则显然你必须在你的子表单上调用this.close()之后调用this.dispose()。但请确保您不会在关闭后使用子表单元素或值。处置将最终清除一切。

MSDN处理非托管资源的示例

Imports System
Imports System.ComponentModel

' The following example demonstrates how to create
' a resource class that implements the IDisposable interface
' and the IDisposable.Dispose method.
Public Class DisposeExample

   ' A class that implements IDisposable.
   ' By implementing IDisposable, you are announcing that 
   ' instances of this type allocate scarce resources.
   Public Class MyResource
      Implements IDisposable
      ' Pointer to an external unmanaged resource.
      Private handle As IntPtr
      ' Other managed resource this class uses.
      Private component As component
      ' Track whether Dispose has been called.
      Private disposed As Boolean = False

      ' The class constructor.
      Public Sub New(ByVal handle As IntPtr)
         Me.handle = handle
      End Sub

      ' Implement IDisposable.
      ' Do not make this method virtual.
      ' A derived class should not be able to override this method.
      Public Overloads Sub Dispose() Implements IDisposable.Dispose
         Dispose(True)
         ' This object will be cleaned up by the Dispose method.
         ' Therefore, you should call GC.SupressFinalize to
         ' take this object off the finalization queue 
         ' and prevent finalization code for this object
         ' from executing a second time.
         GC.SuppressFinalize(Me)
      End Sub

      ' Dispose(bool disposing) executes in two distinct scenarios.
      ' If disposing equals true, the method has been called directly
      ' or indirectly by a user's code. Managed and unmanaged resources
      ' can be disposed.
      ' If disposing equals false, the method has been called by the 
      ' runtime from inside the finalizer and you should not reference 
      ' other objects. Only unmanaged resources can be disposed.
      Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
         ' Check to see if Dispose has already been called.
         If Not Me.disposed Then
            ' If disposing equals true, dispose all managed 
            ' and unmanaged resources.
            If disposing Then
               ' Dispose managed resources.
               component.Dispose()
            End If

            ' Call the appropriate methods to clean up 
            ' unmanaged resources here.
            ' If disposing is false, 
            ' only the following code is executed.
            CloseHandle(handle)
            handle = IntPtr.Zero

            ' Note disposing has been done.
            disposed = True

         End If
      End Sub

      ' Use interop to call the method necessary  
      ' to clean up the unmanaged resource.
      <System.Runtime.InteropServices.DllImport("Kernel32")> _
      Private Shared Function CloseHandle(ByVal handle As IntPtr) As [Boolean]
      End Function

      ' This finalizer will run only if the Dispose method 
      ' does not get called.
      ' It gives your base class the opportunity to finalize.
      ' Do not provide finalize methods in types derived from this class.
      Protected Overrides Sub Finalize()
         ' Do not re-create Dispose clean-up code here.
         ' Calling Dispose(false) is optimal in terms of
         ' readability and maintainability.
         Dispose(False)
         MyBase.Finalize()
      End Sub
   End Class

   Public Shared Sub Main()
      ' Insert code here to create
      ' and use the MyResource object.
   End Sub

End Class

(适用更新)[检查]

如果您的子表单有签名。这些默认情况下会添加到表单中。

'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
    MyBase.Dispose(disposing)
End Sub

答案 6 :(得分:0)

您可以使用一种可用的内存分析器,例如: ANTS Memory Profiler(看看Using ANTS Memory Profiler to track down a memory leak in a WinForms application)。您也可以使用WinDbg,但没有经验,它将比专业工具更复杂。

常见的“内存泄漏”原因之一就是添加“外部”事件处理程序(例如静态或长生命对象),而不是在表单销毁时将其删除,因此GC“认为”你引用了表单而不是收集其数据。

Why and How to avoid Event Handler memory leaks ?

.NET Memory Leak Case Study: The Event Handlers That Made The Memory Baloon

Fundamentals of Garbage Collection

Dispose, Finalization, and Resource Management

How to identify memory leaks in the common language runtime

How to detect and avoid memory and resources leaks in .NET applications