VB.NET - 内存泄漏使用while循环,线程和wmi

时间:2013-07-02 17:47:47

标签: vb.net multithreading memory-leaks wmi

此代码开始使用40 MB RAM,2小时后开始使用120 MB RAM。我试图更改一些行,但使用的内存仍然上升。我究竟做错了什么?是while循环吗?是theads?是ContextDB.SaveChanges()吗?

UPDATE 1 :显然在线程上使用“IsBackground = True”并处理每个“m”对象解决了问题。使用的RAM稳定在48 MB。谢谢你们。

Dim Work As New Thread(AddressOf MonitorServer)
Work.IsBackground = True 'Added this line
Work.Start(Server.ServerID)

Using searcher As New ManagementObjectSearcher(Scope, New ObjectQuery("SELECT * FROM Win32_OperatingSystem"))
    Using queryCollection = searcher.Get()
        For Each m In queryCollection
            Server.Host = m("csname")
            Server.OS = m("Caption")
            m.Dispose() 'Added this line
            m = Nothing 'Added this line
        Next
    End Using
End Using

更新2 :问题仍然存在,24小时后代码使用 350 MB !!!!

原始代码:

Full code: http://200.98.144.175/monitor.txt

Private Shared ServerList As New List(Of Server)

Private Shared Sub StartMonitor()

Using ContextDB As New EnvironmentMonitorEntities

    While True

        Dim Servers = (From A In ContextDB.Server).ToList

        For Each Server In Servers

            If Not ServerList.ToList.Contains(Server) Then

                ServerList.Add(Server)

                Dim Work As New Thread(AddressOf MonitorServer)
                Work.Start(Server) 'Start a thread for each server

            End If

        Next

        GC.Collect() 'Force CG
        Thread.Sleep(CInt(TimeSpan.FromSeconds(10).TotalMilliseconds)) 'Check new servers each 10 seconds

    End While

End Using

End Sub

Private Shared Sub MonitorServer(p As Object)

Dim Server = CType(p, Server)

Using ContextDB As New EnvironmentMonitorEntities

    While True

        Dim ServerID = Server.ServerID
        Dim ServerStatus = (From A In ContextDB.ServerStatus Where A.ServerID = ServerID).First
        Dim Scope As New ManagementScope("\\" + Server.IP + "\root\CIMV2")

        ServerStatus.LastUpdate = Now
        ContextDB.SaveChanges()

        Scope.Connect() 'Connect on server

        Alerts.SetServerStatus(ServerStatus, AlertType.Online)
        ContextDB.SaveChanges()

        Using searcher As New ManagementObjectSearcher(Scope, New ObjectQuery("SELECT * FROM Win32_OperatingSystem"))
            Using queryCollection = searcher.Get()
                For Each m In queryCollection
                    Server.Host = m("csname")
                    Server.OS = m("Caption")
                Next
            End Using
        End Using

        ContextDB.SaveChanges()

        Using searcher As New ManagementObjectSearcher(Scope, New ObjectQuery("SELECT * FROM Win32_Processor"))
            Using queryCollection = searcher.Get()
                For Each m In queryCollection
                    Server.Processor = m("Name")
                    Server.Architecture = m("Architecture")
                Next
            End Using
        End Using

        ContextDB.SaveChanges()

        Using searcher As New ManagementObjectSearcher(Scope, New ObjectQuery("SELECT * FROM Win32_OperatingSystem"))
            Using queryCollection = searcher.Get()
                For Each m In queryCollection
                    ServerStatus.RAMUsage = m("FreePhysicalMemory")
                    ContextDB.SaveChanges()
                Next
            End Using
        End Using

        GC.Collect()
        Thread.Sleep(CInt(TimeSpan.FromSeconds(3).TotalMilliseconds)) 'Monitor server each 3 seconds

    End While

End Using 'ContextDB

End Sub

1 个答案:

答案 0 :(得分:1)

哇,一旦你写了GC.Collect()这句话本来应该是一个提示,这可能不是解决这个问题的最佳方法。你看过Server Health Using PowerShell了吗?我认为这更适合你正在做的事情,但只有你才能成为法官。祝你好运。