提问人:czxinc 提问时间:1/15/2023 最后编辑:czxinc 更新时间:1/16/2023 访问量:76
频繁创建线程时内存泄漏
Memory leak when thread create frequently
问:
我想在触发事件时生成图像。当事件再次触发并且图像仍在生成时。它将中止生成并再次生成。 但是我的代码会导致内存泄漏。应用程序的内存使用量将迅速增长。有谁知道怎么了?
编辑:我在线程中止后放置。但这没用。GC.Collect()
编辑:当计时器的间隔设置为一个大数字(如 2000 年)时,泄漏速度会变慢。
这是我的代码:
Public Class Form1
Dim a As Threading.Thread
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Main()
End Sub
Sub Main()
If a IsNot Nothing AndAlso a.IsAlive Then
a.Abort()
a.Join()
'GC.Collect() 'not working
'GC.WaitForPendingFinalizers() 'not working
End If
a = New Threading.Thread(AddressOf func)
a.Start()
End Sub
Sub func()
Dim newObj As Class1 = Nothing
Try
newObj = New Class1
Catch ex As Threading.ThreadAbortException
newObj?.Dispose()
End Try
End Sub
End Class
Public Class Class1
Implements IDisposable
Dim Img As New Bitmap(4095, 4095)
Public Sub Dispose() Implements IDisposable.Dispose
Img?.Dispose()
End Sub
End Class
答: 暂无答案
评论
GC.WaitForPendingFinalizers()
Thread.Abort()