Memory leak in program

Wyko

Member
Joined
Jul 11, 2009
Messages
5
Programming Experience
5-10
This is a little app that sits in your task bar and check a website for the presence of a simple string. The string is either "ONLINE" or "OFFLINE". THe problem is that there seems to be a minor memory leak, since every once in a while (I suppose when the timer expires) the memory it takes up increases by a good bit. Any ideas on what's wrong?


VB.NET:
Public Class frmMain
    Dim WithEvents nIcon As NotifyIcon
    Dim WebB As WebBrowser
    Dim WithEvents timer1 As Timer
    Dim WithEvents timer2 As Timer
    Dim AtLastCheck = False 'Turns true if server was on at last check

    Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        nIcon.Visible = False
    End Sub

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        nIcon = New NotifyIcon
        timer1 = New Timer
        timer2 = New Timer
        Me.Visible = False
        Me.ShowInTaskbar = False

        WebB = New WebBrowser
        WebB.Navigate("http://beta.leagueoflegends.com/server_status.php?raw")

        timer1.Interval = 15000
        timer2.Interval = 15000
        timer2.Start()
        nIcon.Icon = My.Resources.icon_small_disabled
        nIcon.Text = "Loading Server Status"
        nIcon.Visible = True
    End Sub

    Private Sub timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer1.Tick
        WebB = New WebBrowser
        WebB.Navigate("http://beta.leagueoflegends.com/server_status.php?raw")
        timer2.Start()
        timer1.Stop()
    End Sub


    Private Sub CheckServer()

        If WebB.DocumentText.Equals("ONLINE") And AtLastCheck = False Then
            nIcon.ShowBalloonTip(600000, "Server Online", "League of Legends Beta Server is now online!", ToolTipIcon.Info)
            AtLastCheck = True
            nIcon.Text = "Server Staus: Online"
            My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Exclamation)
            nIcon.Icon = My.Resources.icon_medium

        ElseIf WebB.DocumentText.Equals("OFFLINE") Then
            nIcon.Text = "Server Staus: Offline"
            nIcon.Icon = My.Resources.icon_small_disabled
            AtLastCheck = False
        End If

        WebB.Dispose()
    End Sub

    Private Sub nIcon_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles nIcon.DoubleClick
        Dim msg As MsgBoxResult = MessageBox.Show("Do you want to exit Wyko's server status updater?", "Exit?", MessageBoxButtons.YesNo)
        If msg = MsgBoxResult.Yes Then
            nIcon.Visible = False
            Application.Exit()
        End If

    End Sub

    Private Sub timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer2.Tick
        CheckServer()
        timer1.Start()
        timer2.Stop()
    End Sub
End Class
 
Yikes; you create a whole instance of IE just to retrieve a string over HTTP?

Try an HttpWebRequest Class (System.Net)

Few lines of code:

Dim hwr as HttpWebRequest = WebRequest.Create("YOUR URL")
Dim sr as New StreamReader(hwr.GetResponse().GetResponseStream())
Dim response as String = sr.ReadToEnd()
sr.Close()

Why do you have 2 timers?
 
the two timers were because it takes a bit to load the data from the server. If I dont give it time, it sometimes creates an error. Your code should help then? Can you explain what it does?
 
Yikes; you create a whole instance of IE just to retrieve a string over HTTP?

Try an HttpWebRequest Class (System.Net)

Few lines of code:

Dim hwr as HttpWebRequest = WebRequest.Create("YOUR URL")
Dim sr as New StreamReader(hwr.GetResponse().GetResponseStream())
Dim response as String = sr.ReadToEnd()
sr.Close()

Why do you have 2 timers?
Simpler still, WebClient.DownloadString.
 
Can someone explain the differences between these last two examples? Is there a variance in efficiency?
 
Wyko said:
the two timers were because it takes a bit to load the data from the server. If I dont give it time, it sometimes creates an error.
Using the event model will simplify this, the WebBrowser has a DocumentCompleted event for example.
WebBrowser.DocumentCompleted Event (System.Windows.Forms)
WebBrowser.ReadyState Property (System.Windows.Forms)
Wyko said:
Can someone explain the differences between these last two examples? Is there a variance in efficiency?
WebClient Class (System.Net)
WebClient help said:
The WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI.

The WebClient class uses the WebRequest class to provide access to resources.
So it is just a helper class that simplifies coding for common request tasks.
 
the two timers were because it takes a bit to load the data from the server. If I dont give it time, it sometimes creates an error. Your code should help then? Can you explain what it does?

Having reviewed jmc's suggestion it does indeed look even simpler. You wouldnt need two timers just to make something wait a bit longer. Timers are a finite resource ona windows system. Try to use as few of them as possible

You should just have 1 ticking away every second and if it has bee more than 15 seconds (use an integer counter) since the last check and there is no check in progress, do a check
 
Back
Top