【发布时间】:2014-05-14 13:47:10
【问题描述】:
首先谢谢,如果我的英语不好,请原谅我。 我尝试了很多方法来实现 IDisposable 接口。根据我从网上获得的图片,我实现了界面。在我的测试项目中,我尝试了很多,所以如果我将一些图像加载到内存中,我可以看到 IDisposible 按我的预期工作。查看内存(PF 使用)我使用了任务管理器。这里的“test.bmp”是 5mb 文件
这是我做过的三个实验
Dim i As Integer = 0
While (i < 1000)
i = i + 1
Dim bmp As New Bitmap("D:\test.bmp")
End While
它扼杀了我的记忆。然后我尝试了
Dim i As Integer = 0
While (i < 1000)
i = i + 1
Using bmp As New Bitmap("D:\test.bmp")
End Using
End While
PF 一直稳定到 1000。意味着内存已正确释放。
然后我尝试了
Dim i As Integer = 0
Dim dt As DateTime = DateTime.Now
While (i < 1000)
i = i + 1
Using oTestDispose As New clsTestDispose
End Using
End While
Public Class clsTestDispose
Implements IDisposable
Dim bmp As New Bitmap("D:\test.bmp")
Private disposedValue As Boolean = False ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: free unmanaged resources when explicitly called
Me.Dispose()
End If
' TODO: free shared unmanaged resources
End If
Me.disposedValue = True
End Sub
#Region " IDisposable Support "
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
这段代码告诉“System.StackOverFlowException”。为什么 dispose 没有清除其中的图像对象?
【问题讨论】:
标签: vb.net idisposable