gpt4 book ai didi

c# - 如何释放被占用的内存

转载 作者:可可西里 更新时间:2023-11-01 14:38:36 25 4
gpt4 key购买 nike

我的项目中有一个主窗口,主窗口中有许多其他 child 寡妇。
我注意到。当我打开主窗口时占用1500K内存,当打开一个子窗口时添加占用的内存6000K。
当我打开第二个窗口做同样的事情。当我关闭两个子窗口时,不会释放占用的内存。
所以我想要的是在我关闭子窗口时释放占用的内存。
我怎么能做到这一点?
如果可能,请在 vb.net 中使用一些代码示例为我提供建议。
这个问题经常出现在本地 NET 上的计算机中,而不是在我的计算机(具有 SQL 服务器的开发人员计算机)中。

最佳答案

按照 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

(更新)[检查]

如果您的 child 表格有签名。这些默认情况下会添加到表单中。
'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

关于c# - 如何释放被占用的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5191897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com