Why is it leaking memory?

It is interesting. We spent the last day trying to fix an issue with the following (deprecated) code that keeps growing its process size. This is done in Visual Studio 2003.

We have a form where we display an image (from a MemoryStream) and some text and a button. Nothing special. It looks something like this:

  Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    MyBase.OnLoad(e)
    Try
      m_lblWarning.Visible = False

      m_grpTitle.Text = m_StationInterface.ProcessToolTitle
      m_lblMessage.Text = m_StationInterface.ProcessToolMessage

      Dim objImage As MemoryStream
      Dim objwebClient As WebClient
      Dim sURL As String = Trim(m_StationInterface.ProcessToolPicLocation)

      objwebClient = New WebClient

      objImage = New MemoryStream(objwebClient.DownloadData(sURL))
      m_imgLiftingEye.Image = Image.FromStream(objImage)

      m_txtAcknowledge.Focus()
    Catch ex As Exception
      '*** This handles a picture that cannot be found without erroring'
      m_lblWarning.Visible = True
    End Try
  End Sub

      

This form is often closed and opened. Each time it is reopened, the process memory usage increases by about 5mb. When the form is closed, it does not abandon its previous use. Resources remain dedicated to the unpublished form. The form is displayed as follows:

      m_CJ5Form_PTOperatorAcknowlegement = New CJ5Form_PTOperatorAcknowlegement
      m_CJ5Form_PTOperatorAcknowlegement.stationInterface = m_StationInterface
      m_CJ5Form_PTOperatorAcknowlegement.Dock = DockStyle.Fill
      Me.Text = " Acknowledge Quality Alert"

      '*** Set the size of the form'
      Me.Location = New Point(30, 30)
      Me.Size = New Size(800, 700)

      Me.Controls.Add(m_CJ5Form_PTOperatorAcknowlegement)

      

The control is later removed from the form after being closed:

Me.Controls.Clear()

      

Now. We've tried so many things. We found that Disposing does nothing, and indeed the IDisposable interface does not actually touch memory. If we don't create a new CJ5Form_PTOperatorAcknowledgment every time, the process will NOT grow in size. But uploading a new image to this form still causes the process to grow in size all the time.

Any suggestions would be appreciated.

0


a source to share


2 answers


You must get rid of the WebClient object and other managed unmanaged resources that you no longer need.


objImage = New MemoryStream(objwebClient.DownloadData(sURL))
objwebClient.Dispose() ' add call to dispose

      

An even better coding way is to use the 'using' statement:


using objwebClient as WebClient = New WebClient      
objImage = New MemoryStream(objwebClient.DownloadData(sURL))      
end using

      

For more information, please look for the 'Dispose' and 'IDisposable' pattern implementation on google and stackoverflow.



One final tip: don't use a memory stream if possible. Load the image directly from the file if you don't need to store it in RAM.

Edit

If I understand your code correctly, maybe something like this will work:


Dim objImage As MemoryStream      
Dim objwebClient As WebClient      
Dim sURL As String = Trim(m_StationInterface.ProcessToolPicLocation)      
using objwebClient as WebClient = New WebClient      
  using objImage as MemoryStream = New MemoryStream(objwebClient.DownloadData(sURL))      
    m_imgLiftingEye.Image = Image.FromStream(objImage)
  end using
end using

      

+2


a source


I don't know why exactly this is leaking, but I can recommend you to use the .NET Memory Profiler . If you run your application with this, it will give you a very good idea of ​​what objects are not being hosted, and if it helps you fix the cause. It got a free trial, but it's worth it to buy.



0


a source







All Articles