Why MemoryStream data is not freed from memory
Is there something to do with the following code to release used memory?
Dim objImage As MemoryStream
Dim objwebClient As WebClient
Dim sURL As String = Trim(m_StationInterface.PicLocation)
objwebClient = New WebClient
objImage = New MemoryStream(objwebClient.DownloadData(sURL))
m_imgLiftingEye.Image = Image.FromStream(objImage)
The code is in a pop-up form that should never be deleted. A new image is loaded into the form every time it pops up. However, the size of the process for the application continues to grow every time it passes through this code block.
I've tried objImage.Close () and .Flush (), objWebClient.Dispose (). The process still grows in size by 4 MB after each call. It looks like an old image stored in memory.
a source to share
Image implements IDisposable, so you must dispose of the old image before replacing it with the new one.
Something like (bear with me, I didn't use VB at the time):
Dim objImage As MemoryStream
Dim objwebClient As WebClient
Dim sURL As String = Trim(m_StationInterface.PicLocation)
objwebClient = New WebClient
objImage = New MemoryStream(objwebClient.DownloadData(sURL))
If m_imgLiftingEye.Image Is Not Nothing Then
m_imgLiftingEye.Image.Dispose()
End If
m_imgLiftingEye.Image = Image.FromStream(objImage)
a source to share
MemoryStream implements the IDisposable interface, so you should call Dispose on this object when you're done with it:
objImage = New MemoryStream(objwebClient.DownloadData(sURL)) m_imgLiftingEye.Image = Image.FromStream(objImage) objImage.Dispose()
I would assume that your conclusion was correct; the image (in the memory stream) remains in memory.
Update: As Mark pointed out, Image.FromStream requires the stream to remain open for the life of the image. To solve this problem, the MemoryStream variable must be declared in the same scope as the image (as a field on the form). When loading an image, you first need to check if the MemoryStream is open, and if so, close and delete it before using the variable for the new stream (let's say we call this m_imageStream). Since the image also implements IDisposable, the same is true for this:
If Not m_imageStream Is Nothing Then
m_imageStream.Dispose()
End If
If m_imgLiftingEye.Image Is Not Nothing Then
m_imgLiftingEye.Image.Dispose()
End If
m_imageStream = New MemoryStream(objwebClient.DownloadData(sURL))
m_imgLiftingEye.Image = Image.FromStream(m_imageStream)
a source to share
I know I already gave one answer, but since then I thought ...
You said that this form should never be deleted. In this case, when exactly does this image download occur? My previous answer assumed it was during the Shown event. However, if it is in the Load form , this should only happen after it is complete.
That is, unless more than one instance of the form is created. If this case and the previous form are not reused, you end up with multiple copies of the same form loaded into memory, each with its own copy of the image.
a source to share